Skip to content

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

Comments

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.