Switches
To put it simply, the switch
statement itself is not problematic per se. In fact, we find it quite elegant and self-explanatory. Many programming languages even offer more advanced forms of switch
statements that can use more complex code as their foundation. They can simplify code and replace ugly nested ifs. I must admit, we have a soft spot for the switch
statement.
However, the issue arises when we encounter repeated switches in object-oriented programming. We consider this to be problematic for several reasons. The switch
statements violate the open-closed principle because every time a developer needs to add a new type, they must insert a new case
statement in each section. This leads to modifying existing code, which goes against the principle. Furthermore, switches can be challenging to maintain. As new requirements emerge, the switch
statements can grow in complexity, making the code harder to manage. Another issue with switches is the potential for redundant code...