Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming local constants: UpperCamelCase or lowerCamelCase?

Which naming convention do you use for local constants in C# and why?

const int Pi = 3;
const int pi = 3;

It seems the trade-off is between lower camel-case indicating restricted scope, and upper camel-case being more readable and easier to move to a class level. I've noticed StyleCop prefers upper camel-case.

like image 585
Anthony Faull Avatar asked Jul 01 '10 11:07

Anthony Faull


People also ask

What is the recommended naming standard for constants?

Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.

Should constants be capitalized?

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)

Does C use CamelCase or Snakecase?

Classic C doesn't use camel-case; I've written code in camel-case in C, and it looks weird (so I don't do it like that any more). That said, it isn't wrong - and consistency is more important than which convention is used.

Should constants be capitalized C++?

I know, that for C++ and Java it is a well established naming convention, that constants should be written all uppercase, with underscores to separate words. Like this (Java-example): public final static Color BACKGROUND_COLOR = Color.


2 Answers

We use lower-case (camel casing) because local constants are almost local variables except of course you cannot modify them. (And we use camel casing for local variables of course...)

like image 174
Daniel Brückner Avatar answered Sep 20 '22 21:09

Daniel Brückner


I'm used to upper case (pascal case) for everything except variables and fields. Global constants are an exception to the fields, I don't know why, probably because they are public in some cases. Local constants are also lowercase so.

It's just a matter of taste imo. Of course, within a product / team, there should be an agreement.

On the other hand, our coding guideline requires full uppercase for constants, this would be PI in this case. I don't like this because upper cases are hard to read and need underlines for separation (which is against code analysis rules). Nobody follows this guideline anymore.

like image 37
Stefan Steinegger Avatar answered Sep 21 '22 21:09

Stefan Steinegger