32 lines
633 B
Plaintext
32 lines
633 B
Plaintext
When a method loops multiple over the same set of data, whether it's a list or a set of numbers, it is highly likely that the method could be made more efficient by combining the loops into a single set of iterations.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,text]
|
|
----
|
|
public void doSomethingToAList(List<String> strings) {
|
|
for (String str : strings) {
|
|
doStep1(str);
|
|
}
|
|
for (String str : strings) { // Noncompliant
|
|
doStep2(str);
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,text]
|
|
----
|
|
public void doSomethingToAList(List<String> strings) {
|
|
for (String str : strings) {
|
|
doStep1(str);
|
|
doStep2(str);
|
|
}
|
|
}
|
|
----
|
|
|
|
|