Tuesday, April 29, 2014

when to use underscores in C# as a naming convention

Just about everything in C# should be named in Pascal Case. (Example: MyDogHasFleas) There are three exceptions:

  1. Private variables that exist only within a loop or a method (almost all private variables) should be in Camel Case. (Example: myDogHasFleas)
  2. The exception to this exception is that private fields, that is controller or page wide variables that hold state from method to method, should be in Camel Case with a leading underscore. (Example: _myDogHasFleas)
  3. Strictly speaking one should never use underscores in names in any scenario other than the one above. One should never have underscores in the middle of name. That said, when I worked at AMD, I liked Joel Holder's convention of using snake case for the names of tests. This is a Ruby convention that he drug in. (Example: my_dog_has_fleas)

No comments:

Post a Comment