Code Smell 53 - Explicit Iteration

Code Smell 53 - Explicit Iteration

Maxi Contieri
·Jan 5, 2021·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

We learned loops back in school. But enumerators and iterators are the next generation.

TL;DR: Don't use indices while iterating. Prefer Higher level collections.


  • Encapsulation

  • Declarativeness


  1. Favor foreach() or high order iterators

  2. You will be able to use yield(), caches, proxies, lazy loading and much more when you hide your implementation details.

Sample Code


for (i = 0; i < colors.count(), i++) {
foreach (color of colors) {

//Closures and arrow functions
colors.foreach(color => print(color));


Linters can find this smell using regex.

There might be false positives. See exceptions below.


If the problem domain needs the elements to be bijected to natural numbers like indices, the first solution is adequate.

Remember all time to find real world analogies.


  • Declarative


This kind of smell do not ring the bell to many developers because they think this is a subtlety.

Clean code is full of this few declarative things that can make a difference.


More info


Photo by Elena Mozhvilo on Unsplash

If you get tired of writing for loops, take a break and continue later.

David Walker

Original twitter thread by Matt Moll

This article is part of the CodeSmell Series.

Last update: 2021/06/13

Share this


Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.