Hacker News new | ask | show | jobs
by ramesh31 1777 days ago
The switch case is still a bit redundant and inelegant, particularly considering how out of place switch case syntax feels in a Javascript codebase in general. Early returns are the way to go.
2 comments

The point of the switch-case is to mimic pattern matching and TS is smart enough tell you whether you handled all cases.

Would you prefer that it's a if-elseif-else based structure instead? I guess that would work too but I feel like it could be easier to write it in a way where you accidentally forget to handle some case (since your else / last return is a potential catch-all).

I personally don't mind switch-case because I'm so accustomed to it in ReasonML/ReScript already.

Although I don't mind switch statements, I was thinking similarly about using early returns instead. But this is where switch/match expressions like in Rust or C# would really shine; it's too bad JavaScript/Typescript doesn't support them though.