Fei Xie
1 min readJun 14, 2019

--

If you are talking about coming with more out-of-the-box way of solving problems than just using if-statements straightforwardly, then yes these are fun little examples. If you are talking about avoiding if-statements as a general coding principle then I cannot agree that these if-less approaches are “better”.

Take the third example. In stead of having a simple function that stratifies operations based on input type with switch-statements, we are creating a second order function and creating an object to store a list of functors. The new code is not less and cleaner than the old one, and I’d argue it is less readable because it requires a little more mental gymnastic. This looks like rewriting without switch statement just for the sake of it.

Better examples will be those that can show by thinking differently, we can come up with cleaner code than straightforward if-statements, perhaps because otherwise there will would very convoluted and complex if-statements, which does happen in real-life.

--

--

No responses yet