In C# 2 there was a new addition in the functionality called static classes and members. When i say "members", i am referring to methods, properties, events and so on. I have a strong opinion about this modifier which i will share with you along the article, but let's start by looking at what "static" means. Take this code as example:
Above we can see that we define two properties and one is static. In the main method we then create an instance of Person and set the normal property (Name). Now notice the Person.NumberOfBrains assignment. This is the static property we created in the class. Notice that the access is made at class level, instead of instance level. This means that there will only be one NumberOfBrains value throughout all Person instances, but there will be a Name value per each instance of Person.
Of course, the example is not the best, but you get the idea. Static applies when all instances share the same value. There are some rules to be aware when you use static.
Above we can see that we define two properties and one is static. In the main method we then create an instance of Person and set the normal property (Name). Now notice the Person.NumberOfBrains assignment. This is the static property we created in the class. Notice that the access is made at class level, instead of instance level. This means that there will only be one NumberOfBrains value throughout all Person instances, but there will be a Name value per each instance of Person.
Of course, the example is not the best, but you get the idea. Static applies when all instances share the same value. There are some rules to be aware when you use static.
- You can't access instance members in static methods. If i had a static method in the Person class, i would not be able to access the Name property.
- You can't have non-static members in static classes.
- You can't instantiate a static class. What would you instantiate anyway?
- You loose OO concepts such as inheritance, and composition (such as implementing interfaces and deriving from classes).
- Static code is not easily testable and mockable. Most isolation frameworks (except super-frameworks) use inheritance to create fake implementations of a class and thus provide an empty implementation for unit testing purposes. If you are using a static class, you can't use such framework and can't isolate parts of the code that use that static class. This is a big hit for me.
- Static classes don't provide good means of allowing your code to be extensible. You can't inherit from a static class (what would you be inheriting) and you can't obviously override it's methods/properties. This completely seals the class for extension.
- I've seen excuses like 'this way you don't have to instantiate an object'. This is non-sense to me by itself. Of course i don't have to instantiate, but what do i gain from that? Only in very simple cases i wouldn't want to use an instance of an object as this would be a good way to keep the behavior flexible.
- Static classes and members get really messy, really fast when you contain application state (or even worse, business logic) in such places. Before you know it, you have to pass a myriad of objects or create a Data Transfer Object for the sole purpose.
Apart from this, i think there are some valid situations where static classes are actually useful.
- To contain helper methods that don't really do much (look at .Net's Convert class, for example)
- To maintain a list of globally accessible variables or services to the entire application. I make these static so i don't have to instantiate an object and inject it everywhere. Of course, i do this only for very, very, very simple cases. Usually, this "Global" static class only contains properties (of interface types), so i can maximize testability.
- As we will see, static classes have big importance in C# with the introduction of Extension Methods. More on this later.
- To contain methods that don't really have a place in a specific class.
My take is: always try to keep actual behavior and business logic out of static members. Why are you using an OO language like C# if you are denying it's OO capabilities? Of course, there will always be a situation when using static is better or easier, so it's a welcome addition to the language!
Comments
Post a Comment