CodeSOD: Constantly Named

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 to
private 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.

[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published. Required fields are marked *