Why this matters
Iterating over the same collection multiple times in separate loops is inefficient. Combining them reduces execution time and improves performance.
Ensure that multiple loops over the same collection are combined into a single loop to improve efficiency.
Iterating over the same collection multiple times in separate loops is inefficient. Combining them reduces execution time and improves performance.
Side-by-side examples engineers can pattern-match during review.
public void doSomethingToAList(List<String> strings) {
for (String str : strings) {
doStep1(str);
}
for (String str : strings) { // Noncompliant
doStep2(str);
}
}public void doSomethingToAList(List<String> strings) {
for (String str : strings) {
doStep1(str);
doStep2(str);
}
}public void doSomethingToAList(List<String> strings) {
for (String str : strings) {
doStep1(str);
}
for (String str : strings) { // Noncompliant
doStep2(str);
}
}public void doSomethingToAList(List<String> strings) {
for (String str : strings) {
doStep1(str);
doStep2(str);
}
}From the same buckets as this rule.