in defense of static methods

I recently came upon a short rant named What’s The Problem With Static Methods? by Jason Gorman.

Jason argues there that

.. methods should be instance methods by default

because static methods cannot be composed/used with IoC and might make your TDD a really horrible experience.

A strongly disagree with both the conclusion and the reason for it – indeed I love to use static methods and will happily use ReSharper to convert them for me.

I would even encourage you all to write as much of them as possible!

Why I disagree


I am talking about the .net stack (say C#) here.

I don’t know enough about the state of things in Java/JVM but I doubt that it will differ much from what we can do in .net/mono.

Why do I think that static methods should not be avoided?

Because you can compose them in a functional way without having to new-up a instance first. And of course sometimes creating a object just because everything needs to be an object is just stupid.

Why should I have to instanciate a ChecksumCreator before I can calculate a checksum? Why is there even something like the singleton-pattern?

Nope – I rather not have objects everywhere and of course modern hybrid-languages like F# encourages you to write more static methods (for example all let bindings in a module).
For me this is working far better than the usual pure OOP paradigm.

Also I don’t buy into the dependency-injection story.

If you think you cannot use DI with static methods (let’s just call them functions) you might want to read a bit about what higher order functions are.
We have delegates for quite a while now and a delegate is more or less just a one-method interface, that works fine with static methods. That’s your DI right there!
Remember: long before there was a IComparable interface we already had generic sorting in C.

The example Jason gave is a static method using some database. I agree with him here: the database(-connection) is a dependencies that is best kept in instance-fields. But for many other examples (like the sorting algorithm) going full OOP with interfaces, classes, .. is just insane.