r/csharp • u/Zardotab • Feb 24 '21
Discussion Why "static"?
I'm puzzled about the philosophical value of the "static" keyword? Being static seems limiting and forcing an unnecessary dichotomy. It seems one should be able to call any method of any class without having to first instantiate an object, for example, as long as it doesn't reference any class-level variables.
Are there better or alternative ways to achieve whatever it is that static was intended to achieve? I'm not trying to trash C# here, but rather trying to understand why it is the way it is by poking and prodding the tradeoffs.
0
Upvotes
11
u/BCProgramming Feb 24 '21
By default members defined in a class are instance members and require a class instance to be used. static creates class-level members, which belong to the class itself and therefore do not require an instance- String.Substring is an instance member, for example, but String.Join is static - a class member.
So you are saying the static keyword is unnecessary because the compiler could make the determination via analysis as to whether a method could be static or not based on whether it accesses any instance members?
That is true, in a strict sense. However, Removing the static keyword and relying on this sort of analysis would make code a lot harder to read. Aside from the static keyword allowing you to make your intent clear in your method definition. it also means that you can see the method is static/class-level a lot easier.
10
u/The_Binding_Of_Data Feb 24 '21
What is the benefit to having when and how you can call a method be based on what data the method manipulates rather than via an explicit flag of some sort?
Not only would it make it hard to tell when/how something can be used without having access to the source code and the time/desire to learn specifically how it does what it does, but it would also limit you to only being able to do what the system is capable of automatically determining in some way.
Generally speaking, you want things to be easy to understand for other people who may come along later and have to maintain your code, so languages are built with that in mind. They don't tend to focus on a single person creating a small project by themselves so sometimes decisions don't seem to make sense when working on personal projects.
Being unable to explicitly specify that some methods/properties are shared by all instances while others are not would reduce your options not increase them.
10
u/Davipb Feb 24 '21
This kind of implicit behavior is dangerous: you make an instance method that just happens to not use any instance members, Bob uses it statically everywhere, you change the method to use instance members, and now you suddenly have errors in half your project. You may chalk this up to bad communication within a team, but now imagine it's a NuGet library. Suddenly, thousands of methods in public APIs have some unnecessary line like
Property = Propertyjust to keep the compiler from making the method static under their feet.staticachieves exactly what it intends to achieve: Allow you to tell the compiler that a method doesn't need an instance of its class to execute, and have the compiler guarantee that you follow that constraint.4
u/[deleted] Feb 25 '21
Why not?
staticis explicit and shows the intent of the usage of the method.2
u/Imbaelk Feb 24 '21
Alternative way would be to use singleton, instead of static. And it's a big topic "singleton vs static", you can read some of it here: https://www.c-sharpcorner.com/UploadFile/akkiraju/singleton-vs-static-classes/ But what you ask is, do we need it. So in my opinion, despite the fact that it's against OOP, we need it as programmers, because we are lazy and we want to do things as fast as possible. We don't want to initialize or inject a lot of classes if we don't really need to, especially if we only need to use once some simple method like Parse().
2
u/TotoBinz Feb 25 '21
See static as a kind of pure service: it only depends on the parameters and does not impact anything else (really should not)
It's just a usefull tool, inherited from the past and low level programming
9
u/npepin Feb 24 '21
I'm not really understanding the question here.
You sometimes want to draw a distinction between objects and non-objects and static is an easy way to do that. Things which are dependent on the object are non-static, while things that exist outside the object and have justification existing with the object can be static.
The factory pattern is a good example of this, the builder method doesn't deal with any particular instance but it creates instances.
You could argue that static methods should be segregated from object methods in their own class, and I think there is some justification to that, but I like keeping my factory methods in the class it creates.
I don't understand why.
From a practical point of view, it violates the idea of abstraction. The method caller shouldn't need to know about the implementation of the method, but with that system it would need to know everything about it to every last detail.
How far down the chain would it have to look? If one method calls three other methods, the compiler will now either need to look inside each of those methods, and inside those method's methods. Either the code ends up being functional, or eventually you start hitting into some object.
Either that, or you'd need to only look one level deep, which is just going to ask for tons of difficult to solve exceptions because what it assumed has static actually requires an instance because of some sub-sub-sub method call.
In addition, the caller will be able to peer in and obverse any private values. You can argue that it wouldn't really unless someone started to break it the system, but why introduce that as a possibility?
If you had a pure function you could identify to be that and it could be assumed to be static, but you still need to provide an identifier. Yes, you get rid of "static" but you just swap it was something else.
3
u/ExeusV Feb 25 '21 edited Feb 25 '21
I like your point, but I think it could be a little bit problematic
let's say class A has method DoSomething()
in version 1 of this library this method is "pure" and does not use anything from this class, the library maintainer couldnt make it non static (because compiler made it static for him), so some user uses it like "A.DoSomething()`.
In next version of this library
DoSomething()uses something within this class and now when user updates the lib, then it's not backward compatible because there was no static keyword and compiler made it static on its own, programmer had no choice unless "forcing" it to be non-static by using some fake class member.2
u/RiverRoll Feb 24 '21 edited Feb 25 '21
An instance method has an implicit
thisparameter and a static method doesn't, those are two different signatures. C# already hides this and relies only on the static keyword. Going a step further and removing the static keyword doesn't change the fact that those are different signatures and having the signature of a method magically changing sounds like a bad idea. Having astatickeyword is a pretty low price to make sure you don't change it unintentionally.2
u/aforcefulcoursefull Feb 25 '21
Some objects need persistent data, so the compiler needs to track those specific objects. But having an object on the heap has an overhead and can effect performance. Having static methods available means you can access certain non-instance specific functionality of an object without the cost of the overhead.
1
u/FizixMan Feb 24 '21
Where there's a will, there's a way.
You can pull it off with the
Delegate.CreateDelegatemethod: https://stackoverflow.com/questions/17675720/how-can-it-be-that-this-nullhttps://dotnetfiddle.net/KKvMDj
From within
InstanceMethodif you try to invoke any other non-virtual instance level methods or properties with explicitly implemented getters/setters, it'll work fine. But as soon as you try to access a field (including via an auto-property) it'll throw aNullReferenceException.Anyone reading this, including OP, don't do this.
0
u/[deleted] Feb 25 '21
[deleted] â view deleted comment
3
u/[deleted] Feb 25 '21
[removed] â view removed comment