Article 6AP2M CodeSOD: Constantly Named

CodeSOD: Constantly Named

by
Remy Porter
from The Daily WTF on (#6AP2M)

We've seen constants in the form of static final int ONE = 1 before. At this point, it's barely worth discussing, it's become so common. But Susan has found us yet another unique twist on the subject.

It started when a contractor wrote Java code that looked like this:

String formattedField = fieldFormatMethod(someArray[14]);

The linter complained about the bare literal, so the contractor dutifully replaced it with a constant:

String formattedField = fieldFormatMethod(someArray[XXX_FOURTEEN]); 

So much better". But note the XXX prefix there. That's anonymized in this case, but it is a hint to what's coming. The contractors wanted to make sure they were compartmentalizing their constants, namespacing them properly", so guess what happened next?

private static final int XXX_ONE = 1;private static final int XXX_TWO = 2;//etc toprivate static final int XXX_THIRTY_FIVE = 35;// then...private static final int YYY_ONE = 1;private static final int YYY_TWO = 2;

Yes, they recreated the same constants out of literal values, but with different prefixes to distinguish where they were used. You can see that these are all declared private. Which means they're all declared in the same class, and yes- it's a gigantic do everything" class with many thousands of lines in it, and only a small percentage of those lines are badly named constants.

otter-icon.png [Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments