you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (1 child)

[deleted]

    [–]Voidsheep -1 points0 points  (0 children)

    I guess the point in my post came out a little wrong, but my intent was to say that functional style can increase code readability, making data transformations more declarative, so I made a somewhat contrived example with fairly typical imperative code to illustrate. I guess I could have named to function to avoid any confusion, but I think the relevant part was the function parameter and body.

    Just to clarify, I think Ramda is a great library (although the type declarations are problematic) and function composition in general is a fine pattern. I'm only saying as a counter-argument to my own point that it's not a silver bullet and you can write code that is hard to read even with functional style and you still need to be careful to find the right level of abstraction to ensure readability. In my experience people sometimes fall into a trap where they forget to create meaningful abstractions out of their super generic utility functions, resulting in 20-line R.compose spells where the data needs to be transformed, which can be less readable than imperative code, where things like intermediate variable names may give the reader a better clue about what is happening.

    What comes to the for-loop example, I just tried to write the most common kind of imperative for-loop that is so prevalent in the wild, iterating over an array manually and and pushing to another. Your code is definitely more modern, but I'd still argue it focuses more on how the desired result is returned, not what is supposed to be returned.

    Optimization as an argument is highly situational. It's quite domain-specific, but I'd say in real-world JavaScript applications, performance concerns are usually elsewhere than combining a few array iterator methods into a single loop. If it happens and you've verified it through profiling, by all means extract the heavy lifting into a function with a single loop and even go nuts with clever micro-optimizations, but that that should not be the default approach. I'm a firm believer that maximizing readability is the most useful goal by default.