Closed hsiaojietng closed 2 years ago
I would say it is, perhaps try splitting into more methods?
@hsiaojietng as stated here, actual code is preferred over screenshots, to make it easy for others to copy-paste-modify the code in their answer.
I think it is, perhaps you can try changing it to this:
if (!Region.isValidRegion(region)) {
return totalPersons;
}
for (Person person: getFilteredPersonList()) {
Preference preference = person.getPreference.isPresent() ? person.getPreference().get() : null;
if (person.getUserType().isBuyer() && preference != null && preference.getRegionInString().equals(region)) {
totalPersons++;
continue;
}
Set<Property> setOfPropertyValues = person.getProperties();
for (Property p : setOfPropertyValues) {
if (p != null && property.getRegionInString().equals(region)) {
totalPersons++;
}
}
}
return totalPersons;
@hsiaojietng as stated here, actual code is preferred over screenshots, to make it easy for others to copy-paste-modify the code in their answer.
I apologize for that. I have rectified the issue.
@flairekq wow I did not think of that, really appreciate how simple it is now, thanks!
Good answer @flairekq In other words, use guard clauses to reduce nesting (and make the happy path prominent).
Is this code considered arrowhead?
If yes, may I know of any suggestions to change it? (Personally, I think it is an arrowhead code because of the additional defensive code checks, which should not count as arrowhead?)
Thank you for your help!