Instead of writing for or while loops, you should use functions like map, filter, and reduce. This is recommended because: Loops make it easy to create side effects in your code, which can turn your code into a mess of spaghetti. When you try to do more than one thing at a time, loops can cause you pain. Functional programming prevents your code from having as many side effects as cold medicine. It forces you to do one thing at a time and is more readable than loops. Loops, like pointers, are a simple programming technique. They are very useful for certain critical code, but I would say that few of us are working on graphics card drivers. Let's cut to the chase - here's a comparison of JavaScript programs. First, the old-school loop version:
This is the new functional programming:
Granted, this is a simple example, and is likely a perfect use case for a filter. Still, we’ve turned 5 messy lines of code into… 0 lines? In the first example, we’ve used one line of code to declare the array. The fun doesn’t end here: we used filter as an example, but there are many more usages like map, reduce, sort and flatten. They give us a huge space to write creative, elegant and most importantly, easy to understand code! What do you think? Maybe my examples are biased, or maybe I'm just plain wrong. Let me know in the comments - I appreciate any and all feedback! |
<<: Pitfalls you may encounter when using Android notifications
>>: A collision of ideas caused by code review
The most primitive trading method of human beings...
The first quarter of 2017 is drawing to a close. ...
Compared to last year's WWDC 2020, which rele...
If I remember correctly, Apple Music service was o...
The following is from Xhacker's Base: I am a ...
Where can I find cheap and delicious mooncakes wh...
If 2021 is the first year of brand live streaming...
Course Outline 1. The traffic-generating advantag...
Audit expert: Yang Yanhui Senior Landscape Engine...
I have sorted out the usage of various pop-up win...
Introduction: The success of gamification marketi...
Produced by: Science Popularization China Author:...
Introduction: As an important festival IP, it is ...
On a media platform, there will be tens of thousa...
According to foreign media VentureBeat, Microsoft...