3 ms·
On the positive side, it's straightforward and is void of cleverness (yes I've been doing Go, can you tell?). I mean if I were to change that option string gen
by Cthulhu_ 6y ago
On the positive side, it's straightforward and is void of cleverness (yes I've been doing Go, can you tell?).
I mean if I were to change that option string generator, I could make a numeric for loop from 1 to 12, left-pad a zero for the value, and lookup the month label from a label array or create a Date object and use its formatting options to output the label. I loop over an array of objects with a value and label. With ES6 I could do that with a .map() operation.
It's inelegant but it does what it says on the tin. Can't have an off-by-one error if you don't use loops or indices.
- deleted 6y ago[deleted]
- deepstack 6y agoFor sure. Using code or data structure to generate code is recent (or resurrection ) phenomenon of function programming. The function is formatted and it does what it suppose to do. It can be refactored to make it less code.
- jfrunyon 6y ago> Using code or data structure to generate code is recent (or resurrection ) phenomenon of function programming. I'm not entirely sure how you came to that conclusion. Lookup tables and format strings have been around for... well, I'd guess almost a century.
- jfrunyon 6y agoOn 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.