Java – Static Methods

I currently work in a code base where static “utility” methods are used fairly often.  This probably stems from the legacy of the code base.  Back when the company was just getting started, there were only a couple of developers (including myself).  While we all knew how to write Java code (syntax), none of us knew how to write good Java code (Object Oriented, encapsulated objects, etc.).  So we wrote a bunch of static utility methods trying to keep the code clean.

Ten years later, I’m still with the company (as are 2 of the other 3 original developers) and we are still working in the same code base.  The company has grown steadily and there has never been available resources to take on a whole system re-write.

The original code from 10 years ago isn’t the only problem.  The development team has grown significantly over the years as well.  Most of the team consists of developers that have very little experience in Object Oriented Java before joining our team.  So they tend to use the existing code as an example of how to write new code, which means more static utility methods and the cycle continues.

So what’s wrong with Static Methods?

If used in the right context, there is nothing wrong with them.  But using them to often or in the wrong places has several issues.

Automated Unit Testing

Static methods make implementing automated unit testing very difficult.  There is no easy way to stub out the methods.  There isn’t an interface to program to, and you can’t use polymorphism to override the static method to return a specific result for testing.  So when writing unit tests on code that uses static methods, you end up testing the not only the code in the method you are trying to test, but the code in the static method(s) as well.  This is often the biggest reason given for not using static methods.  The company I work for doesn’t use automated unit testing (I’ll post about that another day), so this doesn’t work as a reason not to use them for us.

Object Oriented

Static methods do not extend any other class or methods and cannot be extended.  So the code in them can not be reused – unless it does exactly what you need it to do already. Static methods are not Object Oriented in anyway. I’m not saying they can’t be utilized in OO code, just that they themselves are not.

Readability

Static Methods alone don’t make for hard to read code, If they are over used or the class or method name is long it can certainly detract from readability:

// Harder to read
User user = new User(StringUtility.removeSpecialCharacters(firstname), StringUtility.removeSpecialCharacters(lastname));

// Easier to read
User user = new User(firstname.removeSpecialCharacters(), lastname.removeSpecialCharacters());

Passing the same object to multiple static methods to have it just return the same object (with modified data/state) quickly becomes difficult and tedious to read.

// Harder to read
order = CartUtility.calculateTaxes(order, billingState);
order = CartUtility.calculateOrderTotal(order)
print("OrderTotal:" + order.getOrderTotal());

// Much easier to read
order.calculateTaxes(); //better yet, make this part of calculateOrderTotal()
print(“OrderTotal:” + order.calculateOrderTotal());

So when SHOULD I use a static method?
You should use a static method if (and only if) ALL the following are true:

You are writing utility class and they are not supposed to be changed.
The method is not using any instance variable.
You aren’t passing in a parameter and getting the same object returned. Generally this means the method should be put in the object you are passing/returning.
It just doesn’t make sense to create them as instance methods for some reason.
You are sure that the definition of the method will never be changed or overridden.

What are your thoughts? Leave a comment below and let us know

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s