Reputation: 3480
Naming convention according to StyleCop for constants is Pascal.
For example
private const double InchToMm= 2.54;
What about naming convention for acronym?
private const int Dpi = 96;
or
private const int DPI = 96;
Upvotes: 1
Views: 5004
Reputation:
If you go your own route, this is somewhat subjective, but hopefully the rationale is not. The easiest UpperCamelCase
standard I've found for teams to follow consistently, if consistency is your primary stylistic goal, is Dpi
. I'm not saying that's my favorite or the most readable or anything like that.
I'm saying it's the easiest standard to apply consistently for a disparate team of people, and even on your own across the years during which you use this convention. The reason for that is that it's fairly robotic and requires little thought. The way to do it is to ask yourself how you would write this in underscore convention. Would you write like this?
d_p_i
... If so, you translate the first of each group of characters separated by an underscore into an upper case. That yields DPI
. But probably almost everyone would write it like this in underscore convention:
dpi
... so the robotic, unambiguous translation from underscore to UpperCamelCase
becomes Dpi
. Same kind of thing if we have:
renderer_gl
... the robotic, unambiguous translation from underscore would be RendererGl
.
Now the benefit of this isn't necessarily that it's pretty or closest to how humans would read and write it. It's just that, provided you can settle on how to write the identifier in underscore (which most people do consistently), even a robot can then convert it to UpperCamelCase
or lowerCamelCase
without any ambiguity.
So if a strong aesthetic among your team is consistency and having a convention where two different people won't be tempted to use different casing, this can be the easiest way to achieve it. In our case consistency was a top goal since we provide a software development kit and we didn't want our API to be inconsistent. For that it helped to establish a no-brainer way of choosing how to use CamelCase
that wouldn't require a lot of thought or involve any form of ambiguity.
With other conventions, often you find a lot of special cases for abbreviations/acronyms. "If it's a common acronym, treat it like a word. If it's not, treat it like an acronym (all caps). If it's 2 letters in length, treat it like an acronym. If it's 3+ letters in length, treat it like a word. If there's an L or M at the end, treat it like a word, otherwise an acronym unless it's followed by a number or preceded by a proper noun. If it doesn't look good, do it the other way." The brain can go kablooey here, and teams can fail hard in being able to apply such standards consistently.
Of course consistency may not be your strongest goal, but if it is, I would suggest this convention.
Upvotes: 2
Reputation: 45101
The Framework Design Guidelines from Microsoft says:
The PascalCasing convention, used for all identifiers except parameter names, capitalizes the first character of each word (including acronyms over two letters in length), as shown in the following examples:
PropertyDescriptor
HtmlTag
A special case is made for two-letter acronyms in which both letters are capitalized, as shown in the following identifier:
IOStream
The camelCasing convention, used only for parameter names, capitalizes the first character of each word except the first word, as shown in the following examples. As the examples also shows, two-letter acronyms that begin a camel-cased identifier are both lowercase:
propertyDescriptor
ioStream
htmlTag
But nevertheless, these are guidelines and conventions and not laws. If you like to take another convention, just use it. But then always and never mix up.
Upvotes: 5
Reputation: 156978
The only StyleCop rule I can find on constants is SA1303. It states:
The name of a constant C# field must begin with an upper-case letter.
So either Dpi
or DPI
would fit that rule.
Microsoft says you should Pascal case it. In my opinion constants should be all uppercase, so I would go for DPI
. Microsoft uses Xml
in namespace names, which should be Pascal cased too, so Dpi
would be most in line with Microsofts own use of naming conventions.
Upvotes: 1