Michele Riva
2 min readApr 26, 2019

--

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:

  1. I won’t increment i using i++. I’d prefer to declare a new constant like const newIndex = i + 1;. Why? When you use i++, you’re creating a side-effect and you won’t be able to get your initial i value back. In fact, you’re now returning msg || i where i is an incremented version of the original i 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.
  2. 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!

--

--

Responses (1)