Which implementation is easier to understand?

Implementation Nr 1:

void addChild(Person child, List<Person> people) { int parentIndex = indexOf(people, person -> person.getName().equals(child.getParentName())); if (parentIndex == -1) { throw new IllegalStateException("No parent found for " + child.getName()); } people.add(parentIndex + 1, child); } 

Implementation Nr 2:

void addChild(Person child, List<Person> people) { List<People> allPeople = CopyOf(people); people.clearAll(); boolean childAdded = false; for (Person person : allPeople) { people.add(person); if (!childAdded && person.getName().equals(child.getParentName()) { people.add(child); childAdded = true; } } if (!childAdded) { throw new IllegalStateException("No parent found for " + child.getName()); } } 

Which implementation looks "better", from the point of clarity and maintenance, and why? I would appreciate if a relative comparison between the two is given ("1 is better than 2 because of x, but 2 is better than 1 because of y"), not absolute comparison ("both are wrong because of x,y,z").

submitted by /u/mercury0114
[link] [comments]

from Software Development – methodologies, techniques, and tools. Covering Agile, RUP, Waterfall + more! https://ift.tt/y2ElCoU

Leave a comment

Design a site like this with WordPress.com
Get started
search previous next tag category expand menu location phone mail time cart zoom edit close