What is to static in programming

By | April 25, 2010

As I recently started a new job I also had to dive into a completely different culture. Programming and code style wise that is. One of the things I came accross was the choice to always use static members. But is this desirable in programming and good conventions.

Personally I think static members should be reserved for helper classes and singleton instances. But why?

One of the main reasons for reserving it for helper classes is simple. A lot of 3rd gen programming languages don’t allow functional programming, hence you need to create a class to support generic functions. In a way you are just trying to store some simple functions in a namespace (C++/.net) or package (Java), but this is not supported. I believe this is fine when all members are static.

A second reason for a static member is to support singleton, or factory model classes. In these situations it is desirable to have one single access point to get an instance of a class. But no other methods should be static.

If you are thinking about making a member static then ask yourself the following question: “Why does this need to be static?”. And the following answers aren’t good once:

  • It’s easier cause I don’t have to use a getInstance() all the time
  • Cause I can (would you be one of the few still using goto statements)

So try and be careful when using static members. And as always just keep thinking when programming and designing code.

Leave a Reply