2-Minute Coding Tip: Don't Use Loops in Your Code

2-Minute Coding Tip: Don't Use Loops in Your Code

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:

  1. const cats = [ 'Antonio' ​​, 'Squid' , 'Tornado' , 'Avocado' , 'Barnacles' , 'Abroteus' ];  
  2. const stringStartsWithA = x => x[0].toLowerCase() === 'a' ;  
  3. const catsWhoseNameStartsWithA = [];  
  4. for (let i = 0; i < cats.length; i++) {
  5. if (stringStartsWithA(cats[i])) {
  6. catsWhoseNameStartsWithA.push(cats[i]);
  7. }
  8. }  
  9. console.log(catsWhoseNameStartsWithA);  
  10. // Output :
  11. // [ "Antonio" , "Avocado" , "Abroteus" ]

This is the new functional programming:

  1. const cats = [ 'Antonio' ​​, 'Squid' , 'Tornado' , 'Avocado' , 'Barnacles' , 'Abroteus' ];  
  2. const stringStartsWithA = x => x[0].toLowerCase() === 'a' ;  
  3. const catsWhoseNameStartsWithA = cats.filter(stringStartsWithA);  
  4. console.log(catsWhoseNameStartsWithA);  
  5. // Output :
  6. // [ "Antonio" , "Avocado" , "Abroteus" ]

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

Recommend

The relationship between hackers and the open source revolution

This article corrects the confusion between "...

2022 National Trend Marketing Rules!

Although the issue of national trends is always m...

Flink in-depth deployment of advanced development and case practice

Introduction to Flink in-depth deployment of adva...

Android View Focus Summary

Android View focus Most of the focus-related logi...

Never try to fight a kangaroo!

Who is the most popular animal in the world? With...

Analysis of short video operation methods

Accounts related to agriculture, rural areas and ...

Analysis of customer acquisition from 0 to 1

Magical 2020, all the anxiety is generated on &qu...

What is it like to go shopping at 2,774 meters below the sea?

She once rode a "Jiaolong" into the oce...

Tmall Genie Lyrics Speaker, let you truly fall in love with music

In the busy city life, how long has it been since...

Huawei AppGallery Paid Promotion Audience Targeting Function Operation Guide

Huawei AppGallery Paid Promotion Audience Targeti...

How to use video marketing to let customers fully understand your products?

A marketing activity that uses video as the main ...