We all know about it but there is always an issue how to explain it to a beginner, why static classes are just a no way to go. Of course, you as a software guru can directly point out at least 10 reasons why… “it is not testable”, “it is not object oriented coding”, “you make your code highly coupled”, and so on… Unfortunately, beginners accept them as “very smart reasons”, but they don’t fully understand them. How to explain why static classes are just pure evil?
Very simple example: you want to ask John a question
Our example comes from life. You want to ask your very good friend – John- a question. John is always happy to answer all of them. Unfortunately, sometimes he doesn’t know the answer so he asks his wife – Alice. Stop! now imagine that you want to map this real life situation to your code…
Your classes are like people
John is a class, you as well, and of course John’s wife too. So we have three classes with names “John”, “Alice”, “You”. When you want to ask John a question, you simply launch method on John object. Well, here you can do two things.
You can make your people(classes) like gods!
When you want to ask John a question, you will simply grab a phone and call to him or just ask him if he is nearby. Well, in the real world this is the common way to go. In the coding world you have bigger power! You are able to make your John a god!
When John is a god, you can ask him wherever you are
If you make your John (class) a god (a static class) you will be able, from now on ask John a question no matter in which place you are. This is just great! Imagine that John can be with you in all places you wish him to be. You are watching a movie and John is always sitting next to you, you are having a bath and John is also next to you… well the second example is not so accurate!
When John is a god, he is with you everywhere, also in places you don’t want him to be!
That is general issue with making John a god (a static class). He is everywhere. Your intimacy is revealed. He can always see you and you see him as well.
John’s wife can also ask him a question when you are talking with him
Imagine that you are talking with John, but suddenly, out of nowhere John’s wife cuts in and asks him something in the middle of your conversation. Because John loves his wife very much, your conversation is instantly over and he will happily answer his wife’s question. If this happens once, this is acceptable but because John is a god (static class). John’s wife can ask him whenever she likes. She is really driving you crazy!
John has to became mortal
To save your friendship with John there is only one way to go! John has to become a mortal (regular class). From now on, you will be able to ask him a question only when he is available. You can have him nearby when you meet on a street (you get him as argument to your constructor) or in a pub (you get him as property sets when your observer pattern notices him in the crowd).
Lastly and very importantly, don’t make an mistake and make John’s wife a god!
You can think that John was lousy god so you decided that instead of him, you can make Alice (John’s wife) a god. Imagine that Alice is able to listen about what you are talking having a beer in a pub… well, I think you know what I mean…
General rule – don’t use static classes unless…
There is no other way to do it (to write your code). Having a god (static class) is not bad, but when you talk with him, make sure that no one can disturb you! (take care of singleton pattern issues, because static class is just a singleton).