Static: The Dark Side of Design
Developer's Cave October 3rd. 2008, 9:42amStudents of software must be taught early the benefits of object oriented design, lest they succumb to the dark side of static programming. When designing a software component, the temptation can be to write globally accessible public static methods in lieu of instantiable objects. Even experienced developers must remain diligent to avoid being turned to this quicker, more seductive, approach.
The static keyword (in Java) is an important language feature, but can be easily abused, leading to several problems.
One problem is static cling syndrome: Static methods beget static methods. Once you start coding with global access methods, other parts of the system tend toward static access. Eventually, the design degenerates into unmaintainable spaghetti code.
Another drawback is initialization. A static system that requires an initialization method to be called is error prone. Consider Java’s class loading system, which may trigger static method calls just by accessing a class.
Purely static code may also break other software features, such as modular component design. The static API can be called from any object in any module at any time with no access control or coordination. In some cases, this type of global access is desirable. For example, a fully instance-based (non-static) logging system would require passing the logging object throughout the code, permeating every class with its presence.
If your code has given in to the dark side of static design, do not despair; there may still be some good left in it, and you may be able to save the design before it destroys itself. Attempt to reform the design by rewriting it with instance-based objects. This is your library implementation. If global access is needed (as with the logging example), follow the singleton pattern and provide static access to a single instance of the system.
Do not underestimate the power of an object design. Even though it still involves global static access, the singleton provides several benefits. For one, you can eliminate static cling by implementing helper classes as objects. Premature access problems are eliminated (or at least exposed), since there is no singleton instance to call until proper initialization is performed. The singleton pattern tempers the dark side, and is the tool of a more civilized design.
You might tell yourself that you’re not afraid of the dangers of a static design. Once you are faced with debugging an evil static system, only to find that your adversary is your very own code staring back at you, you will be; you will be…






October 3rd, 2008 at 11:24 am
By the way, although object oriented design is well established, I felt obligated to write this article because I just spent several days cleaning up a static API of my own.
October 3rd, 2008 at 11:37 am
You forgot to mention the Dryer Sheet of Refactoring.
October 3rd, 2008 at 12:05 pm
Indeed, I couldn’t think of a pun for eliminating static cling. That’s exactly what I needed. Good one.
By the way, the alpha testing server is down while I consider an incremental release, including some new eye candy.