It's generally agreed upon that relying heavily on global stuff is to be avoided. Wouldn't using static classes and methods be the same thing?
-
Why do you say "back in the day"? Do you have any reason to think that it is less frowned upon today? – jalf Jun 30 '10 at 17:29
-
Duplicates: http://stackoverflow.com/questions/752758/is-using-a-lot-of-static-methods-a-bad-thing and http://stackoverflow.com/questions/731763/should-c-methods-that-can-be-static-be-static and http://stackoverflow.com/questions/2227793/why-are-static-classes-used and ... Plus it should probably be community wiki. – Abel Jun 30 '10 at 19:41
-
Global state is bad, but the reason we have things like singletons (which are *workarounds* and not a real solution) is that they at least depend on lazy construction (initializing as they are accessed) which avoids initialization order problems. That and the fact that they're objects means we can apply OOP techniques to them like inherit them, use polymorphism, and so on. – stinky472 Jun 30 '10 at 23:26
-
This question seems to have been over-edited. Many of the comments don't even make sense anymore . Same with some of the tags. Goes to show what a decade of small style edits can do when compounded. – Nate T Jan 15 '21 at 19:09
10 Answers
Global data is bad. However many issues can be avoided by working with static methods.
I'm going to take the position of Rich Hickey on this one and explain it like this:
To build the most reliable systems in C# use static methods and classes, but not global data. For instance if you hand in a data object into a static method, and that static method does not access any static data, then you can be assured that given that input data the output of the function will always be the same. This is the position taken by Erlang, Lisp, Clojure, and every other Functional Programming language.
Using static methods can greatly simplify multi-threaded coding, since, if programmed correctly, only one thread will have access to a given set of data at a time. And that is really what it comes down to. Having global data is bad since it is a state that can be changed by who knows what thread, and any time. Static methods however allow for very clean code that can be tested in smaller increments.
I know this will be hotly debated, as it flies in the face of C#'s OOP thought process, but I have found that the more static methods I use, the cleaner and more reliable my code is.
This video explains it better than I can, but shows how immutable data, and static methods can produce some extremely thread-safe code.
Let me clarify a bit more some issues with Global Data. Constant (or read-only) global data isn't nearly as big of an issue as mutable (read/write) global data. Therefore if it makes sense to have a global cache of data, use global data! To some extent every application that uses a database will have that, since we could say that all a SQL Database is one massive global variable that holds data.
So making a blanket statement like I did above is probably a bit strong. Instead, let's say that having global data introduces many issues that can be avoid by having local data instead.
Some languages such as Erlang get around this issue by having the cache in a separate thread that handles all requests for that data. This way you know that all requests and modifications to that data will be atomic and the global cache will not be left in some unknown state.

