Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't constants all in upper-case in .Net?

Microsoft naming conventions for .Net put constants in Pascal Case. In fact, it explicitly tells us to avoid using all caps for constants:

You might also have to capitalize identifiers to maintain compatibility with existing, unmanaged symbol schemes, where all uppercase characters are often used for enumerations and constant values. In general, these symbols should not be visible outside of the assembly that uses them.

From MSDN.

On SO I found some questions on the subject, like this one, but I couldn't find a rationale. So, anyone know or have a reference that points to why MS chose this convention?

like image 742
Bruno Brant Avatar asked Sep 23 '10 22:09

Bruno Brant


People also ask

Should constants BE ALL CAPS C#?

C# convention is not to use uppercase for constants.

Why are constants in all caps?

Symbolic constant names are commonly written in upper case so they can be readily distinguished from lower case variable names. In many ways, this was a holdover from assembly language, where macros were defined in uppercase along with labels, opcodes, register names and everything else.

Are constants in all caps Java?

Constant Naming ConventionsJava constants should be all UPPERCASE where words are separated by underscore character (“_”). Make sure to use the final modifier with constant variables.

Should constants be capitalized in Python?

In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.


4 Answers

Its just a style guideline. Programming languages have started to recommend and push formatting conventions so that code is more readable.

Also, symbols that get substituted by the preprocessor deserve special attention-- they live outside/before the type system and may not be what they appear to be. Constants are just constants, they won't change at compile or runtime.

like image 85
user318904 Avatar answered Oct 03 '22 23:10

user318904


BECAUSE ALL-UPPERCASE IS UGLY AND HARD TO READ AND IT'S INTENDED FOR THE PRECOMPILER, WHICH .NET DOESN'T EVEN HAVE.

Also, Bill Gates wants it that way, and money is never wrong.

like image 33
Steven Sudit Avatar answered Oct 04 '22 01:10

Steven Sudit


Saying that all caps is ugly and thus should not be used is borderline childish logic. It comes from people complaining about what they are not used to.

Having gone from Java to C, I started using underscores_between_words in variable names and functions. camelCase is extremely ugly to me now. But I still use camelCase for C++/Java without complaining because it is convension.

If you can't make yourself flexible, programming will become a harsh mistress. That is the skill of a programmer.

like image 29
Pizzach Avatar answered Oct 03 '22 23:10

Pizzach


Microsoft doesn't following its own rules because if you reflect over the new System.Threading.Tasks classes in c# 4 YOU_WILL_FIND_LOTS_OF_CAP_CONSTANTS.

Its a style thing. Personally I don't mind. Just be consistent.

like image 30
Bear Monkey Avatar answered Oct 04 '22 00:10

Bear Monkey