5 ms·
On the other hand, look at the validation: - Why not check the length of the string before the switch? - Why return a string which is (meant to be) identical t
by jfrunyon 6y ago
On the other hand, look at the validation:
- Why not check the length of the string before the switch?
- Why return a string which is (meant to be) identical to a variable?
- Why not set the regex into a variable and then only have one if-block?
"Don't repeat yourself" is a very good rule for many very good reasons, but a big one is that you don't have to change 10* different places to change 1 thing, which gets rid of a whole class of extremely common bugs. Also, it makes it much more obvious what the code is doing, since your brain only needs to parse 10 different one-line assignments instead of 10 different six-line blocks.
* Replace "10" with whatever the actual number is.