Today I Learned: The term “arrowhead anti-pattern” from Jeremy Miller’s tweet. (If you’re interested in software craftsmanship and you’re not following him, change that.)
The “arrowhead” describes the visual pattern made by a nasty set of nested conditionals:
if if if if do something endif endif endif endif
The above snippet was lifted straight from the great article on the C2 wiki. If you want to take the visual aspect a step further, go see the Daily WTF’s article Coding Like the Tour de France.
Nested conditionals are awful. Avoid them. Read up on cyclomatic complexity and learn to handle things differently in your code. You’ll be happy you did. (See Chris Missal’s article on Los Techies for some other good discussion.)
I’ve long known the troubles this sort of code causes. I just didn’t know the cool name for it.
Learn something every day…
I call this "sawtooth code" in my refactoring presentations. I didn't coin "sawtooth", but I can't remember for the life of me who told me that term.
ReplyDeleteSawtooth is maybe worse though, because it's like several arrowhead blocks in succession.