Coding Guidelines

The most important rule when contributing to a source code base – any source code, not just an open source project – is to keep the style consistent. To enable that consistency, our command line build runs both FxCop and StyleCop to attempt to catch both common programmer errors as well as to enforce a common style. Please observe the general rule to leave all the code looking consistent: that includes new files being consistent with the existing style. Any pull requests which include major reformatting of the source code or deviate dramatically from the normal style will be declined and asked to resubmit once they’re amended.

Definitions

  • Camel case is a casing convention where the first letter is lower-case, words are not separated by any character but subsequent words have their first letters capitalized. Example: thisIsCamelCased .
  • Pascal case is a casing convention where the first letter of each word is capitalized, and no separating character is included between words. Example: ThisIsPascalCased .

C# coding conventions

In general, we have striven to use a style which is compatible with the out-of-the-box defaults for Visual Studio. In particular, this means we use Allman bracing style.

Some other rules we follow:
  • Private fields are camel-cased without a prefix.
  • Using statements go at the top of the file, sorted alphabetically.
  • Use of regions (#region) is strongly discouraged.
  • Using var is okay, if and only if the type is obvious (namely, for “new” statements, cast statements, or anonymous objects).
  • All production code must have associated unit tests.
  • All new production code should have at least rudimentary XML doc comments.

Examples of var usage

// This is ok
var tuple = new { Name = >"John", Age = 50 }; 

// This is ok
var stream = new MemoryStream();

// This is ok
var product = (Product)GetProduct();

// This is NOT ok
var values = GetProducts();

Example of the source code style

using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Practices
{
    public class ClassName
    {
        private List<SomeType> privateMember;

        public List<SomeType> ShortProperty
        {
            get { return privateMember; }
        }

        public string AutoProperty { get; set; }

        public int ComplexProperty
        {
            get
            {
                if (someCondition)
                {
                    return 42;
                }
                else
                {
                    return 2112;
                }
            }
        }

        public string SomeMethod(Status status)
        {
            switch (status)
            {
                case Status.Strange:
                    return "It was a Strange";

                case Status.Charm:
                    return "Turns out it was a Charm";

                default:
                    return "I’m honestly not sure what it was";
            }
        }
    }
}

Last edited Nov 21, 2013 at 3:26 PM by bennage, version 1

Comments

No comments yet.