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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
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
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.