- Keep the length of each line under 150 characters.
- Use an indentation of 4 whitespaces, and don't use tabs
- Keep one whitespace between keywords like if and the expression, but don't add whitespaces after ( and before ) such as:
if (condition == null)
. - Add a whitespace around operators, like +, -, ==, etc.
- Always follow the keywords if, else, do, while, for and foreach with opening and closing curly braces, even though the language does not require it.
- Always put opening and closing curly braces on a new line.
- 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;
// System namespaces are first
using System;
using System.Collections;
using System.XML;
// Then any other namespaces grouped by name
using Softeq.Toolkit.Platform;
using Softeq.MessageBus;
using Telerik.WebControls;
using Telerik.Ajax;
Tip: Visual Studio has organize usings feature that can automate this action. You can find more info here in the organize usings section.
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.
Members must be ordered by type in the following order:
- Constants
- Fields
- Constructors
- Finalizer
- Events
- Properties
- Methods
- Nested types
Then by access modifier:
- Public
- Internal
- Protected
- Private
Then by type/instance modifiers:
- Static
- Instance
Then by assignment modifiers:
- Const
- Readonly
And then by inheritance modifiers:
- Abstract
- Virtual
- Override
- No modifier
Note: members with the same order priority must be placed in call order.
If the class looks too big consider refactoring it into set of classes according to SRP.
If a line of code looks too big consider breaking it. For method definitions and calls put each argument on a new line with a correct indent.
public AzureIotHubCommunicationChannel(
CertificateStorage certificateStorage,
string assignedHub,
string deviceId)
In case of methods chain put each call on a new line:
var converter = JsonSubtypesConverterBuilder
.Of(typeof(IMeasure), "type")
.RegisterSubtype(typeof(Temperature), MeasureType.Temperature)
.RegisterSubtype(typeof(Humidity), MeasureType.Humidity)
.SerializeDiscriminatorProperty()
.Build();