I know it probably doesn't matter from a performance standpoint (due to branch prediction and compiler optimization), but I still prefer this form:
public boolean checkNameStartsWith(List<Foo> foos) {
for (Foo foo : foos) {
if (foo.getName().startsWith("bar")) continue;
// Mismatch found
return false;
}
return true;
}
I think it's because it makes it easier to see that you're absolutely done with the iteration once you've determined there's no match. Otherwise, you might have something like this:
public boolean myMethod(List<Foo> foos, final String prefix) {
for (Foo foo : foos) {
if (!foo.getName().startsWith(prefix)) {
// do something complex (many lines of code)
}
// here you could possibly do something else, regardless of condition true/false
}
return true;
}
You don't know unless you search for the end of the loop whether something is still done if the condition is false.