3 ms·
Semi-serious: switch cases are only complicated when you're using compilers that can't check totality for you.
by KirinDave 8y ago
Semi-serious: switch cases are only complicated when you're using compilers that can't check totality for you.
- Jtsummers 8y agoI'll mostly agree with that. If you have a compiler that can check totality (I've been playing with Idris lately at home, that feature is fantastic), then the work of adjusting your switch-cases becomes much easier. But there's still a lot of work relative to, for instance, creating a type class (or in Idris terms an interface) and implementing the functions of that type class/interface for your specific data type. Similarly, in OO-languages with interfaces, you can apply this same approach. It doesn't eliminate the conditional, it still exists in the code. But it moves where you have to think about it and reduces the redundancy across your code base. With these things you don't even have to modify every function when you add a new value to your equivalent to an enum, you just add the implementations for that new type/class. Though it does shift the difficulty when you want to add a new function/method to the interface. Now you will have to go to each implementation and add this new capability. But the nice thing, here, is that most languages with static typing (whether fancy dependently typed languages like Idris, or relatively simpler ones like Java) will make it immediately clear which implementations are incomplete.