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

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 an

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.

Acer Iconia A500 review

A couple of weeks ago I bought an Android tablet, the Acer Iconia A500. Today I received an email from Acer asking to fill in a short survey to tell them what I like and don't like about my new tablet. I might as well share my thoughts on this tablet in a short review on my blog.