Coding Style GuideTopic 3 of 5~5 min

Naming Conventions

Variable Named 'x' Causes Three-Day Debugging Session; Developer Claims 'It Made Sense at the Time'

Illustration of a naming convention museum with exhibits showing good vs bad variable names

Dear Marilyn: I named my variable 'temp' because it's temporary. Now I have 47 variables named 'temp' and I can't tell them apart. Help.

— Temporarily Confused in Tampa

Dear Temporarily: You have discovered, through painful experience, the First Law of Naming: A name should describe what something IS, not what it ISN'T. "Temporary" describes what it isn't (permanent). What IS it? A customer? An order? A database connection? Name it that.

The C# Naming Conventions

ElementConventionExample
ClassesPascalCaseCustomerService
InterfacesIPascalCaseIOrderRepository
MethodsPascalCaseCalculateTotal()
PropertiesPascalCaseFirstName
Local VariablescamelCaseorderTotal
Private Fields_camelCase_customerRepository
ConstantsPascalCaseMaxRetryCount
ParameterscamelCasecustomerId

Names to Avoid

Never Use:

  • temp, tmp, data
  • x, y, z (except in math)
  • foo, bar, baz
  • thing, stuff, item
  • myVariable, theObject

Instead Use:

  • pendingOrder, cachedResult
  • xCoordinate, yPosition
  • testCustomer, mockRepository
  • orderLineItem, configurationSetting
  • currentUser, selectedProduct

Quick Check

What is the correct naming convention for a private field in C#?