I am almost always dismayed when I see a method with too many if statements. More often than not, this is a precursor to similar conditions in other methods in the same or other classes, using the same condition to switch the actions to be called. This is an anti pattern I’ve seen repeated at far too many places. I think the anti if campaign mirrors my thought process about what needs to be done. Continue reading
This follows my post on unnecessary if statements, and broaches a bigger evil. As Uncle Bob says at the end of this article: “Boolean arguments loudly declare that the function does more than one thing. They are confusing and should be eliminated.” Same verdict should apply for an enum as a method parameter.
One quality I like in code is easy/quick grokability – from the work grok – to understand thoroughly and intuitively. That is usually enhanced if the code consists of well spaced and familiar lines of code with minimal interruptions – like if/else statements that need their own branches in the method.Every one of these interruptions break the reading of the code and the brain has to make note and focus to see if there’s anything worth noting in the branch inside a method. What can help in structuring code better for grokability are monads. Continue reading