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

Mar 3, 2025 - 20:02
 0
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> type1ExceptionTypes = List.of(
        ExceptionType2.class,
        ExceptionType3.class,
        ExceptionType4.class,
        ExceptionType5.class
);
boolean result = false;
for (Class 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 clazz) {
    return ExceptionUtils.indexOfThrowable(throwable, clazz) != -1;
}