Page 1 of 1
function return values
Posted: Fri Aug 24, 2007 3:30 pm
by s.dot
Are any of these ways of returning, invalid or could pose a problem?
Code: Select all
private function _compareHashes($hash1, $hash2)
{
return $hash1 == $hash2;
}
private function _compareHashes($hash1, $hash2)
{
return $hash1 == $hash2 ? true : false;
}
private function _compareHashes($hash1, $hash2)
{
if ($hash1 == $hash2)
{
return true;
}
return false;
}
I've been using the first one a lot lately, using operators to be evaluated in the return statement. I'm unaware of if this is practiced a lot, or if it could potentially cause anything different than an if{} statement.
Posted: Fri Aug 24, 2007 3:33 pm
by John Cartwright
It all seriously depends on your personal preference and readability. I would lean towards the first example though.. although I usually wrap my expressions in brackets.
Posted: Fri Aug 24, 2007 9:30 pm
by s.dot
Jcart wrote:It all seriously depends on your personal preference and readability. I would lean towards the first example though.. although I usually wrap my expressions in brackets.
Any reason for that codewise? Or just readability?
Posted: Fri Aug 24, 2007 9:46 pm
by feyd
Technically, it would be the most likely to have the least number of opcodes thereby executing a fraction of a second faster, depending on how efficient the opcode optimizer is.
Posted: Fri Aug 24, 2007 10:53 pm
by Ambush Commander
First, for me. Especially since this a private method, and therefore should be small and easily refactorable should such a procedure become necessary.
Posted: Fri Aug 24, 2007 11:02 pm
by Kieran Huggins
#1 FTW!
It's easiest to read. period. It takes a lot of page requests to make up for the time lost due to overly-optimized code.
Posted: Fri Aug 24, 2007 11:03 pm
by Christopher
They are all identical to the code that calls the function. The question then becomes which is the most readable, consistent and maintainable considering the rest of your codebase.
The first would be my preference as well -- mainly for readability. With it I can see most clearly what is being returned. The others require scanning multiple lines or operators.