On Wed, 20 Aug 2003, Mark Chaimungkalanont wrote: > Date: Wed, 20 Aug 2003 15:04:46 +1000 > From: Mark Chaimungkalanont <[EMAIL PROTECTED]> > Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]> > To: Struts Users Mailing List <[EMAIL PROTECTED]> > Cc: [EMAIL PROTECTED] > Subject: [OT] Static variables are evil? (was Question about Servlets & > Threads) > > > > > > Craig, > > Just wanted to pick up on the comment: > > "It's fair to warn you, though, that object oriented purists will frown on > anything like this, because static variables (similar to globals in PHP) > are the root of all sorts of program bugs :-). Explicitly passing things > around creates *much* safer code." > > What sorts of problems do static variables cause?
Static variables are accessible from anywhere. Therefore, they are updatable from anywhere -- and, if some arbitrary code scribbles on your global variable, it can be very hard to find the culprit. > Does this also mean that > you should avoid GlobalConstants type classes as well? Global constants are not the same sort of problem, because they cannot be modified. > > On a related note, what're some good sources for such info related how best > to OO your code? Is it just a matter of sticking to design patterns like a > glue? > Well, that's a good start. There are many good design patterns books around -- for Java, I would pay particular attention to the kinds of patterns that people like Josh Block and Martin Fowler recommend, but they are by no means the only good role models. The other thing to think about, though ... I'm not sure that "OO your code" is something you can effectively do after the fact. It seems to me that this is much more an issue that needs to be addressed in the initial architecture. The things people love about scripting languages (including PHP and JavaScript) -- polymorphic variables, globals as the normal way of sharing information, relatively little emphasis on the concept of an "object", intermixed business logic and presentation -- tend to be reflected in the code that gets written in these languages. It's definitely possible to write O-O oriented code in PHP, for example, but it seems to be rare. If you are looking at migrating from such a platform to Java, you might want to seriously consider a rewrite rather than a migration. It's not always the best answer, but if you're going to have to maintain a significant application for a long time, you want the features of a language like Java helping you, not fighting you. > cheers > > MC > Craig --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]