DEV Community

Cover image for Code Smell 111 - Modifying Collections While Traversing
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

4 1

Code Smell 111 - Modifying Collections While Traversing

Changing a collection while traversing might lead to unexpected errors

TL;DR: Do not modify collections while traversing them

Problems

  • Unexpected Results

  • Concurrency problems

Solutions

  1. Avoid altering the collections

  2. Make collection copies

Context

We over-optimize our solutions with the prejudice that copying collections is expensive.

This is not true for small and medium-size collections.

Languages iterate collections in many different ways.

Modifying them is generally not safe.

Sample Code

Wrong

Collection<Integer> people = new ArrayList<>();
//here we add elements to the collection...

for (Object person : people) {
    if (condition(person)) {
        people.remove(person);
    }
}
//We iterate AND remove elements 
Enter fullscreen mode Exit fullscreen mode

Right

Collection<Integer> people = new ArrayList<>();
//here we add elements to the collection...

List<Object> iterationPeople = ImmutableList.copyOf(people);

for (Object person : iterationPeople) {
    if (condition(person)) {
        people.remove(person);
    }
}
//We iterate a copy and remove from original

coll.removeIf(currentIndex -> currentIndex == 5);
//Or use language tools (if available)
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi Automatic

Many languages provide control both in compile and run time-

Tags

  • Fail Fast

Conclusion

This is something we learn in our first courses.

It happens a lot in the industry and real-world software

Relations

More Info

Credits

Photo by Christine Roy on Unsplash


Bugs are bugs. You write code with bugs because you do. If it’s a safe language in the sense of run-time safe, the operating system crashes instead of doing a buffer overflow in a way that’s exploitable.

Ken Thompson


This article is part of the CodeSmell Series.

Redis image

Short-term memory for faster
AI agents 🤖💨

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay