Long Method? Move Accumulation to a Collecting Parameter

Table of contents
Reading Time: 2 minutes

You would have definitely come across long bulky methods which accumulate information to a local variable and pass back that information. Here the local variable goes through a lot of changes throughout the logic of the method before it is finally returned back. Let us see how the combination of compose method, that we covered in the last post, and moving accumulation to a collecting parameter can help us in this case. Let us consider the following code sample

[sourcecode language=”java”]
public List<Event> filterEvents(){
List<Event> filteredEvents = new ArrayList<Event>();
for (StateEvent stateEvent : allStateEvents) {
//if some logic is true then
filteredEvents.add(stateEvent);
}

for (DistrictEvent districtEvent : allDistrictEvents) {
for (Event event: filteredEvents){
if (event.getChairman().equals(districtEvent.getChairman())){
filteredEvents.add(districtEvent);
}
}
}
for (CityEvent cityEvent : allCityEvents) {
//if some logic is true then
filteredEvents.add(cityEvent);
}


… continues …

return filteredEvents;
}
[/sourcecode]

As you would notice, this method is an accumulating method. Why? Because it is a method that accumulates information into a collecting parameter. The collecting parameter here happens to be filteredEvents. Ideally, you could decompose this method in 2 ways

Either we can use the compose method and have individual methods pass back the results as events. These events could then be combined into a final list which can be passed back.

OR

Instead of having each of the methods return a result, which you later combine into a final result, you can incrementally accumulate a result by passing a Collecting Parameter to each of the methods. The methods write their information to the Collecting Parameter, which accumulates all of the results.

For our case let us see how the method would change

[sourcecode language=”java”]
public List<Event> filterEvents(){
List<Event> filteredEvents = new ArrayList<Event>();
filterPerStateEvents( filteredEvents);
filterPerDistrictEvents( filteredEvents);
filterPerCityEvents( filteredEvents);
return filteredEvents;
}
[/sourcecode]

As you can see the method becomes very small and clean as compared to the one that we started with. This method not only connects well with the reader by showing exactly what is happening but is also easy to extend and maintain.

And what do our private methods look like? You would have guessed by now, that they too are easy to read, clean and follow SRP.

[sourcecode language=”java”]
private void filterPerStateEvents (List<Event> filteredEvents){
for (StateEvent stateEvent : allStateEvents) {
//if some logic is true then
filteredEvents.add(stateEvent);
}
}

private void filterPerDistrictEvents (List<Event> filteredEvents){
for (DistrictEvent districtEvent : allDistrictEvents) {
for (Event event: filteredEvents){
if (event.getChairman().equals(districtEvent.getChairman())){
filteredEvents.add(districtEvent);
}
}
}
}

private void filterPerCityEvents (List<Event> filteredEvents){
for (CityEvent cityEvent : allCityEvents) {
//if some logic is true then
filteredEvents.add(cityEvent);
}
}
[/sourcecode]

With this post we saw how easy it is to refactor code and reduce the size of long bulky methods by using a combination of compose method and moving accumulation to a collecting parameter. Keep tuned. Wishing you a clean code.

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading