The prototype methods map, filter, reduce are useful because they avoid side effects and mutation by returning a new instance of the array, but they're also composable (or chainable in this chase), arguably their biggest benefit. You may have already noticed this in the previous examples, where loops inside functions were replaced by one-line methods.Īccessor methods are handy for modifying an array, and iteration methods replace most if not all functionality provided by for loops without touching the original array.Īll of these are tools that serve different purposes. By explicitly calling array instance methods, iteration can be performed in something like a self-enclosed scope. Many array methods are higher-order-functions - they take in a callback function as one of their parameters. Using array methods cut them down into a single beautiful line with an immediately recognisable purpose. As you can see, these are ugly, long, and you actually have to spend time reading through the entire thing to figure out what's going on. Array methods bring us closer to that ideal, they are almost descriptive enough to read as some sort of "shorthand natural language".Ĭonst checkEveryValueGreaterThanForty = myArray => In ideal circumstances, good code should be self-evident and self-explanatory. Let's recap the reasons against for loops and how these methods solve them. Again, this is at odds with functional programming principles.Īs of ES6, Array.prototype methods have been introduced that make for loops obsolete in most ways. By themselves, for loops are kinda useless, which means you have to declare a variable to mutate, or mutate the original array. A lot of the time, this is also unavoidable because all for loops can really do is facilitate iteration. Thirdly, for the exact same reason as above, for loops are prone to requiring mutation in some form. Unfortunately, this clashes with more modern programming paradigms in functional programming, specifically precluding anything involving for loops from being a pure function. This inherently increases the chance you'll end up manipulating variables outside of the loop's scope. for loops-and while loops- are what's known as control statements, meaning they must be placed inside of a function and can not be used as standalones. Secondly, for loops increase the chance of unwanted side effects. And while comments are an expected courtesy, there's a reason why bad documentation is such a frequent joke amongst devs. In complex algorithms, they can quickly become enigmatic, nigh unreadable save for commenting. Let's face it, for loops just don't make the prettiest blocks of code, and only increase in ugliness the larger they get. While they are quite versatile, they suffer many flaws. Here's why you shouldn't and what you should use instead.įor loops and their usage are largely a holdover from imperative languages, in particular C-likes and their derivatives. You yourself may even be using for loops on a regularly basis. It is current year new Date().getFullYear(), and still I still see many JavaScript devs using and even recommending the use of for loops.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |