Skip to main content

When to use static methods

I started using Resharper again after some time. And one thing that bugs me is that it always warns me that functions can be made static. I've kinda stopped using static methods. Static methods arent injectable, static methods make code using them less modular and less testable and static methods are the devils own handywork if you want to believe some people. On the other hand they make your code simpler by not requiring an object instance to be called. This got me thinking. Do I want to use statics or just ban them completely? And if not when is it ok to use them?

I started using IoC extensively in my code with Windsor and Ninject to support this habit. I've found this makes my code more testable and because it forces me to think about introducing dependencies it nicely decouples everything it touches. Less problems with calling uninitialised classes etc. I think IoC is a big enabler of TDD. I used unit tests before I did IoC but usually it took me more time to write and maintain my tests than the time it saved me. Mostly because of dependencies in my code.

Calling static methods means tightly coupling your code to this method so when is it ok to do this? I've come up with the following rule of thumb. I thought I'd throw this out there to get some feedback.

Static functions should have no side effects, they should not change the objects you give them as parameters and should only generate a return value.

This also means no reading/writing of files, databases etc. No network access. No PInvokes (yeah i know they are static by definition, that's why I wrap them in facade classes).

The reasoning behind this is that you should have no reason to want to replace a call to a static method in your code. Not for testing and not in production because you can't. If you want to unit-test or use code that uses this method you're forced to see it as a whole with the static method call itself.

What am I left with? Mostly simple utility methods. Parsing path names for example. Simple calculations. Simple factory methods, although those are an edge case to me. 

What do you think? 


Popular posts from this blog

Using xUnit.Net with .Net 4.0

I’ve been using xUnit.Net for a while now. It’s just a tiny bit cleaner and slightly less abrasive than other .Net unit testing frameworks. Leaving out unnecessary stuff like [TestFixture] and shortening Assert.AreEqual to the equally clear but shorter Assert.Equal don’t seem like big improvements but when you type them several times a day tiny improvements start to add up. I also like the use of the [Fact] attribute instead of [Test]. It shifts the focus from testing to defining behavior. So how do we get all this goodness working with the Visual Studio 2010 beta?

Running a Git repository on Ubuntu using Gitosis

20I’ve been using Git for a couple of small projects that I’ve been hosting on but version control for my bigger ‘secret’ projects still runs on a windows machine with visual svn server.Now that I’m starting to use Mono for a couple of projects so I’m playing with linux more. Last week I decided to try to try out gitosis on an ubuntu server. I found out it’s pretty easy to use when you know your way around git but for a noob like me some things weren’t immediately clear. Eventually I solved most problems I ran into, so I decided to write up the steps I took to install gitosis on ubuntu 10.04

Android development resource links

I've been playing with the Android SDK and I have a growing list of bookmarks to Android dev resources for my own use. I thought the best place to keep them would be here on my blog. That way other people can benefit too. I'll keep updating this list so feel free to add suggestions in the comments.