9

In the following example - given that the return value isn't of any importance - is there a reason to prefer either method over the other?

// Method 1
function (a, b) {
  if (a == b){
    // I'm just interested in
    // the stuff happening here
  } else {
    // or here
  }
return true;
}

// Method 2
function (a, b) {
  if (a == b){
    // I'm just interested in
    // the stuff happening here
    return true;
  }
  // or here
  return true;
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
knex
  • 1,733
  • 3
  • 15
  • 11
  • 1
    You may want to use `==` instead of `=` since you're comparing, not setting values. ;) Also a simple `return a == b;` would sometimes do the trick. – Wabbitseason Aug 03 '11 at 18:08
  • Just out of curiosity, why would you write a method that always return true or the return value has no significance? Aren't there any edge cases? – Kumar Aug 03 '11 at 18:09
  • 1
    If the return value has no importance, then why do explicit returns at all? Just let the code "fall through". – Marc B Aug 03 '11 at 18:10
  • 1
    `a === b` is preferred to `a == b` to prevent type coercion – Eric Fortis Aug 03 '11 at 18:25
  • 1
    I just wanted to make clear that I really don't care about the outcome and it is like it was always the same. – knex Aug 11 '11 at 08:37

6 Answers6

5

It seems that best practices (mostly by places I've worked for) is to set default values at the top of a method or function and only change those values if some condition occurs. Thus, the use of else is not needed so Method 2 is preferred.

Since the example is JavaScript, special attention needs to be paid in regards to code size. So Method 2 would create less code for the same functionality, furthering its argument as the preferred.

However, if you have more than 2 possible conditions, an else or else if cannot be avoided. However, most places I've worked prefer a Switch Case in these situations.

Todd Moses
  • 10,969
  • 10
  • 47
  • 65
4

I would prefer Method 1 because it is less confusing to read. Also, less duplicate code.

hspain
  • 17,528
  • 5
  • 19
  • 31
  • It's really a style thing, one is more declarative (if else), the other is briefer. I prefer the second one better myself as well. – orkutWasNotSoBad Aug 03 '11 at 18:09
  • I disagree. Having multiple return statements makes code much harder to follow (think recursion!) especially when it can be avoided. In this case, we always return true! – adu Aug 03 '11 at 18:14
  • You are right, I meant Method 1 – hspain Aug 03 '11 at 18:22
3

I would base my decision on clarity of code and readability, i.e.:

  • Choose method 1 when you need to do more stuff in the block after the if-block.
  • Choose method 2 when you only need two blocks of code, it's then clearer to read
  • Choose method 1 again in cases where you explicitly think your readers wouldn't understand your cryptic code without the word "else"; this is common when the blocks become larger than a few lines.

Many of today's programmers consider less indentation easier to read and I agree. In which case general preference should go to using the second method.

Abel
  • 56,041
  • 24
  • 146
  • 247
1

I would recommend method 1 as it is more readable and self documented.

Samir Adel
  • 2,691
  • 2
  • 15
  • 16
  • 1
    Disagree that it's more readable. More nesting generally makes code less readable and is considered bad practice. Accordingly, leaving out the else condition is better practice I think. – Jeff Aug 03 '11 at 18:15
  • But if you want to edit in his code, would you be able to understand the business logic easily ?! – Samir Adel Aug 03 '11 at 18:19
  • Reading multiple levels of nested if/elses is harder to understand business logic, IMHO. – Jeff Aug 03 '11 at 18:45
  • I totally appreciate your opinion but may be because while i am developing i use if else as my option so i am used to understand it faster. – Samir Adel Aug 03 '11 at 18:47
  • Everyone's got different tastes :) – Jeff Aug 03 '11 at 19:01
0

Readability here really depends on the role of the function.

If this function will ALWAYS return true, then I would prefer the Method 1 It is clear because it only returns in one place, and it is easy to see it will always be true.

In the above case, Method 2 is more confusing. It returns in multiple places, and is more confusing thusly. Consider a developer unnecessarily traversing possible branches and then seeing how they affect the return value. In this simple case, it is not as big of a deal, but when you get more elaborate conditionals, I would really avoid this approach.

I would only use Method 2 if you have very little code in the if block. Such as something that would deal with an edge case.

Hope that helps.

adu
  • 947
  • 1
  • 8
  • 15
0

Any modern browser's interpreter should eliminate any performance advantage in either direction.

There are a couple of reasons method 1 is preferable that haven't been mentioned yet. Having a single point of exit makes any future modifications that require an action common to both branches easier and less likely to be buggy (because the author missed the early return. Similarly, in some cases it makes debugging easier, by providing a common place to put a breakpoint or alert().

Ed Staub
  • 15,480
  • 3
  • 61
  • 91