Using Static

1 36
Avatar for StrungSafe
4 years ago

Disclaimer

I realized that some of these posts may not be syntactically or semantically correct for all languages so for the most part I am referring to .NET and similar languages.

Static All The Things

So you are programming and you might be using an IDE like Visual Studio and maybe even using an add-on like Resharper (which I highly recommend). All of a sudden you notice notifications showing on the side-bar. These suggestions say that your method could or should be static. You think, why not...it provides great suggestions overall and why would it suggest a bad change? I plead that you avoid this suggestion.

Using Static As Required

So when should I use static? In short, when static is required or you want to express an intent. Static, like your method signatures, should express intent; it should tell the consumers how you expect them to consume your code.

Using static, when it is not required, can cause confusion. Junior developers may not realize the implications of adding static; especially as more applications move to the web where multi-threaded applications are ubiquitous. Even senior developers may overlook details if static is overly used.

Exceptions

Exceptions will always exist and one should probably conform to the code base one is working in. Consistency can be beneficial even if that means persisting bad practices. Just remember the implications of adding static to your classes and methods; it has the potential to cause problems later.

3
$ 0.00
Avatar for StrungSafe
4 years ago

Comments

You inspired me with this article, very cool and I'm waiting for more, [I read to read it to others

$ 0.01
4 years ago