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;
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;
In C++ at least, you should avoid capitalizing constants to avoid confusion with #defines, which are canonically capitalized.
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.
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.
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.
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.
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.
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.
I prefer constants to be all caps unless there is a reason not to.
The IDesign C# coding standard recommends Pascal case for constants.
Also, check out this related question.
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.
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.