views:

196

answers:

9

Is it a good OOP practice to have static variables to store global, changing information needed by different classes?

as opposed to passing parameters around so that it can be accessed by the called classes.

+4  A: 

It's not generally a good idea, no... it can definitely simplify some things, but it makes testing harder (and means you can't run tests in parallel, for example).

Some aspects such as logging are typically implemented like this, but I would tend to try not to do it. Dependency injection makes life much simpler in terms of testing. (It can become painful when you need to pass a dependency to class Foo just for that to pass it to Bar, which then passes it to Baz etc. I think we're still not quite "there" in terms of dependency injection. I think something more advanced around scoping/lifecycle would be useful as part of the language, but we'll see... I can't see it happening in C# itself, mind you.)

Jon Skeet
A: 

Combining them in some sorts of Properties classes and implementing them as Singletons would be an acceptable OOP practice.

Grozz
No it wouldn't - see other answers for details.
Péter Török
debatable since singletons often define good OODhttp://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial
Rune FS
@Rune, the source you are quoting seems to be saying the opposite of what you say.
Péter Török
@Grozz, I like to write unit tests, so to me it does... YMMV. The point is not having a single instance of e.g. a configuration class, but how you access it. You can pass around that single instance using dependency injection as much as you like, that won't create problems the way a single point of access does.
Péter Török
@Peter yes but only because of a funny typo I made. It should say defy _not_ define. Singleton is in my book an antipattern
Rune FS
@grozz no files are not singletons they are a file and you Can copy them and assign to them (e.g. Override Them with a differrent file) and even if you couldn't do All that they still wouldn't be a singleton since that's a design (anti)pattern and as such can't be found outside of code
Rune FS
@grozz, btw, I didn't downvote you, so in case you are angry with me because you got downvoted (the tone of your comment seems to suggest this), your anger is misdirected.
Péter Török
nah, I apologise if it sounded like that.
Grozz
+2  A: 

static variable are generally used to represent fixed value with final like

public final static String JANUARY="january";
org.life.java
+4  A: 

Apparently you mean Singletons. The answer is: it is not a good idea in general, because it creates difficult to follow dependencies within your code. This in turn makes your code hard to understand, maintain and extend. What's more, it makes unit testing difficult.

If a method is using global objects, you have no way of knowing it other than looking at the source code. However, if the method uses only its parameters and class members, you understand its dependencies by looking at its signature and the enclosing class' definition.

Setting up unit tests for a method which uses global objects is much more difficult than for "normal" methods. Also, there is a risk that someone forgets to reset global state after each test, which results in global state flowing over to other unit tests. This makes your tests secretly depend on execution order, which can produce strange test results.

Péter Török
Would the downvoter care to explain why?
Péter Török
I too would like to see a reason for downvoting what I personally think deserves the checkmark
Rune FS
don't know who downvoted, but i upvoted it in order to compensate :P
Louis Rhys
A: 

Who owns the information? usually it would be best to wrap the information into the class that owns it, and have it accessed through static functions of that class, I believe.

This also makes it easier to address the issue of multi-threaded access, as you can put the synchrionisation mechanisms into those functions to control access.

Ragster
A: 

The use of static variables introduces complexity to multi-threaded applications and so is generally not considered to be a good idea. Wrapping such data in singleton classes allows you to setup a suitable locking mechanism to govern access to the shared data.

John Pickup
A: 

I think better practice is to use *.properties files which contains all configurable parameters. Also you can implement singletone-like class *Configurator which will load all parameters into your application.

There is a lot of open source libraries which can help you manage application parameters. In case of Java: http://commons.apache.org/configuration/index.html

Konoplianko
A: 

No, try to have as much as loose coupling as possible. If you want your code to perform as individually cohesive entities, then sharing global information using static variables is highly not recommended. Yet, you can constant values with them. But then, Enums are there in Java. So, I would recommend not to use them.

Bragboy
+1  A: 

As per my observation, since the static members are initialized only once, there will be no over head of re-allocation of memory, each time the member is accessed.

However it won't be a good OOP.

The danger with this approach sometimes is, you may get some unexpected results if somebody is trying to access and modify the properties in wrong.

But for private methods or the metods called from several places for the purpose of same functionality, it will be a good practice to keep the static methods, and the same is advised by FxCop too.

Siva Gopal