Skip to main content

A Case Against Uppercase

Posted by evanx on September 28, 2006 at 7:13 AM PDT

Consider the following code.

public class Car {
   private static final int SPEED_LIMIT = 120;
   private int speed;
   public boolean isLegal() {
       if (speed > SPEED_LIMIT) return false;
       return true;

OK, in C our constants were #define macros, and we put them in uppercase.
Because macros are quite distinctive animals, compared to domesticated code.

In Java by convention we put them in uppercase as well. They are
distinguished from other class attributes
by being static and final. Since other attributes might also be static,
and others might also be final, semantically this distinction is weak.

Let's say we decide to make the above class more customisable where
the SPEED_LIMIT is configuable, eg. for different countries.
So we make it not final.

public class Car {
   private static int SPEED_LIMIT = 120;
   private int speed;
   public static void setSpeedLimit(int newSpeedLimit) {
      SPEED_LIMIT = newSpeedLimit;

Of course as soon as we take away the final modifier, we should rename it speedLimit.

The argument i'm trying to illustrate is that we should drop this legacy hangover
of uppercase and underscores for "constants." Because they aren't macros anymore.
They are class attributes with a final modifier, and in my book that's not enough for any special
treatment, such as breaking the clean Java camel-cap naming convention.

To summarise, LOUD UPPERCASE WORDS AND _UNDERSCORES_ SHOULD BE BANISHED, as relics of a bygone era,
don't you think!?