views:

452

answers:

13

What do you guys think? Should constants be capitalized? Or is that an archaic practice? So...

const int MY_CONSTANT = 5;

vs.

const int myConstant = 5;
+2  A: 

In C++ at least, you should avoid capitalizing constants to avoid confusion with #defines, which are canonically capitalized.

Kluge
A: 

That's the official and de facto standard for naming constants in the Java language, so unless you consider Java an archaic language, I guess the answer is no.

Don
So because Java took something from C/C++ over a decade ago, that means we need to continue the practice?
Tim Merrifield
I didn't suggest you should continue the practice, I merely pointed out that this convention is still in widespread use and therefore cannot be considered arcane.
Don
+8  A: 

You should do what is the convention in the language/platform you are using. For example in .NET constants should be pascal cased, e.g.

SomeConstant

In other languages it is likely to be different.

Greg Beech
A: 

What language?

No, but you should put them in a class or a namespace.

+1  A: 

If you're adding to or maintaining an existing code base, do what they do to keep from confusing people. If this is a new project, settle on a convention with your team and do that.

If it's your own project, do what you're used to.

Jeffrey Martinez
+1  A: 

Actually, this may not be language-agnostic. Some languages have a tradition of using capitals, while others do not. For example, in C++ uppercase is usually for enums and defines only, while Java uses uppercase more liberally.

Marcin
+1  A: 

Personal feeling is that naming conventions should mean something, and are good as long as they provide meaning. Other than that best is to follow conventions of the language you're working in. Otherwise instead of simplifying for the reader, you're (mildly) obfuscating. For example, in java

THIS_CONSTANT

is a common way to write static finals. Other languages have different styles.

Steve B.
A: 

I don't capitalize my constants. In fact, I use 'const' so proactively that it doesn't make sense to capitalize them. For example:

const size_t size = v.size();
for( size_t i = 0; i < size; ++i ) ...

This usage of 'const' may seem useless, but it gives confidence that 'size' is a snapshot of that vector's size at that point in time. The compiler will make sure that this is true for the entire scope.

I do, however, capitalize my MACROS. But, using #define to declare constants is a different topic.

Martin Cote
A: 

I capitalize and use underscore for .Net, Java and PHP.

Daok
A: 

I prefer constants to be all caps unless there is a reason not to.

Paul Nathan
A: 

The IDesign C# coding standard recommends Pascal case for constants.

Also, check out this related question.

cxfx
+2  A: 

The all-caps convention was invented for macros, where there was good reason to be shouting, "Look out! A macro!" (There are lots of ways to mess up.) A language-level constant is a tamer beast, but the convention persisted.

Use your judgment; where there's a strong convention, like I suppose in the Java world, it's a waste of energy to fight it.

Darius Bacon
+2  A: 

Effective Java recommends you us capitals and underscores for static final values that are immutable. Otherwise, they are not constant.

i.e.

public static final String  ID_FOO_BAR = "org.acme.foo.bar";
public static final List<String> sList = new List<String>();
public static final List<String> LIST = Collections.unmodifiableList(sList); 
public static final String[] sArray = new String[] {"foo", "bar", "baz" }; // FindBugs complains, if this is public.
public static final List<String> ARRAY = Arrays.asList(sArray);

Here's the quote:

By convention, such fields have names consisting of capital letters, with words separated by underscores. It is critical that these fields contain either primitive values or references to immutable objects.

... otherwise, if mutable objects look like constants, they get confused as such.

jamesh