6

Sometimes I like to use early return statements to prevent nesting if statement, which I find makes for less readable code.

I am wondering if there is any objective or overwhelming general consensus as two which of the following patterns is better practice? I don't think this is a subjective question, since what I am really asking is there a near objective preference.

void func() {
    if (a) {
        do b
    }    
    else {
        do c
    }
}

or

void func() {
    if (a) {
        do b
        return;
    }

    do c
}
Scorb
  • 1,654
  • 13
  • 70
  • 144
  • I've always been under the assumption that return statements, where possible, should be limited to being used at the end of the function because it's easier to read and comprehend the flow. Others may disagree with that, but that's why I'd opt for the first snippet. – b85411 Oct 13 '16 at 01:58
  • I think it quite similar with this http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – hakim Oct 13 '16 at 02:00
  • Duplicate of https://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement The question on softwareengineering has higher quality answers. – Valentin Jul 14 '21 at 11:08

3 Answers3

7

Frankly, I recommend the second one.

    1. The second is more clear to understand
    1. When some else modify the code more easy to understand is the first place. Maybe the first is more clear in math but not in human being.
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
lisprez
  • 71
  • 1
  • 6
0

I would opt for the first version. I was actually given a lengthy explanation several years ago regarding this.

The two examples, as your wrote them now, are functionally identical. If the a condition be true, then the logic in the first if condition will execute, and the function will return. However, have a closer look at the second scenario:

void func() {
    if (a) {
        do b
        return;
    }

    do c
}

Right now, should the first if fire, the function would return, otherwise c would execute. However, consider that at some point down the line a programmer decides to refactor the method for some reason. If he were to take out the return statement, then the logic for c would also execute if a were true. This may seem far-fetched, but it could happen more easily than you might think. On the other hand, if you use a full if-else, then even a refactor of the if condition would never result in the c logic evaluating at the same time.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

The first is better. Simply put,it helps another developer to understand that c compiles because the condition is false. It also prevents other people from making damaging changes to your code. That said,they are both correct and would both work just fine

Nyakiba
  • 862
  • 8
  • 18
  • 5
    I disagree, this first option is definitely bad. https://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement – Marcus Ekström Oct 13 '19 at 15:57