If I’m understanding correctly, languages like rust avoid that problem by throwing a compile error when a match statement doesn’t cover all possible cases.
Of course, a catch-all would then somewhat reintroduce the problem.
Re:When are enums not a code smell?
by aardvark179 on Monday November 22, 2021 @04:14PM
(#29311640)
Yes. The problem you then have is that adding additional values to the enum then either breaks compatibility or relaxes that guarantee. Since Java tries to run old code for as long as possible the only option for switch statements is to ensure there is a fallback so that you at least know there is a problem.
I think in a lot of cases it’s definitely worth stopping and thinking, and making sure you are implementing the right thing if you find yourself using enums heavily, or using a class hierarchy as an enumerator replacement.