Today I encountered something that I have been wondering about in the past a few times before. How can I refactor a method that has this format:
private boolean isRectificationUpgradable(Rectification rectification) {
final boolean validType = rectification.getType() == RectificationType.VH
|| rectification.getType() == RectificationType.COL;
if (!validType) {
return false;
}
if (rectification.getStatus() != RectificationStatus.IN_PROGRESS && rectification.getStatus() != RectificationStatus.OPEN) {
return false;
}
if (rectification.getVehicle() == null) {
return false;
}
final List<CarPassCertificate> certificates = rectification.getVehicle().getCertificates();
if (CollectionUtils.isEmpty(certificates)) {
return false;
}
CarPassCertificate validCertificate = getValidCertificate(rectification);
if (validCertificate == null) {
return false;
}
return true;
}
When a rectification is created it has the status "open". An employee indicates that he started to handle it by putting it in the status "in progress".
The upgradable in this context means that sometimes the creator chose a certain type for the rectification, when in fact he could have picked a more accurate other type. In this case someone in the application can "upgrade" this rectification to the more specific type.
In code, this "upgraded" type is not modeled as another class as it is not really an upgrade, it always stays the same Rectification class and some shared additional fields are set to make it a bit more specific.
I think it is pretty readable but as more validations are added it can become very long. Any ideas?