Wednesday, November 23, 2011

Variable Naming Conventions

The benefits of naming conventions in software code are generally recognized while specific "conventions" are debatable. Personally I'm looking for the implied intent and meaning of the source code, code that reads as a narrative. Here are the heuristics that I use (when not given a coding standard.)
  • Compound identifiers, containing more that one word
  • CamelCase the compound identifiers
  • Start with a letter; [a-z,A-Z]
  • Use only letters and numbers, no other chars like -_+~!. (Under_score is possible exception.)
  • Mnemonic names built from a (informal) dictionary of shorthand strings.
  • Start function identifiers with a cmdlet verb (Powershell convention).

Since I am currently developing in a .NET world the Microsoft recommendations also apply.

  • UpperCamelCase for most identifiers.
  • lowerCamelCase for parameters and variables.
  • No type prefix hints.
  • End the identifier with a base class name (optional).
And then there are the PowershellCommunity.org recommendations.
  • ScriptAndGlobalFunctionNamesShouldFollowCmdletNamingStandards
  • VerbsShouldBeSelectedFromStandardList
  • ScriptAndGlobalFunctionNamesShouldBePascalCased
  • CamelCaseVariableNames
  • NounNamesShouldBeSpecific
  • NounNamesShouldBeSingular
  • ScriptsIntendedToBeDotSourcedShouldUseLibraryPrefix
  • ProvideDescriptiveVariableNames
  • AvoidHungarianNotation

Sail to other shores with Coding Guidelines (PDF), naming conventions of OBO Foundry, Hungarian Notation pros/cons, and Powershell best practices.

No comments: