re: Language Proposal: The 'Any' Switch Case VIEW POST

FULL DISCUSSION
 

Code is read much more often than it is written, and we understand code best when it executes the way we read it: top to bottom. We get into trouble when the flow jumps around. This is what makes asynchronous code so hard to understand without async/await.

Take this code block for example:

first();
setTimeout(function(){
    third();
}, 0);
second();

Here, the name of the functions I'm calling indicates in what order they're called, and there's this annoying jump.

So I think that even though


switch (a)  {
    case 1:
        doOne();
        doCommon();
        break;
    case 2:
        doTwo();
        doCommon();
        break;
    ...
}

Having this common code in each case might be a little bit longer, but when reading it, it takes a little less overhead. If the common code between cases spans so many lines that it's annoying to update everywhere, maybe you should put it into a function.

 
 

Readability is absolutely, and even I had some concerns about readability before I made the post. While the idea was drawn to me because I was thinking about the amount of cases there may be, so it would be more than a few lines, the particular piece of code that drew me to this, also may be able to be reworked to not use a switch at all and achieve the functionality I was hoping for.

code of conduct - report abuse