TBH I've never considered doing this but now that I see it, it feels like an abuse of the switch statement.
Neither example seems particularly "clean" to me.
My preference would be to refactor this code such that you can return early.
function getLogMessage() { if (someExpressionA) { return 'yes'; } if (someExpressionB && someExpressionC || someExpressionD) { return 'nope'; } return 'maybe'; } console.log(getLogMessage())
Much more straightforward than either IMHO.
I agree, the console.log was purely for the example. I also think most people will agree with you, my first feeling was also that it feels hacky. But... There are people that do use it, so it is good to know it exists 🙂
Which can, in turn, be simplified by Java 13's Enhanced Switch statement (or even a ternary statement at that point).
console.log(switch(true) { case someExpressionA -> "yes"; case someExpressionB && someExpressionC || someExpressionD -> "nope"; default -> "maybe"; });
Thnx for sharing! Honored to be your first comment 😊😉
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
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.
TBH I've never considered doing this but now that I see it, it feels like an abuse of the switch statement.
Neither example seems particularly "clean" to me.
My preference would be to refactor this code such that you can return early.
Much more straightforward than either IMHO.
I agree, the console.log was purely for the example. I also think most people will agree with you, my first feeling was also that it feels hacky. But... There are people that do use it, so it is good to know it exists 🙂
Which can, in turn, be simplified by Java 13's Enhanced Switch statement (or even a ternary statement at that point).
Thnx for sharing! Honored to be your first comment 😊😉