Skip to content

Comment on Stop Writing If-Else Trees: Use the State Pattern Instead

Comments

Turning if statements into a collection of classes requires a bit more thoughts than "Stop Writing If-Else Trees"

Sometimes the State pattern transforms "one big if-else tree" into "a forest of tiny classes" with its own complexity tax. Good design isn't about eliminating conditionals — it's about putting them where they make the most sense.

The real skill is knowing when your state transitions are complex enough to warrant the abstraction overhead.

I sort of agree. I think that rather than develop that skill, many programmers just don’t care and add another “if”. The default I’ve seen is not a forest of classes but a warren of conditional logic.

Conditional logic falls into 3 categories, more or less:

1. Algorithms 2. Validation 3. Type replacements

It’s the third that is most common, and always “wrong”. Sometimes it’s better to keep conditional logic rather than try and figure out the correct type modeling. But most often, the programmer reaching for “if” isn’t even aware of what they are doing. They have a hammer, and everything looks like a nail.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.