10/09/2021

QM: Do good programmers use Else?

Coming from a C/C++ legacy codebase written by self-taught engineers, I understand why someone would embrace this notion. You can only look at so many gigantic piles of nested if statements before you go "you know what? I'll just collapse the whole pile by exiting early using return statements." Before long, you'll clear away all these little methods that only branch when their various calls result in a null pointer, and you'll pat yourself on the back for having eliminated all the else statements. Tempting, then, to wonder when else is even necessary.

True, those else statements were redundant. That means very little. All it proves is that like any other tool, else can be misused. That doesn't make it a bad tool, it just means it was used in the wrong place. For short, well-considered routines, if-else is perfectly legitimate.


Notes on the Quora Migration: this piece originally appeared on Quora. Since Quora is no longer what it was, I'm migrating my content here.

No comments:

Post a Comment