Skip to content

Coding Guidelines

Rowan Miller edited this page Jul 26, 2016 · 1 revision

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.

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 have their first letter 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 strived 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 prefixed with an underscore and camel-cased.
  • Don't use this.
  • Always include curly braces around blocks, even when they would be optional.
  • Using statements go at the top of the file, inside the namespace declaration.
  • Prefer to use var.
  • All production code must have corresponding tests. We use two primary styles of test: Unit and Functional - Unit tests test a single class in isolation while Functional test features end-to-end.
  • All new public surface code should have XML doc comments.
  • Indentation: 4 spaces.
  • Constants should be Pascal cased.
  • Lines exceeding 120 columns should be broken.

Example of the source code style

namespace System.Sample
{
    using System;

    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.Foo:
                    return "It was a Foo";

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

                default:
                    return "I'm honestly not sure what it was";
            }
        }
    }
}
Clone this wiki locally