For my work I have to develop a small Java application that parses very large XML files (~300k lines) to select very specific data (using Pattern
), so I'm trying to optimize it a little. I was wondering what was better between these 2 snippets:
if (boolean_condition && matcher.find(string)) {
...
}
OR
if (boolean_condition) {
if (matcher.find(string)) {
...
}
}
Other details:
boolean_condition
is a boolean
calculated on each iteration using an external functionboolean
is set to false
, I don't need to test the regular expression for matchesThanks for your help.
One golden rule I follow is to "Avoid Nesting" as much as I can. But if it is at the cost of making my single if condition too complex, I don't mind nesting it out.
Besides you're using the short-circuit &&
operator. So if the boolean is false, it won't even try matching!
So,
if (boolean_condition && matcher.find(string)) {
...
}
is the way to go!