DEV Community

Discussion on: If/else or just if?

Collapse
 
danielw profile image
Daniel Waller (he/him)

As long as every branch returns then the 2nd version is fine with me.
(Although I'd still use brackets instead of guard clauses because of personal preference regarding readability)

It becomes a problem when the if does not return and gets more complex than a simple one liner.
I.e.

if (env.DEBUG) {
    Logger.log(/* message */);
}
// Rest of logic here

would obviously be okay, but


let var1;
let var2;
let var3;

if (/* Some condition */) {
    // Many lines
    // of code
    // that affect 
    // the state of 
    // the variables
}

// Rest of logic
// working on the 
// variables unconditionally,
// here

return result;

would not be okay, because it makes it really hard to reason about the output of the function and the logical flow of your data can't be parsed at a glance.