Avoid Else, Return Early (2013)
11–20 of 601 posts
Re: Avoid Else, Return Early (2013)
#12 if (err) {
handleError(err)
return
}
and if (err) return handleError(err)
are equally good. The second one doesn't really make it clear wether handleError returns a value and that value is intended to be returned.Re: Avoid Else, Return Early (2013)
#13GOTO has a bad rap IMO. There's a place for it, and the author should probably be using it.
Re: Avoid Else, Return Early (2013)
#14Avoid rules, use your judgment.
Re: Avoid Else, Return Early (2013)
#15Re: Avoid Else, Return Early (2013)
#16"removing a whole line and more braces" - this is really fighting the wrong enemy. Code should be written in a way it is more readable, not shorter.
Personally I think sometimes shorter code, that gets rid of unnecessary bureaucracy, is more readable. For example, C++ recently (in the past decade...) gained range based iteration.
Previously:
for(containe_type::iterator i = my_container.begin(); i != my_container.end(); i++){ do_stuff(i); }
Now:
for(auto& i : my_container) { do_stuff(i); }
This is really context sensitive. Some code benefits from verbosity while other don't.
Re: Avoid Else, Return Early (2013)
#17With Ruby it's even cleaner because of the postfix conditionals.
def something()
return value2 if error1
return value2 if error2
do_something
end
The return values in case of errors stand out, the conditions for the errors do not clobber the code because they are sidelined.Re: Avoid Else, Return Early (2013)
#18"removing a whole line and more braces" - this is really fighting the wrong enemy. Code should be written in a way it is more readable, not shorter.
Exactly. This led to the famous OSX double goto bug [0] if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail; ... other checks ... fail: ... buffer frees (cleanups) ... return err; [0] https://www.dwheeler.com/essays/apple-goto-fail.html
Re: Avoid Else, Return Early (2013)
#19Re: Avoid Else, Return Early (2013)
#20"removing a whole line and more braces" - this is really fighting the wrong enemy. Code should be written in a way it is more readable, not shorter.
Exactly. This led to the famous OSX double goto bug [0] if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail; ... other checks ... fail: ... buffer frees (cleanups) ... return err; [0] https://www.dwheeler.com/essays/apple-goto-fail.html