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? 

Comments

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?

Square One available on the Android market

This is just a short post to let you know that a first version of the Android app I’ve been working on for the last couple of weeks is available on the Android market. The app is called Square One and it’s a simple bassline synthesizer. It’s free so try it out and let me know what you think of it, but be prepared it’s still an early version. I hope to add more features in the next few months and maybe build something that can be used to create real music.The lower part of the screen contains the sequencer controls that can be used to program your own bass lines. On the left is a four by four grid of buttons where you can select a step in the sequence. On the right you can select the note to be played on that step. When you’re done you can press Start and the sequence starts playing. The knobs on the top can be used to control a couple of parameters from the synthesizer engine that creates the sound. You can control the cutoff frequency and resonance of the low-pass filter, attack and …

Building Android projects with Jenkins, Ant and Mercurial

I have recently set up a Jenkins build server for my Android projects hosted on Bitbucket. It’s not difficult but there are a couple pitfalls and the information on how to do this isn’t available from one single place so I decided to document the process and put up the information over here. Maybe other people will benefit from having a step-by-step guide too.