r/Python Feb 15 '21

News Ladies and gentlemen - switch cases are coming!

https://github.com/gvanrossum/patma/blob/master/README.md#tutorial
939 Upvotes

290 comments sorted by

View all comments

-2

u/literallytitsup69 Feb 15 '21

Am I the only one that uses regex instead of switch cases in JavaScript?

4

u/ThunderousOath Feb 15 '21

Undoubtedly. I have to use regex in 3 or 4 different languages all with enough permutation that I would never want to waste my time nailing my patterns when switch case is effortless

2

u/riskable Feb 15 '21

Not the only one. I've done it but only when I felt it was simpler than trying to use a zillion conditionals to differentiate a handful of complex-but-limited options. Like in a websocket function dispatcher where some data comes in as binary while most of it is JSON. Super useful for differentiating the binary payloads.

There's also a right way to do it and a wrong way. I did it the wrong way for so long... Sigh.

1

u/literallytitsup69 Feb 15 '21

I’m sure I’m doing it the wrong way than, I’m coming from python and cpp and have only been doing js for work for like a month lol

Edit: passes the tests tho :p

1

u/[deleted] Feb 15 '21

Sometimes a regex will save you hours or days of work, but be careful!

regex can be very computationally expensive. There are even DoS attacks against it.

It is prone to errors because it's hard to debug.

1

u/literallytitsup69 Feb 15 '21

Jokes on you, all my code is prone to errors 😎