Argument validation syntax standards?
I was just wondering if any such practices have been taken as unwritten standards in function/method argument validation syntax.
The two that stick out so far as I can see are:
PHP Code:
function foo($a, $b, $c){
if(!$a){ throw new Exception("..."); }
if($b < 0){ throw new Exception("..."); }
if(!test($c)){ throw new Exception("..."); }
//do stuff with $a, $b, and $c
}
And alternatively:
PHP Code:
function foo($a, $b, $c){
if($a){
if($b >= 0){
if(test($c)){
//do stuff with $a, $b, and $c
}else{ throw new Exception("..."); }
}else{ throw new Exception("...") }
}else{ throw new Exception("...") }
}
Some of the advantages/disadvantages of both seem clear to me, but I was wondering if a higher knowledge could direct me towards any documentation on any sort of standards that exist to satisfy this question.
Personal opinions are of course welcome, but if you use either (or another method) of structuring argument validation within functions and methods, any insight as to how you made your choice would be greatly appreciated :)
Re: Argument validation syntax standards?
I hate nesting IF statements like that and I personally prefer to have logic like that placed at the top, like in your first example. when you nest the statements like that, maintenance is annoying and it can get incredibly messy.
as far as I know, there is no real standard for that sort of thing.
Re: Argument validation syntax standards?
PEAR has a couple of coding standards.
The 'Best Practices' mentions that code with the *lowest amount of indentation* is more readable.
And if I look at your examples again, I say I'd agree.