Layout Guidelines
Use a common layout (HTA2400)
-
Keep the length of each line under 130 characters.
-
Use an indentation of 4 spaces, and don’t use tabs. The majority of tools use spaces by default and using spaces instead of tabs can be seen as the most common approach in software industry. To set up your VS, please refer to this wiki page (authorized access)
-
Keep one space between keywords like
if
and the expression, but don’t add spaces after(
and before)
such as:if (condition == null)
. -
Add a space around operators like
+
,-
,==
, etc. -
Always put opening and closing curly braces on a new line.
-
Don’t indent object/collection initializers and initialize each property on a new line, so use a format like this:
var dto = new ConsumerDto { Id = 123, Name = "Microsoft", PartnerShip = PartnerShip.Gold, ShoppingCart = { ["VisualStudio"] = 1 } };
-
Don’t indent lambda statement blocks and use a format like this:
methodThatTakesAnAction.Do(source => { // do something like this }
-
Keep expression-bodied-members on one line. Break long lines after the arrow sign, like this:
private string GetLongText => "ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC";
-
Put the entire LINQ statement on one line, or start each keyword at the same indentation, like this:
var query = from product in products where product.Price > 10 select product; or var query = from product in products where product.Price > 10 select product;
-
Start the LINQ statement with all the
from
expressions and don’t interweave them with restrictions. -
Remove redundant parentheses in expressions if they do not clarify precedence. Add parentheses in expressions to avoid non-obvious precedence. For example, in nested conditional expressions:
overruled || (enabled && active)
, bitwise and shift operations:foo | (bar >> size)
. -
Add an empty line between multi-line statements, between multi-line members, after the closing curly braces, between unrelated code blocks, and around the
#region
keyword.
Order and group namespaces according to the company (HTA2402)
// System namespaces come first.
using System;
using System.Collections.Generic;
using System.Xml;
// Then any other namespaces in alphabetic order.
using AvivaSolutions.Business;
using AvivaSolutions.Standard;
using Telerik.WebControls;
using Telerik.Ajax;
Using static directives and using alias directives should be written below regular using directives. Always place these directives at the top of the file, before any namespace declarations (not inside them).
Place members in a well-defined order (HTA2406)
Maintaining a common order allows other team members to find their way in your code more easily. In general, a source file should be readable from top to bottom, as if reading a book, to prevent readers from having to browse up and down through the code file.
- Private fields and constants
- Public constants
- Public static read-only fields
- Factory methods
- Constructors and the finalizer
- Delegates
- Events
- Private Enums
- Public properties
- Private properties
- Interface implementations in regions (see HTA2407)
- Other methods and private properties in calling order
- Private classes
Note: For public classes, enums, interfaces, please refer to rule HTA1507
Declare local functions at the bottom of their containing method bodies (after all executable code).
Be reluctant with #region
(HTA2407)
Regions can be helpful, but can also hide the main purpose of a class. Therefore, use #region
only for interface implementations (only if the interface is not the main purpose of that class).
Use expression-bodied members appropriately (HTA2410)
Favor expression-bodied member syntax over regular member syntax only when:
- the body consists of a single statement and
- the body fits on a single line.