Posts

Showing posts from February, 2018

Strategy Pattern: Switch Statements, Begone!

Image
Strategy Pattern: Switch Statements, Begone! Switch statements seem like a pretty convenient tool for handling control flow, and it certainly has its places. In my experience, however, switch statements (more often than not) end up leading to code smells. They gradually become parts of your code base that developers actively avoid. Why is that? switch (switchVar) { case "1": // A ton of code case "2": // Some different code break; case "3": // Some more code break; case "4": // This method is getting long break; case "5": // More code break; . . . case "100": // Heeelp break; } This is because switch statements make it very easy to extend functionality in an un-maintainable way. Whenever a new business case comes up, another switch case can be added to this already long function. As time