Functions should do one thing, do it completely, and do it well.
When a function is this long, it makes it Long functions are difficult to reason about.
I'm not including them here, rather I'm assuming the presence of String getSubjectCode()
and Pupil getPupilInformation()
methods, each containing the current code embeddedcurrently in other functionalitythe mega-function.
School.getSubjects()
should return a List<Subject>
(or Collection
). (Or at least a properly-typed array; nothing with Object
s.)
In the loopThere's a lot of code in block that loops over the school's subjects, you have a large bodymost of code insidewhich is the block for when the subject is found.
Better toIn cases like that, might as well do thea negative test and continue
in this case, slightly "flattening" the loop's guts. (Code turned on its side is not a graph of how awesome it is ;)
That said, this functionality is available in a contains
etc. and loopingmethod--looping in the mainline code isn't necessary. See next.
As per the above, the functionality for findingDeciding if a school has a specificgiven subject should be moved into the School
class, for example:
This workAll the above turns the mainline code into the following: