jargon

Comparison

Arrow anti-patternvsGuard clause

Arrow anti-pattern

the code drifts right for six levels and back again, and the shape of it on screen is a wide arrowhead.

Deeply nested conditionals whose indentation forms an arrow, where the meaningful line is buried at the deepest point and every closing brace is unlabelled. It is a readability problem with a mechanical cure: guard clauses for the failure cases, and extracted methods for the nested blocks that remain. Each level of nesting is roughly another condition a reader must hold in their head, which is why it stops being readable at about three.

Full entry →

Guard clause

the invalid cases return in the first four lines, and the rest of the method is not indented at all.

Handling exceptional or invalid input with an immediate return at the top of a method, so the main path is unnested. It is the standard cure for deeply nested conditionals and makes the preconditions readable as a list. It is the smallest structural change in this corpus and the one most likely to be worth making in code you are reading right now.

Full entry →

Related comparisons