Skip to main content

Isolate Complexity

When you are faced with some inherent complexity, no effort[1] will make it go away. The best thing you can do is prevent the complexity from contaminating surrounding code. You do this by isolating it, drawing a box of abstraction around it, with a clean, simple API — the one you wish the problem had in the first place, if complexity wasn’t a problem. And then you deal with all the complexity inside the box. In there you should write some good comments, explaining the inherent complexity in clear, easy to read prose. This is not breaking the no comments rule, and is necessary because it is highly unlikely that you can encode all the intent in just variable and function names — if that were possible, the level of complexity would have been low enough that you wouldn’t be using isolation as a solution[2].

Next: Avoid Nested Branching ⇒

[1] Except for avoiding the problem (are you solving the right problem?)

[2] You could be using isolation for other reasons, e.g. for clean organization of code, but that’s somewhat different (though there is certainly overlap) from using it as a means of hiding complexity.

Comments