Always! have your preconditions and Guard Clauses at the begging of the Method.
Don’t check Guard Clauses and don’t do validations in the middle of the method
DON’T
if (CONTRACTED == newStatus) {
newOffer.setContractedDate(now(of()));
if (isCalculatedOffer(newOffer.getId())) {
newOffer = saveConsideringOldVersion(newOffer, oldOffer, companyId);
newOffer = recalculateContractedPrice(newOffer);
} else {
throw new BadRequestAlertException(withKey(OFFER_NOT_CALCULATED));
}
facade.resetNewCustomerAndSubstitutionalSupplyFlags(newOffer.getCustomerId());
facade.updateHasContractedOfferField(newOffer.getCustomerId(), true);
}
DO it like this:
if (CONTRACTED == newStatus) {
if (!isCalculatedOffer(newOffer.getId())) {
throw new BadRequestAlertException(withKey(OFFER_NOT_CALCULATED));
}
newOffer.setContractedDate(now(of()));
newOffer = saveConsideringOldVersion(newOffer, oldOffer, companyId);
newOffer = recalculateContractedPrice(newOffer);
facade.resetNewCustomerAndSubstitutionalSupplyFlags(newOffer.getCustomerId());
facade.updateHasContractedOfferField(newOffer.getCustomerId(), true);
}