{"id":7,"date":"2008-05-21T10:27:00","date_gmt":"2008-05-21T10:27:00","guid":{"rendered":"http:\/\/www.andypalmer.com\/blog\/?p=7"},"modified":"2009-05-22T13:10:39","modified_gmt":"2009-05-22T13:10:39","slug":"static-utility-methods","status":"publish","type":"post","link":"https:\/\/andypalmer.com\/2008\/05\/static-utility-methods\/","title":{"rendered":"Static Utility Methods"},"content":{"rendered":"
A few of us were chatting about static methods the other day.
I’m not a big fan. I think that they tie you unnecessarily to a concrete class.<\/p>\n
Most people were saying that there is no harm in having static methods in utility classes, and this is one place where I would disagree.
The example used was the java.lang.Math class with it’s utility methods.<\/p>\n
java.lang.Math is non-instantiable, you can only access it’s utility methods through static calls.
For the most part, this is fine, but take the example of Math.sqrt(x)
This method returns NaN if x is < 0. For the majority of cases this is fine, but if I start working with complex numbers, then this is no longer the ideal behaviour.
Admittedly, in this case I would not want the return value to be a double, but I may want it to throw a ComplexNumberException or something similar. If I have used the static method throughout my code, I now have to go and update every single reference to the concrete class. What a pain in the class!<\/p>\n
However, if I had used a utility class with instance methods, I could have injected the utility class in the constructor and I could now change it out for my new ComplexMathUtility class through configuration \/ DI.<\/p>\n
This isn’t the best example, as the required return types are different, but think of a String utility class, perhaps something that formats a header for a report.
We could (naively) implement this using static utility methods. If we later require two different types of report heading, this is going to make our life difficult.
If we implement this using an instantiable ReportFormatter, we can swap in new ones at runtime \/ configuration. We’ve just implemented a ReportFormatterStrategy \ud83d\ude42<\/p>\n
My feeling is that implementing utilities as instance methods keeps the design flexible and allows for more code reuse as things naturally gravitate towards smaller, more easily testable, logically grouped units of functionality…
Or am I just dreaming? \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"
A few of us were chatting about static methods the other day.I’m not a big fan. I think that they tie you unnecessarily to a concrete class. Most people were saying that there is no harm in having static methods in utility classes, and this is one place where I would disagree.The example used was […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20,17,21],"_links":{"self":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/7"}],"collection":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":1,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":124,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/7\/revisions\/124"}],"wp:attachment":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}