0

I happened to review recursion again and noticed that some people write recursion with and if else statement. The if being the base case and the else having the recursive call. However, I have been only using an if for the base case with no else, and just the recursive call under it. See below.

//using else
    if(n === 0) return 0;
    else if(n === 1) return 1;
    else return fib(n - 1) + fib(n - 2);

//versus no else
    if(n === 0) return 0;
    else if(n === 1) return 1;
    return fib(n - 1) + fib(n - 2);

Logically they are equivalent because of the returns. There is no difference in the result. But is there any reason to do one over the other? I feel like sometimes I flop between them, but I default to not using the else.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
DelSint
  • 13
  • 1
  • See [Unnecessary 'else' after 'return'. (No-else-return)](/q/46875442/4642212) and [no-else-return](//eslint.org/docs/latest/rules/no-else-return). `else` is unnecessary after `return`. This goes hand-in-hand with the [early return style](//softwareengineering.stackexchange.com/q/18454/404722). This has nothing to do with recursion. – Sebastian Simon Jan 21 '23 at 02:24
  • Whichever way you like is fine. Some people are certain that their personal preference is the officially preferred style, but they're just that kind of people. – Matt Timmermans Jan 21 '23 at 02:25
  • 1
    @MattTimmermans - true. My preference is the correct one, though (:troll:) - if you are using `return` in an `if`, don't write `else`. `else` requires more cognitive load (i.e. what was the condition in the `if` that resulted in this `else`?), and it's completely unnecessary if you're returning. My cognition can only handle so much load. Be nice to people like me, and your co-workers, and your future self. FWIW OP, you don't need the `else` before the second `if`, let alone the `else` before the last `return` – Adam Jenkins Jan 21 '23 at 02:28
  • Aside: your Fibonacci function can be shortened to `const fib = (n) => if(n < 1){ return n; } return fib(n - 1) + fib(n - 2);`. – Sebastian Simon Jan 21 '23 at 02:30
  • Keep it going @SebastianSimon, no need for `if`'s at all - `const fib = (n) => n < 1 ? n : fib(n-1) + fib(n-2)` – Adam Jenkins Jan 21 '23 at 02:32
  • Thank you for your response. I assumed that it was more of a preference thing, but I wanted to make sure I was not missing anything. @SebastianSimon I do realize now that it has nothing to do with recursion, I just mentioned it as this is the way that I was informed is the "standard" way of writing a recursive function. – DelSint Jan 21 '23 at 02:36
  • 1
    Does this answer your question? [Unnecessary 'else' after 'return'. (No-else-return)](https://stackoverflow.com/questions/46875442/unnecessary-else-after-return-no-else-return) – Adrian Mole Jan 21 '23 at 13:59

2 Answers2

0

It doesn't matter in this case since each if statement is returning something. This would be a more stylistic approach; if this matters, one would consider using brackets. But it only matters if someone should use an else statement is if one wants to run the code in the else statement ONLY if the if statement(s) are false.

0

This is a purely stylistic question so I will offer a purely stylistic answer.

The general question is, should I write

{
  if (x) {
    y();
    return;
  }
  z();
}

or

{
  if (x) {
    y();
  } else {
    z();
  }
}

Well, what is the purpose of the function? Is z the ultimate purpose of the function, and the condition x and the y basically handling some edge case? Or are y and z two alternative modes, roughly equivalent?

If the first, if z is the “real” code, then you should try x and y as a guard clause — it’s just some uninteresting case to dispose of before getting to the meat of the function; y might even be empty. Use an early return.

In the second case, y and z are symmetric, so they should be treated symmetrically: one should be the “then” and the other the “else”.

Your case is recursion, which is not exactly either of the two cases I mentioned, but the function has two states — the “termination” state, where this is the last step, and the “recursion” state — and they are very different.

Therefore I would write them differently. The termination case would go in an if block, with a triumphant return statement. The rest of the function would be the recursion case.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144