Hi Andreas Grafen and thank you for your response!
Well, I have to say: using .map instead of loops is just about code maintainability, beauty and absolutely not about performances. If you’re writing a high-performance app, maybe it’s not the perfect solution! But of course, it is worth no know loops alternatives!
So, about the switch/case: I just don’t like if/else statements. I mean, they are a necessary evil and we all can not work without them, but switch/case sometimes feels like some kind of “pattern matching”.
Coming from other great languages such as Haskell, you will miss pattern matching a lot! I strongly believe that large if/else if/else declarations are poorly maintainable.
About your code… it works! And works well!
I’d just change a couple of things:
- I won’t increment
i
usingi++
. I’d prefer to declare a new constant likeconst newIndex = i + 1;
. Why? When you usei++
, you’re creating a side-effect and you won’t be able to get your initiali
value back. In fact, you’re now returningmsg || i
wherei
is an incremented version of the originali
variable. What does it mean? Poorly maintainable code, harder to test and adds some kind of local state to your piece of code. You can read more about immutability advantages here. - Same for
msg
. You’ve coded a great function, don’t get me wrong! But once again, I’d prefer to code an immutable piece of code for the reasons above!
By the way, I still believe that your example code is good. At some point it’s all about personal preferences!