You mention compiled languages, so you mean language like: C, C++, C#, Java, etc.
I mean, yea... in those cases, the compiler will note error before it can even compile at all.
But what about interpreted languages like PHP? There's a thing as "too much" code validation, right?
Let me narrow it down as you requested:
function example($arg, $arr) {
if (!isset($arg) || !isset($arr) || empty($arg) || empty($arr)) {
return false;
}
$result = array();
if (function_exists('another_function')) {
if (is_array($arr) && count($arr) > 0) {
foreach ($arr as $element) {
if (!empty($element) && $element === $arg) {
array_push($result, $element);
}
}
}
if (is_array($result) && count($result) > 0) {
return $result;
}
}
return false;
}
I wrote a simple PHP function that returns all matching elements of an array. I also put various validations in the function to prevent it from breaking. I purposely put too many validations. Less likely to break, but pretty long, right?
I could also let write the code like this:
function example($arg, $arr) {
$result = array();
foreach ($arr as $element) {
if ($element === $arg) {
array_push($result, $element);
}
}
return $result;
}
This code lacks validation, but is much shorter and easier to read. It will break at the foreach if I put in a variable that contains anything other than an array.
So there's such thing as too much code validation and I am looking for a rule of thumb to not use too many code validations.