I really dislike the term "code smell". Nothing wrong with the concept, but it tends to be used to make half-baked, patronizing remarks about other people's code. And when you try to argue there's nothing wrong with a particular piece of code, you get hit back with "it's bad and you just don't know it, and I can't explain it but it just is".
If you can't verbalize your reasons, it doesn't belong in a code review -- otherwise it's preference. And that's where people get confused about what a "code smell" is really supposed to be.
The article does not suggest an alternative. If I consider any typical real-life case where I would use else, trying to finagle the code into not needing else would make it smell considerably more.
If you think of programs as logic then I see the lines above as a logical expression written in CNF conjunctive normal form, where each line is a clause.
whereas this...
$x = condition ? b : a;
...is like a nested logical expression.
And breaking the program down into clauses makes it easier to understand, for humans and computers.
> Use "if (condition()) { ifBlock(); } if (!condition()) { elseBlock(); }" -- assuming that ifBlock() can't possibly change the result of condition().
What?? That seems terrible to me - just asking for `condition` to be updated to be modifiable by `ifBlock`, or for some completely undebuggable race condition to occur.
My team has an ongoing style war about guard clauses:
if foo:
bar()
baz()
vs
if foo:
bar()
else:
baz()
Consensus seems to favor the first option but I prefer the second because it helps me keep in mind the conditions that lead me to `baz()`. Otherwise I have to scan up the whole function to figure out the restriction `!foo`.
It’s just an abstraction for the idea of “this pattern tends to signal a problem”. The same way humans have learned that certain smells signal spoiled meat or rotten eggs. Sometimes those smells can safely be ignored, like when eating Limburger cheese or durian fruit.
Code smells themselves aren’t problems, they’re just useful track marks that might indicate a problem is lurking nearby. Worth looking into and making the decision yourself.
Poor patterns elicit physical repugnance in some individuals, so the term is only more correct in those cases.
The term “smell” over something more concrete like “rotten” is quite perfect as what smells for me might not smell for you; whereas if something is rotten there’s little dispute.
Additionally, a code smell can describe code that feels wrong, but you can’t quite put your finger on it. Similar to catching a whiff of a new smell when out. You can’t quite discern where it came from, but it’s there.