0

I have a question, might sound simple, but I am trying to understand what or if it makes any technical differences how we write the conditional statements in code. Here I am talking about PHP, but it may apply to other languages as well.

I want to understand if there is any difference between

Check IF NOT then Return

function my_func($var or $val)
{
    if ($var != $val) {
        return false;
    }

   // do the stuff ...

}

Check IF then DO

function my_func($var or $val)
{
    if ($var == $val) {
        // do the stuff ...
    }

   return false;

}

What I can assume that retuning in IF NOT will be better in performance as if conditions don't match it will not even go through the code. However, that applies to another approach as well. So which one is better or it depends on the situation?

Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • 2
    The two patterns should have the same performance, as they both either run some code or return based on a single equality/inequality check. Well there _is_ a difference between the two; one returns a boolean while the other is void. Maybe you can comment on that. – Tim Biegeleisen Sep 22 '19 at 05:59
  • 3
    Most high-level languages (especially those that don’t cleanly JIT) are too over-headed and/or abstracted to really have *any* noticeable performance difference: use the form that more clearly represents the issue. – user2864740 Sep 22 '19 at 06:02
  • 2
    Looks very similar to https://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement – Nigel Ren Sep 22 '19 at 06:02
  • Also https://stackoverflow.com/questions/38920362/how-is-return-early-concept-useful-in-php and https://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – Nigel Ren Sep 22 '19 at 06:03
  • Well, the way both get interpreted by PHP would be different, but the end result is the same and so is the performance. – nice_dev Sep 22 '19 at 06:22
  • @UlrichEckhardt alright, I did that. – Code Lover Sep 22 '19 at 08:50
  • Okay, you changed the `return false` part. However, it's still fucked: In the top snippet, you either return `false` or the (implicit) `null`. In the lower one, you always return `false`. Also, you have `$var or $val` as parameters (WTF?) and then you compare `$var == $val`? Just use `if (...some condition...)` and drop the parameters, but as it is now it just confuses the reader. – Ulrich Eckhardt Sep 22 '19 at 10:58

1 Answers1

1

There is probably no performance effect in most languages, so you can just concentrate on clear representation. In most cases it's better to keep inner blocks of code smaller and return earlier, so the preferrable solution depends on the size of "do the stuff" block. If it's one-liner then I'd prefer second example; if not - I'd prefer first example because just returning is one-liner and I don't want to write a lot of code in nested block.

In case both branches of if are one-liners I prefer avoiding unnecessary negation. It doesn't affect performance but slightly affects reading.

Edward Surov
  • 469
  • 4
  • 3