1

If I have

return recordsAffected > 0;

which would return either true or false, do I need to put return recordsAffected > 0 ? true : false?

BlitZ
  • 12,038
  • 3
  • 49
  • 68
Stephen K
  • 697
  • 9
  • 26

3 Answers3

9

No, you do not have to as your code works just fine. You may find some developers recommend doing it because it is clearer to read and understand but that's a matter of personal opinion.

Always code as if the person maintaining your code is a violent psychopath who knows where you live - Martin Golding

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I'm upvoting *purely* for the advice regarding potential psychopathy. – David Thomas Oct 02 '13 at 19:57
  • Me too; in case I've disclosed my address. – Bathsheba Oct 02 '13 at 19:57
  • 2
    I'm a violent psychopath who will eat your dog if you do `foo ? true : false`. OK, I will actually only get mildly annoyed and tweet about it, but it's almost the same thing. – kojiro Oct 02 '13 at 19:57
  • 3
    Of course, if a developer recommends `return condition ? true : false;`, then you shouldn't listen to their opinion... – Oliver Charlesworth Oct 02 '13 at 19:58
  • I was wondering who did you quote, but you were too quick to edit answer. [Also on SO](http://stackoverflow.com/questions/876089/who-wrote-this-programing-saying-always-code-as-if-the-guy-who-ends-up-maintai). – Glavić Oct 02 '13 at 19:59
  • I had just read it again yesterday so it was fresh in my mind. Everything but the author, that is. But that was easy enough to find. – John Conde Oct 02 '13 at 20:04
1

You do not need that. Main reason, is that it would be 2 operations instead of one: first for comparison, second for value choosing. I also want to mention, that each conditional operator (even ternary) affects performance.

Short test:

<?php
header('Content-Type: text/plain; charset=utf-8');

$start  = microtime(true);

for($i = 1; $j = 1, $i <= 10000000; $i++){
    ($i == $j);
}

$end    = microtime(true);

echo 'Not ternary: ', $end - $start, PHP_EOL;

$start  = microtime(true);

for($i = 1; $j = 1, $i <= 10000000; $i++){
    ($i == $j ? true : false);
}

$end    = microtime(true);

echo 'Ternary: ', $end - $start, PHP_EOL;
?>

An it's results.

BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • 1
    an interesting corner for optimization (from the lang POV I mean); I thought even PHP would have been able to optimize `x ? true : false` as simply `x` – ShinTakezou Oct 02 '13 at 20:35
  • @ShinTakezou yes... Still, I belive that is the one of thing, that may kill performance one day. Might add [this](http://en.wikipedia.org/wiki/Analysis_of_algorithms) and [this](http://en.wikipedia.org/wiki/Algorithmic_efficiency) articles. "I understand" shouldn't affect performance. And, yeah, I was suprised by execution time too. `;)` – BlitZ Oct 03 '13 at 02:49
0

You certainly do not need to. The intention and semantics of

return recordsAffected > 0;

is perfectly clear. This should hold true for every decent programmer reading your code.

return recordsAffected > 0 ? true : false;

is redundant at best, but I'd go further and call it detrimental. The second snippet does not add anything to the statement but complexity. I'll bet if you did not write code like this all the time (and I believe most decent progammers don't) the second statement will take you at least two passes to grasp the meaning, if not more. When there are two semantically equal solutions you should always stick to the clearest one, which is not necessarily the most explicit one. Nobody would ever write something like

if(recordsAffected > 0 ? true : false)
{
}
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57