Editor settings

  • Insert spaces (not tabs)
  • Tab size: 4
  • Indent size: 4

Member naming

  • Type names (classes, structs, interfaces, delegates, enums) should use PascalCase
  • Public, protected, and internal method, field, event, property, and constant names should use PascalCase
  • Private method, event, and property names should use PascalCase
  • Private instance field names should use _camelCaseWithLeadingUnderscore
  • Private static field names should use s_camelCaseWithLeadingCAndUnderscore
  • Private constant names should use c_camelCaseWithLeadingCAndUnderscore
  • Local variable and parameter names should use camelCase
  • Method names should start with verbs: public void ProcessResult()
  • Interface names should start with an "I"
  • Type names should be nouns: public class Processor, public interface IProcessor, public enum Color, delegate EventHandler();

Code formatting

Braces

  • In general, opening and closing braces are always on a separate line:
class C
{
    public void Method()
    {
        if (condition)
        {
        }
    }
}
  • Automatic property declarations can be one-liners:
public int Property { get; set; }
  • The getter and setter of non-automatic property declarations can be one-liners if they contain a single statement:
public int Property 
{ 
    get { return _property; }
    set { property = value; }
}

Spacing

  • Space between operators: 3 + 4 * 7
  • Space between method and non-empty parameter list: Method (foo)
  • Space between parameters: Method (foo, bar), void Method (int foo, string bar)
  • Space between keywords and following parentheses: if (condition)
  • Spaces around the arrow in lambda declarations: (x, y) => x + y

Readable code

  • Try to keep one (complex) statement per line:
var solution1 = GetSolution (1, 2, 3);
var solution2 = GetSolution (4, 5, 6);
return Transform (solution1, solution2);

// Not: return Transform (GetSolution (1, 2, 3), GetSolution (4, 5, 6))
  • When in doubt about the complexity, choose intermediate bloal variables (such as solution1 and solution2 above),

Last edited Nov 24, 2011 at 11:43 AM by FabianSchmied, version 1

Comments

No comments yet.