- 10,455
- 1
- 44
- 80
-
8+1 for `Global data is bad. However many issues can be avoided by working with static methods.`. That distinguishes static data from static methods. – decyclone Jun 30 '10 at 17:59
-
2without using global data, could you please elaborate how not to waste memory on an IIS server where each request spawns dozens of lookups, maps and dictionaries which could eat up 10-20 MB per connection? I think the blanket statement "global data is bad" will lead web developers to make some poor design choices. – code4life Jun 30 '10 at 19:02
-
It appears that no one is really making much use of state versus statelessness, which, imo, is core here. I agree with @code4life on blanket statements being misleading (especially when they're the first sentence!) – Marc Jun 30 '10 at 19:19
-
1@code4life - you are correct, there are some times when global data is needed. For instance, I have a graphic rendering engine where I cache hundreds of MB for use by multiple threads. So perhaps my blanket statement is a bit off. Let me edit my comment above. – Timothy Baldridge Jun 30 '10 at 19:46
-
What are the advantages of a global static cache against a cache with an interface passed around? The later makes testing and debugging easier. What can the former do for us? – mg30rg Mar 19 '18 at 09:30
-
@TimothyBaldridge Could you elaborate more on the statement "For instance if you hand in a data object into a static method, and that static method does not access any static data.....then you can be assured that given that input data the output of the function will always be the same" Thank you – Abdulkarim Kanaan Apr 16 '18 at 08:51
static
doesn't necessarely mean global. Classes and members can be static private
, hence only applying to the specific class. That said, having too many public static
members instead of using appropriate ways to pass data (method calls, callbacks, etc.) is generally bad design.

- 20,544
- 2
- 29
- 55
-
5`static private` data is still global, though it is only visible to the class in question. – Jeff Sternal Jun 30 '10 at 17:31
-
1-1 for misunderstanding what private static data is. See above comment. – Timothy Baldridge Jun 30 '10 at 17:37
-
11I do very well understand what private static means. A private static member is not meant to be accessed globally (i.e. from any other class than the declaring one). Of course its data is global, but without hackery it is not gobally *accessible*. – Janick Bernet Jun 30 '10 at 17:41
-
6But it still has the same issues as global data, i.e. Anything accessing that data will be modifying the same piece of data used by every other thread/method call. This is the true issues with global, or static data, it ruins the deterministic nature of the code, and makes it harder to debug, and maintain, since you have no way to know what method modified the code. Static Private data in a C# object is practically different than a file global variable in C. Both have the same drawbacks – Timothy Baldridge Jun 30 '10 at 17:49
-
5What you are aiming at here is more an issue of parallel data access than of static members. The issues of the same piece of data used by different threads is independant of static or not static data. The problem with public static data is that it is often missused to pass some information between classes/ objects instead of passing the corresponding data as a parameter to a method. Having on static 'registry' introduced some high coupling/dependency on that repository. That's the problem I see with public static, that definitly doesn't apply to private static. – Janick Bernet Jun 30 '10 at 18:11
-
Sorry, I didn't mean to pick a fight about terminology - I just meant that making static variables private doesn't solve **all** of the problems associated with global state. I think of them as "Hidden Globals" [in the term coined by the C2.com contributors](http://www.c2.com/cgi/wiki?GlobalVariablesAreBad). – Jeff Sternal Jun 30 '10 at 18:58
-
I totally agree. However, IMO, there is still a considerable difference between private static and public static. – Janick Bernet Jun 30 '10 at 19:32
If you're trying to be purist about your OO development, then statics probably don't fit the mold.
However the real world is messier than theory, and statics are often a very useful way to solve some development problems. Use them when appropriate, and in moderation.

- 3,409
- 22
- 25
Mutable static variables are bad because they're just global state. The best discussion I know of about this is here, under the heading "Why Global Variables Should Be Avoided When Unnecessary".
Static methods have several drawbacks that often make them undesirable - the biggest one being that they cannot be used polymorphically.

- 47,787
- 8
- 93
- 120
-
-
-
2@Dean J - indeed, I'm not a madman. :) I'll update my answer accordingly. – Jeff Sternal Jun 30 '10 at 17:55
As an addition to whatever else is said, final static
variables are just fine; constants are a good thing. The only exception to that is when/if you should just move them to a properties file, to be easier to change.

- 39,360
- 16
- 67
- 93
-
3This question is tagged as c#, so I'll assume you mean `const` instead of `final static`. – Powerlord Jun 30 '10 at 19:03
-
2The question is also tagged language-agnostic - maybe it's a wash? – Jeff Sternal Jun 30 '10 at 19:23
-
Upvote on both comments; I still don't rapidly switch between C# and Java well. – Dean J Jul 01 '10 at 19:19
-
In my own projects, I even store my constants in ConstantProvider classes accessed through IConstantProvider interfaces. – mg30rg Mar 19 '18 at 09:34
First, why are the old global variables so bad? Because it is state that is accessible from anywhere, any time. Hard to track.
There are no such problems with static methods.
That leaves static fields (variables). If you declared a public static field in a class, that would truly be a global variable and it would be bad.
But make the static field private and most problems are solved. Or better, they are limited to the containing class and that makes them solvable.

- 263,252
- 30
- 330
- 514
Static methods are used to implement traits in Scala. In C#, extension methods (which are static) fulfill that role in part. That could be seen, as the DCI proponents state, as a "higher order form of polymorphism".
Also, static methods can be used to implement functions. This is what F# uses to implement modules. (And also VB.NET.) Functions are useful for (unsurprisingly) functional-programming. And sometimes they're just the way something should be modeled (like the "functions" in the Math
class). Again, C# comes close here.

- 55,340
- 13
- 112
- 144
public class Foo
{
private static int counter = 0;
public static int getCounterValue()
{
return counter;
}
//...
public Foo()
{
//other tasks
counter++;
}
}
In the code above you can see that we count how many Foo objects were created. This can be useful in many cases.
The static keyword is not global, it's telling you that it's on class level, which can be very useful in various cases. So, in conclusion, class level things are static, object level things are not static.

- 361
- 1
- 5
- 10
-
I can - by hearth - name at least three cases when counter won't necessarily contain the actual number of Foo objects created. – mg30rg Feb 05 '20 at 13:39
I think the bad thing about global variables is the idea of having global state - variables that can be manipulated anywhere and tend to cause unintended side effects in far-flung areas of a program.
Static data would be similar to global variables in that they introduce a kind of global state. Static methods though are not nearly as bad, assuming they are stateless.

- 59,820
- 9
- 127
- 177
Not entirely. Static actually determines when, where and how often something is instantiated, not who has access to it.

- 11,663
- 2
- 41
- 66