Home United States USA — software Revisiting Switch and If-then-else

Revisiting Switch and If-then-else

127
0
SHARE

Learn about temporal logic and how switch and if-then-else statements tend to be overused in code.
Join the DZone community and get the full member experience. In computer science, there is nothing more fundamental and intuitive than control structures. Every student learned about them in the first few weeks of any computer science program. We could not code without them, period. But they are not set in stone: we did get rid of the infamous GOTO in the 80s! At the pre-ALGOL meeting held in 1959 Heinz Zemanek explicitly threw doubt on the necessity for GOTO statements; at the time no one paid attention to his remark, including Edsger W. Dijkstra, who later became the iconic opponent of GOTO. [3] The 1970s and 1980s saw a decline in the use of GOTO statements in favor of the ” structured programming ” paradigm, with goto criticized as leading to “unmaintainable spaghetti code ” And this paper is far from being the first call for using less conditional flows in programming. Even Uncle Bob has tackled the topic. In npm alone, countless packages have been published to provide an alternative. People tend to think that the catch with switch and if-then-else is that the more branches in your code, the more opportunities for untested and unexpected behavior. Every branch requires different use cases to be tested thoroughly, and even though, some tools, such as Istanbul, provide some indication as to how many branches have been executed, in addition to the traditional function coverages. But branches are part of the nature of programming, there is nothing we can do about it. In this short opinion brief, I would like to give a new spin on when to use control structures and when to use some functional alternatives.

Continue reading...