Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 53 — Explicit Iteration

Maximiliano Contieri
Dev Genius
Published in
2 min readJan 5, 2021
Photo by Elena Mozhvilo on Unsplash

Problems

  • Encapsulation
  • Declarativeness

Solutions

  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

Wrong

Right

Detection

Linters can find this smell using regex.

There might be false positives. See exceptions below.

Exceptions

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.

Tags

  • Declarative

Conclusion

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.

Relations

More info

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

David Walker

Original twitter thread

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet

Write a response