How to use Pascal Casing and Camel Casing for Short Acronyms in C#?

16.5k Views Asked by At

A short acronym is like ID or DB which has only 2 characters.

How to name these when pascal casing (for Property or Class names) and for Camel Casing (input prameters, arguments)?

I know each Company has their own standard but I'm looking for a more generally acceptable standard.

e.g. when pascal casing:

ProductID or ProductId?
IDOfProduct or IdOfProduct?
DBRate or DbRate?
RateOfDB or RateOfDb?

and when camel casing:

productID or productId?
idOfProduct?
dbRate?
rateOfDb or rateOfDB?
4

There are 4 best solutions below

0
On BEST ANSWER

According to MSDN (or Msdn :)

  • Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier.

  • Do capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier.

  • Do not capitalize any of the characters of any acronyms, whatever their length, at the beginning of a camel-cased identifier.

More info here

0
On

Capitalization of ID has been debated a lot with no conclusive answer.

The API design guidelines have the Pascal as Id and the camel as id

https://learn.microsoft.com/dotnet/standard/design-guidelines/capitalization-conventions#capitalizing-compound-words-and-common-terms

ID in the context of software engineering is typically the abbreviation for identifier (as opposed to ID acronym for Identification Document in other, IRL, contexts). As such an abbreviation should indeed follow the casing shown by the Pascal and camel examples of the reference above. However outside of programming context while still in the context of computer science, ID has been broadly used and accepted as the de-facto casing for the abbreviation. The ID capitalization even made it to the Microsoft Writing Style Guide:

https://learn.microsoft.com/style-guide/developer-content/reference-documentation

Which happens to match what the Pascal casing would have been for a two letter acronym (which it isn't!). Thus ID bled back into software variable names and we have the modern mess of lack of consistency.

So as unsatisfying an answer as it is, the casing of ID in Pascal cased variable names does come down to a question of personal, or project, preference. As a rule stickler, my personal preference is Id for code and ID for any user content. But as a collaborator, I'm for whatever the team can agree on.

1
On

Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier.

A property named DBRate is an example of a short acronym (DB) used as the first word of a Pascal-cased identifier. A parameter named ioChannel is an example of a short acronym (IO) used as the first word of a camel-cased identifier.

Check this link.

http://msdn.microsoft.com/en-us/library/ms229043(v=vs.100).aspx

0
On

It doesn't matter.

This is really personal preference. When a short acronym finishes a name, I capitalize both:

testDB

or

TestDB

When a short acronym begins a name, I only capitilze the first because I think it makes it more readable:

dbTest

or

DbTest

It's all about readability. Whatever you choose to do, do it consistently.