Which is better: a chain of OR statements or IF in a loop? (Java)
Which code style is preferred in Java? final boolean result = isCausedBy(e, ExceptionType1.class) || isCausedBy(e, ExceptionType2.class) || isCausedBy(e, ExceptionType3.class) || isCausedBy(e, ExceptionType4.class) || isCausedBy(e, ExceptionType5.class); log.debug("Error [{}] classified as {}", e.getMessage(), result ? "type 1" : "type 2"); return result; Or: final List

Which code style is preferred in Java?
final boolean result = isCausedBy(e, ExceptionType1.class)
|| isCausedBy(e, ExceptionType2.class)
|| isCausedBy(e, ExceptionType3.class)
|| isCausedBy(e, ExceptionType4.class)
|| isCausedBy(e, ExceptionType5.class);
log.debug("Error [{}] classified as {}", e.getMessage(), result ? "type 1" : "type 2");
return result;
Or:
final List> type1ExceptionTypes = List.of(
ExceptionType2.class,
ExceptionType3.class,
ExceptionType4.class,
ExceptionType5.class
);
boolean result = false;
for (Class extends Throwable> type1ExceptionType : type1ExceptionTypes) {
if (isCausedBy(e, type1ExceptionType)) {
result = true;
}
}
log.debug("Error [{}] classified as {}", e.getMessage(), result ? "type 1" : "type 2");
return result;
Is the second code snippet really preferred according to "clean code" considerations?
UPD. For context, isCausedBy
is based on Apache Commons:
import org.apache.commons.lang3.exception.ExceptionUtils;
private boolean isCausedBy(final Throwable throwable, final Class extends Throwable> clazz) {
return ExceptionUtils.indexOfThrowable(throwable, clazz) != -1;
}