DEV Community

Discussion on: I've never become overly convinced that switch statements are that much cleaner than `if else if else if else if else`

Collapse
 
powerc9000 profile image
Clay Murray • Edited

I like switch for when I return because you skip out on so much boilerplate over and over.

switch(something){
case "this":
    return 1;
case "that":
    return 2;
default:
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Just feels much easier to type than

if(something === "this"){
    return 1;
} 
if (something === "that"){
    return 2;
}

return 0;
Enter fullscreen mode Exit fullscreen mode

You have to keep typing something === over and over and I dislike that.

The speed etc is not important to me.

Collapse
 
cubiclebuddha profile image
Cubicle Buddha

I often see that developers enjoy that approach until they run into a nasty bug down the line. I describe a bug like that here and I’d love to get your perspective on if my defensive coding approach I describe would be beneficial to you.