Kevin Greer's Stuff
[ start | index | login ]
start > 2005-02-16 > 1

2005-02-16 #1

Created by kgr. Last edited by kgr, 3 years and 239 days ago. Viewed 181 times. #1
[edit] [rdf]
labels
attachments

Statics: Foe!

From >>Cedric's "Static: friend or foe?"

Dave,

An 'else' isn't going to help you if you run two or more instances at once. In that case everyone will share the verbosity level of whoever set m_verbose last.

Statics values do have a purpose: they often afford short-term convenience, but at the cost of loosing re-usability and re-composability. Once you use a static you restrict yourself to only having a single instance of that item or whatever else depends on it.

Fortunately in Java statics really aren't global to the JVM but only to the ClassLoader so you can work around this problem by using multiple class-loaders. It would be better however if you didn't have to. Inversion of Control or Environmental Acquisition systems are the solution (Java even has one built in the java.beancontext package but it is a bit verbose and easier solutions are available).

The software industry has long hoped to establish a level of software re-use through componentization, much like the electronics and other industries. Unfortunately software components haven't succeeded as expected. The reason for this failure is the simple static or global variable. Only in software do individual components have a “magical” connection to this shared pool of global or static information or services. In all other fields, components, be it CPU or carburetor, only have access to what they’re explicitly connected to. Because of this limitation they are guaranteed to be re-usable and re-composable. I can use a given CPU in a computer or a DVD player and I can put two tail pipes on a car if I want. Software on the other hand has all sorts of artificial restrictions. Try to instantiate two instances of your favourite ORB (because you want to be configured with different hostnames of socket layers on different networks for example), or database driver or logging package. Even having one part of your application use a different System.out than the other can be a problem. Software components which rely on globals being one way or another are potentially excluding other software from running (especially another instance of themselves). Globals/Statics are holding back the entire software industry (or rather, the people who use them are); and that’s why I classify them as “evil”.

Regards,

KGR

Icon-Comment kgr, 3 years and 170 days ago. Icon-Permalink

Please login to post a comment.
peerbox.com | Copyright 2005-2006 Kevin G. R. Greer