Day 3 (filtering melons by type and weight)

Things are getting even worse. Mark has now asked us to add a new filter that filters melons by type and weight, and he needs this quickly. However, the quickest implementation is the ugliest. Check it out:

public static List<Melon> filterByTypeAndWeight(
List<Melon> melons, String type, int weight) {
List<Melon> result = new ArrayList<>();

for (Melon melon: melons) {
if (melon != null && type.equalsIgnoreCase(melon.getType())
&& melon.getWeight() == weight) {
result.add(melon);
}
}

return result;
}

In our context, this is unacceptable. If we add a new filter criterion here, the code will become hard to maintain as well as prone to errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset