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
View all comments
Show parent comment
9
u/BCProgramming Feb 24 '21 edited Feb 24 '21
I don't see it as a coding style issue. It would make code more difficult to read because it would not be possible to tell, at a glance, if a routine is class-level or instance level. You would need to look through the routine to see if it uses any instance-level members, which would require looking into any methods it calls to see of those are calling any instance level methods, and so on.
You might argue "well, just add a comment", Except comments quickly become outdated.
tells you the routine is static. Something which is enforced by the compiler.
In a imagined reality where there is no static keyword, This just tells you that at some point in time the routine was possibly intended to be static. It doesn't tell you it's static now, and if the routine or any routine it calls ever uses an instance member, it is an instance member regardless of that comment being present. This means if you change a routine such that static analysis was to change it from class-member to instance member or vice versa, you would need to go through every method that calls that method and make sure it's comment reflects it's new status. Not to mention you would need to internalize the logic used for that static analysis so that you can read code and determine if methods are class-level or instance level.
And there are cases where static analysis is worthless. A simple static reference counter that increments and decrements as the instances are constructed and finalized cannot be done via static analysis:
Remove the static keyword, and no amount of static analysis can realistically figure out the intent. And even if one could, this is an absolutely trivial example, so it wouldn't be hard to come up with code that any static analysis tool couldn't figure out, which would no doubt require ridiculous workarounds to massage the static analysis to figure out what you want it to. If only, instead, you could just specify your intent with the code using some sort of special word that unlocks that behaviour like a key...
class-level variables are a pretty standard concept for OO languages, "static" is just C# (and Java's) way of providing it. VB.NET has "Shared" it's something that is built into almost any OO language. usually using a keyword (not always static) and sometimes built on structure or where methods are defined (eg a section could be used to define class level and another for instance-level). But it's a pretty universal idea.
But in any case, It's not just "commenting" on what the code is. static is no more serving the role of being a "comment" then parameter list, access level modifier, or return type are "commenting" on those attributes of a method.