views:

178

answers:

9

Which is better?

function test($val = 'a') {
    if($val == 'a') {
        return true;
    }
    return false;
}

or

function test($val = 'a') {
    if($val == 'a') {
        return true;
    } else {
        return false;
    }
}

Effectively, they do the same thing. If $val isn't 'a', the function returns false. Just personal preference?

+21  A: 

They are the same. However, for this case, I prefer:

function test($val = 'a') {
    return ($val == 'a');
}
Flavius Stef
A: 

I'd stick with the first one, the simpler the code (and easy to read) the better.

Federico Cristina
+9  A: 

Over those, I prefer the second for clarity. However, I actually prefer

return ($val == 'a');
mathepic
+7  A: 

I think it comes down to how the comparison "feels" to you. I'd use the first if it seemed like $val being "a" was a special case, and usually the function returned false. I'd use the second if it felt more like it was 50/50 which way it would go.

Ned Batchelder
A: 

While you are reading second code block. You can easily understan that it returns false when val is not equal to 'a'.

But at the first code block it's a bit hard to understand when it'll return false. It's not so hard at that example but i assume that your if clauses won't be so simple.

Ahmet Kakıcı
+3  A: 

In PHP, if nothing is done in a function and the end is reached it will be as if it returns false. Because of this it's never necessary to return false if nothing else is to be executed inside the function. This leaves us with this:

function test($val = 'a') {
    if($val == 'a') {
        return true;
    }
}

If there is only one command after an if, elseif or else statement, the braces ("{" "}") aren't necessary, which make us end up with this:

function test($val = 'a') {
    if($val == 'a') return true;
}

In PHP you can actually return a comparison, which will be executed right before it's returned. This is what some of the others who answered this post suggested. Doing this leave us with this code:

function test($val = 'a') {
    return ($val == 'a');
}

True will be returned if the block "($val == 'a')" is true, otherwise false will be returned, since it's not true. Logic.

I actually tend to use the second convention I presented, just out of habit. Seeing the beauty of the simplicity of the third one presented by the others I will probably switch to that when applicable.

EDIT:

If you want to write code that is easier for non-PHP experts to understand, another alternative might be the following:

function test($val = 'a') {
    if($val == 'a')
        return true;
    else
        return false;
}

I'd say that not using the braces in the circumstances as described in my second example gives you easier-to-read code, since the braces tend to make your code look messy if they don't contain multiple rows.

Emanuel
This may all be true and accurate, but your first two examples will not result in code that is clear and easy to understand for anyone other than a PHP expert. Only the third will be clear to most programmers.
Craig Trader
That being said, this post is about PHP programmers, so his point is perfectly valid. And I learned something new about PHP today!
Blindy
I do agree that the third example is the most logic one since it implies the existance of "return false". But then, knowing and understanding these kind of things about a programming language _may_ help one write easier-to-understand and more efficient code, so I wrote that.
Emanuel
+2  A: 

I really believe it's what ever intent your trying to convey for the code. If you look at this:

function test($val = 'a') {
    if($val !== 'a') {
        return false;
    }
    return true;
}

You can see that it does the same thing as your examples, but it has a different intention. For this example it really wouldn't make any sense to have:

function test($val = 'a') {
    if($val !== 'a') {
        return false;
    }
    else {
        return true;
    }
}

I think that @Ned had it, because he is trying to convey an intention for the if operation.

Gutzofter
Your (first) variant is a nice example of the happy path. http://en.wikipedia.org/wiki/Happy_path
nikc
'Blue pill or Red pill...' LOL! Happy path is always the better path!
Gutzofter
A: 

Rather than:

function test($val = 'a') {
    if($val == 'a') {
        return true;
    } else {
        return false;
    }
}

The below approach is better, there is no more overhead of else and code is also short:

function test($val = 'a') {
    if($val == 'a') {
        return true;
    }
    return false;
}

It could be made even shorter:

function test($val = 'a')
{
    return ($val == 'a');
}
Sarfraz
+2  A: 

If you were not returning boolean I would choose the first form. In this case, I would simply do :

return ($val == 'a');

as others have suggested.

fastcodejava