[development] comment module: minor issue unreachable code
Jakob Petsovits
jpetso at gmx.at
Fri Aug 24 12:21:52 UTC 2007
On Wednesday, 22. August 2007, Doug Green wrote:
> For example, I do prefer:
>
> function example() {
> if (variable_get('example', 0)) {
> // ... do something and return
> return 'something';
> }
> // ... falls off the end with an empty return
> }
Regarding the structure, I agree.
What I don't like is to mix up return statements with and without return value
in the same function. That is, imho, harder to grasp for people trying to
figure how they should parse the return value, and I believe that when there
is something to return at one place in the function then there should always
be a return statement for each code path, and each return statement should
include a real return value. Like,
function example() {
if (variable_get('example', 0)) {
// ... do something and return
return 'something';
}
return NULL;
}
Does the same thing, but instead of the caller not noticing that there's
actually some different possibility to 'something', it's instantly
recognizable that the function needs to be checked on isset() as well.
Also, always having return values for retrieval functions prevents cases like
arg() where the docs say that FALSE is returned if the arg is not present,
but actually it returns without return statement and thus returns NULL.
Stuff like this would not happen with the more verbose approach.
More information about the development
mailing list