Which of the following code snippets (A and B below) is more efficient (performance wise) and why?
Furthermore:
Does the compiler rearrange the
validate()
method calls so that they are called "when needed" i.e. when evaluating each separate condition in the if-statement? In other words, will the compiler treat snippet A just as if it were written as snippet B?Are there other alternatives (besides snippet A and B) for better perfomance?
(validate(...)
checks if a field (provided as an argument) contains valid values)
Snippet A:
boolean okTitle = validate(inputTitle);
boolean okLocation = validate(inputLocation);
boolean okDuration = validate(inputDuration);
boolean okReminder = validate(inputReminder);
boolean okRepetition = validate(inputRepetition);
boolean okDate = validate(inputDate);
if(okTitle && (okLocation || okDuration || okReminder || okRepetition || okDate)){
setEnabled(true);
}else{
setEnabled(false);
}
Snippet B:
setEnabled(
validate(inputTitle) &&
( validate(inputLocation) || validate(inputDuration) || validate(inputReminder) || validate(inputRepetition) || validate(inputDate) )
);