Switch is EVIL! (Part 1)

Programming has evolved from early days of hand coded binary and assembly language. Earlier structured (non-procedural) languages like COBOL relied on GOTO statement for control flow. GOTO created “spaghetti” code which made it difficult to debug and maintain the applications

Then came procedural languages like Pascal and C, where the programming style moved away from GOTO, instead using functions/procedures. But for multiway testing, they used switch or if-else-ladder.

Finally we evolved to Object Oriented languages which have polymorphism but we couldn’t get rid of switch. C language has GOTO, doesn’t mean we have to use its. Similarly OO languages have switch, doesn’t mean we have to use it. In fact, most of developers problems can be traced by to heavy use of switch case and if-else ladders.

Some “enlightened” language creators, like Guido van Rossum, probably realised that switch is EVIL (Just like me… great minds think alike, but then fools seldom differ 😂). So now people write if-else ladder in Python and curse the creator for not providing “simple” switch-case.

Programming to me is to write code such that I don’t have to visit it and modify it again

Sanjay Vyas
Here is what I called stupid unnecessary switch-case
function dayName(day) {
var whichDay = null;
switch (day) {
case 1:
whichDay = "Mon";
break;
case 2:
whichDay = "Tue";
break;
case 3:
whichDay = "Wed";
break;
case 4:
whichDay = "Thu";
break;
case 5:
whichDay = "Fri";
break;
case 6:
whichDay = "Sat";
brake;
case 7:
whichDay = "Sun";
break;
}
return whichDay;
}

Why do we write so much code just to choose what I call “value-replacement”? Not to mention that this code works until the day case 6 is called.

Replace "value-replacement" switch with a simple array
function dayName(day) {
// Use an array instead of switch
const dayNames =
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
// Is the day value between 1-7?
if (day < 1 || day > 7)
throw 'Invalid day number';
// Array starts with 0, Mon->1
return dayNames[day1];
}

The above code not only shortens the code but eliminates issues like misspelled break which only show up later when the actual code is called. So whenever you are simply “replacing” a value, STOP writing switch-case and use array instead.

Let’s look at what we gain apart from shortening the code and making it less error prone. We can possibly read the values of the array strings from an external source for different languages.

Here is weekday names in German language

["Mon", "Dien", "Mitt", "Donn", "Freit", "Sams", "Sonn"]
Now let's load the German day names in our code. Later you can change the weekday.json with any language like Hindi, French, Arabic and there will be NO CHANGE in our code. Yay! Maintenance free.
function dayName(day) {
// Load the day names from a file
const dayNames = require('./weekday.json');
// Were there seven names in the array?
if (dayNames.length != 7)
throw 'Error in weekday.json';
// Is day value between 1-7?
if (day < 1 || day > 7)
throw 'Invalid day number';
// Array start with 0, Mon -> 1
return dayNames[day1];
}

This was just one way in which we can eliminate switch-case. There are at least 6 more ways of eliminating switch-case and if-else ladder.

Stay tuned for more Switchzilla (Switch Killer) 👍

Switch is EVIL Series

  1. Switch is EVIL! (Part 1) – Convert it to arrays
  2. Switch is EVIL! (Part 2) – Convert it to maps
  3. Switch is EVIL! (Part 3) – Create function maps
  4. Switch is EVIL! (Part 3a) – Implementing in Java
  5. Switch is EVIL! (Part 3b) – Implementing in C#