Don't give me static...

Justin Harris jharris at redhat.com
Tue Jun 1 20:16:26 UTC 2010


I was just burned pretty bad by a subtle bug and want to share some general thoughts with folks.  The core of the issue that I had was that the Config class (which is not static) held a static map that represents the internal state of the object.  Additionally, this static map was only initialized upon the first creation of a Config object.  This doesn't really cause much harm in a deployed environment, but it caused a lot of pain in our unit test suite -- specifically why one of the tests would pass when run by itself, and yet failed when run in the larger test suite.

So I think that the lesson learned is:  Don't use static anything.  Or, at the very least, treat static classes/variables as a code smell, and really try to justify it before throwing it in.  One of the really nice aspects of Guice is its ability to abstract away how the dependencies of a component are created, and I think we should defer to the framework for e.g. creating objects as singletons for optimization purposes.

What do you guys think?  Am I overreacting?  :)

 - Justin



More information about the candlepin mailing list