← Back to Upcase

When to make simple method static?


(Justin Gordon) #1

Supposing a small utility method does not use any instance data. Say it’s simply a string transformation. Should this method by default be static? Suppose the situation is relatively simple, so we’re not worried about subclassing.

I can see arguments both for and against making the method by default static. For example, declaring the method to be static makes it clear that there’s no dependency on instance data.

OTOH, why bother making such a method static? This would make it slightly harder to subclass later. Then again, designing about what might happen violates YANGI.


(Ben Orenstein) #2

I pretty much always just make things like this instance methods.

It doesn’t hurt anything up front, and makes later refactoring easier (I find a class with one static method will almost always collect a few more over its lifetime).