Here is the code in question, I can either check for the condition once outside:
if (booleanCondition) {
for (Integer x : integerList) {
doSomething(x);
doSomethingElse(x);
}
} else {
for (Integer x : integerList) {
doSomething(x);
}
}
Or inside:
for (Integer x : integerList) {
doSomething(x);
if (booleanCondition) {
doSomethingElse(x);
}
}
The first code sample is (imo) less readable and longer. The second one is concise and easy to understand, yet inefficiently checks repeatedly for the condition.
I'm not sure if the compiler is smart enough to be able to reduce the second example to the first (I'm using java 8, javac 1.8.0_45), what do you think would be the correct way to write this then?
Aucun commentaire:
Enregistrer un commentaire