What's That Noise?! [Ian Kallen's Weblog]

« Black Sun to open... | Main | It's the most wonder... »

20040223 Monday February 23, 2004

Constants Abuse Hard-coding string and numeric values into application logic is often problem-plagued. I tcan lead to duplication, which leads to maintenance nightmares, which can lead to increased substance abuse, drinking, crack cocain... it's a treacherous road. Don't go there.

It's a pretty common (and usually, good) practice amongst java programmers to organize all of the little magic values that application logic depends on into a class that defines public constants. However, this can lead to another problem: constants bloat. Cases where the developer needs to define a constant that is only accessed by one component (let's just call a single class and its instances "one component" for the time being) but stores it in a shared centralized constants class is another form of substance abuse. It's constants abuse.

I would really prefer to see constants with a limited scope of applicability confined to that scope. For instance, if it's only used by one class -- define it in that class, not the shared centralized beast. If it's only used by a cluster of classes that work on a particular area, perhaps there's already a bean that they all know about; the constant may be better suited for that bean than a class shared system-wide.

Confine the scope of your constants definitions to where they are useful and save yourself and your colleagues maintenance headaches down the road. ( Feb 23 2004, 10:55:14 AM PST ) Permalink
Comments [1]

Comments:

Every time I see a class named 'Constants' I want to scream. Who on earth ever thought this was a good idea? Look at the freaking JDK. Constants are grouped with relevant classes. Imagine if there was a Constants.java for the JDK?

Posted by Bo on April 19, 2004 at 09:28 PM PDT #

Post a Comment:

Comments are closed for this entry.