Naming and Capitalization Conventions

CodeSmellz_001_Capitalization

A small group of us were doing a code review, and the topic of capitalization and naming conventions came up. I’m very rigid in my ways, and super anal about making sure everything is correct and consistent. Unfortunately, at work we don’t have any official standards documentation to define the style we use. There are some general patterns that are followed, but some of the more controversial topics–like use of var–are left to the preference of the developer. I knew that MSDN had published naming guidelines, so I dug them up to present to the group. Now when we review code, we can nitpick names and capitalization using the argument that “our standard is to follow Microsoft’s guidance.” That’s good with me!

The specific topic that got us started down this path was capitalization for acronyms, and Microsoft offers three rules for dealing with them:

  1. Do capitalize both characters of two-character acronyms, except the first word of a camel-cased identifier. (e.g., DBRate, ioChannel)
  2. Do capitalize only the first character of acronyms with three or more characters, except the first word of a camel-cased identifier. (e.g., XmlWriter, htmlReader)
  3. Do not capitalize any of the characters of any acronyms, whatever their length, at the beginning of a camel-cased identifier. (e.g., xmlStream, dbServerName)

Ahh, just how I like it.

An additional distinction is made for abbreviations. It advises that, generally, abbreviations should not be used in library names. Two exceptions are noted, though: ID and OK. These are acceptable to use in an identifier name and should follow the same casing rules as regular words. In other words, Id and Ok for Pascal-case and id and ok for camel-case.

While we’re on the topic of capitalization, here’s a list of conventions that I follow in terms of Pascal-case versus camel-case. Violating these rules is a good way to irritate me

  • Class: Pascal
  • Property: Pascal
  • Parameter: Camel
  • Private instance field: Camel, prefixed with “_” (no official guidance; I’m flexible on this one)
  • Public/internal/protected instance field: N/A (use Property instead; read more)
  • Event: Pascal
  • Local variable: Camel
  • Enum types and values: Pascal

What to read more? (I know; I love this stuff, too!) Check out the complete guidance here.

Advertisements

One thought on “Naming and Capitalization Conventions”

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s