views:

135

answers:

7

I'm refactoring a library in C# and I found a lot of upper case constants:

INTERVAL, TIME, SECONDS.

I think this a kind of unnecessary, and personally I prefer declare everything with camel case. Exist some exactly definition about the better way?

+6  A: 

Ultimately the case isn't going to make any difference (unless it collides with a type/keyword/etc). So really consistency is the main thing.

The Capitalization Conventions don't distinguish between constants and other members - but these are guidelines only. So it would be pascal-case.

Marc Gravell
+2  A: 

It's all up to the standards that your group/team chose when they defined their coding guidelines.

If everybody else uses ALL_UPPER_CASE, then you should fall in line.

Personally, I prefer to use upper case for constants just so I know what they are simply by looking at them.

Justin Niessner
I do it for similar reasons. In fact I go one step further by having my local variables start with _ and my constants starting with __ (double underscore). This way when you use Intellisense, a _ gets you to all your local variables and likewise for the constants.
fung
The ALL_UPPERCASE thing goes back to using the C pre-processor to fake up constants. We've come a long way since then...
Steven Sudit
A: 

This is all preference, but the upper case is I believe a throw back to #define because a const isn't a far cry from it as I understand.

More specifically, I believe the upper case const is just another delineation method where you see in a method something in all caps and you know it's a const, if it's camel case it's local, camel case with _ is member private, and pascal case is member public.

Though this is just one standard of consistencies which some prefer, it really is preferential, though I think the reason is as I said, just to make it obvious when you see all caps you know it's a const.

Jimmy Hoffa
+5  A: 

All-caps constants are a common convention…

…but a convention is just that, and is arbitrary. If you are writing code for yourself, there is no compelling reason not to choose your own. If you work with other developers, you must all agree on a naming convention.

If you are writing something that will be consumed by others outside your team, you might do well to stick to the most common and recognizable naming conventions to avoid confusion.

In the end, consistency is what counts.

Jay
Not going to bother downvoting you, but I'd argue that the default would be .NET conventions, which require PascalCase.
Steven Sudit
Exactly! But as I'll refactor the library for my team. I want some convention like @Marc suggested.
Luís Custódio
+1  A: 

Microsoft's recommendations make no mention of all-uppercase names. They do not explicitly specify a casing convention for constants, but they have one for enum values (PascalCase) and read-only static fields (also PascalCase). So according to Microsoft's guidelines, PascalCase is probably your best option.

Thomas
+3  A: 

The MSDN page on constants suggests that constants should be treat like static field members. In this case, the Captialization Conventions would suggest that PascalCasing is appropriate.

If your constant is part of a public API, I would recommend following this convention.

If the constant is just a private member, however, you can use any convention you wish. MSDN, in this case, actually has lower case constant members in the Constant help page, for example.

Reed Copsey
Indeed. You can see this in place with CLR constants such as `System.Int32.MaxValue`.
MiffTheFox
+1  A: 

I generally use PascalCase for public constants and camelCase for private ones. The exception is when the constants are imported from an older C/C++ library or similar (such as those use alongside P/Invoke). - I keep them as they were written in the original library.

Mark H
+1: When the used constant is supposed to "map" to some well-known C/C++ code (where lots of libraries use the upper-case convention) I tend to keep them unchanged too... for clarity
S.C. Madsen