Long Methods? Try Compose Methods

Table of contents
Reading Time: 2 minutes

We all have seen long winding code that goes on and on. By the time you have hit page down a couple of times, you forget what the method was doing. You forget because the method is doing a lot. It is failing the SRP principle and needs to be broken down into smaller pieces. According to me if you are looking at a method and cannot figure out what it is doing in 5 secs then it needs to be rewritten.

Want to try, ok tell me what this method is doing
[sourcecode language=”java”]
public class ListToMania{

public void add(Object element) {
if (!readOnly) {
int newSize = size + 1;
if (newSize > elements.length) {
Object[] newElements = new Object[elements.length + 10];
for (int i = 0; i < size; i++) {
newElements[i] = elements[i];
}
elements = newElements;
}
elements[size++] = element;
}
}
[/sourcecode]

5 seconds are up 😉

To give you some idea, this is the add method for adding a new element to the list. This code checks to see whether the size of the elements array will exceed capacity if a new object is added. If capacity will be exceeded, the elements array is expanded by a factor of 10.

Ok, now let us see if this could be simpler to read and work for the 5 second rule that we have

How about this one,
[sourcecode language=”java”]
public class List.ToMania{
public void add(Object element) {
if (readOnly) return;
if (isTheListAtCapacity()){
growTheList();
}
addElement(element);
}
[/sourcecode]

Does this one suggest what it is doing in 5 Secs? Sure it does.

Let us see what we have done,

We have tried to do is convert add(Object element) into a composed method. It is composed of many other methods. What this does is that it calls other methods. You can read a composed method within seconds and decipher WHAT it is doing. For understanding the HOW part you need to visit the methods that it is calling.

Our private methods now look like this
[sourcecode language=”java”]
private boolean isTheListAtCapacity(){
return (size + 1) > elements.length;
}

private void growTheList(){
Object[] newElements =
new Object[elements.length + GROWTH_INCREMENT];
for (int i = 0; i < size; i++)
{
newElements[i] = elements[i];
}
elements = newElements;
}

private void addElement(Object element) {
elements[size++] = element;
}
[/sourcecode]

So using the compose method, you avoid having long methods, you also pass the 5 second understanding test. The caveat is that if there are too many methods in a composed method then you need to look if the composed method is doing a lot of things. Another problem is that if there are too many private methods then debugging would be tedious. However if you face any of these issues then probably your method is trying to do a lot in the first place.

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

1 thought on “Long Methods? Try Compose Methods2 min read

Comments are closed.

Discover more from Knoldus Blogs

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

Continue reading