September 2019
Intermediate to advanced
816 pages
18h 47m
English
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.