Repository navigation
Coding Guidelines
Gordon Breuer edited this page Mar 24, 2023
·
1 revision
The general rule we follow is "use Visual Studio defaults".
- We use Allman style braces, where each brace begins on a new line. A single line statement block can go without braces, but the block must be properly indented on its own line and must not be nested in other statement blocks that use braces (See rule 18 for more details). One exception is that a
usingstatement is permitted to be nested within anotherusingstatement by starting on the following line at the same indentation level, even if the nestedusingcontains a controlled block. - We use two spaces of indentation (no tabs).
- We use
_camelCasefor internal and private fields and usereadonlywhere possible. Prefix internal and private instance fields with_, static fields withs_and thread static fields witht_. When used on static fields,readonlyshould come afterstatic(e.g.static readonlynotreadonly static). Public fields should be used sparingly and should use PascalCasing with no prefix when used. - We avoid
this.unless necessary. - We always specify the visibility, even if it's the default (e.g.
private string _foonotstring _foo). Visibility should be the first modifier (e.g.public abstractnotabstract public). - Namespace imports should be specified at the top of the file, outside of
namespacedeclarations, and should be sorted alphabetically, apart fromSystem.*namespaces, which are to be placed on top of all others. - Avoid more than one empty line at any time. For example, do not have two blank lines between members of a type.
- Avoid spurious free spaces.
For example, avoid
if (someVar == 0)..., where the dots mark the spurious free spaces. Consider enabling "View White Space (Ctrl+R, Ctrl+W)" or "Edit -> Advanced -> View White Space" if using Visual Studio to aid detection. - If a file happens to differ in style from these guidelines (e.g. private members are named
m_memberrather than_member), the existing style in that file takes precedence. - We only use
varwhen the type is explicitly named on the right-hand side, typically due to eithernewor an explicit cast, e.g.var stream = new FileStream(...)notvar stream = OpenStandardInput().- Similarly, target-typed
new()can only be used when the type is explicitly named on the left-hand side, in a variable definition statement or a field definition statement. e.g.FileStream stream = new(...);, but notstream = new(...);(where the type was specified on a previous line).
- Similarly, target-typed
- We use language keywords instead of BCL types (e.g.
int, string, floatinstead ofInt32, String, Single, etc) for both type references as well as method calls (e.g.int.Parseinstead ofInt32.Parse). - We use PascalCasing to name all our constant local variables and fields. The only exception is for interop code where the constant value should exactly match the name and value of the code you are calling via interop.
- We use PascalCasing for all method names, including local functions.
- We use
nameof(...)instead of"..."whenever possible and relevant. - Fields should be specified at the top within type declarations.
- When including non-ASCII characters in the source code use Unicode escape sequences (\uXXXX) instead of literal characters. Literal non-ASCII characters occasionally get garbled by a tool or editor.
- When using labels (for goto), indent the label one less than the current indentation.
- When using a single statement if, we follow these conventions:
- Never use single-line form (for example:
if (source == null) throw new ArgumentNullException("source");) - Using braces is always accepted and required if any block of an
if/else if/.../elsecompound statement uses braces or if a single statement body spans multiple lines. - Braces may be omitted only if the body of every block associated with an
if/else if/.../elsecompound statement is placed on a single line.
- Never use single-line form (for example:
- Make all internal and private types static or sealed unless derivation from them is required. As with any implementation detail, they can be changed if/when derivation is required in the future.
This document is based off on the official Microsoft Coding Styles for .NET
Honeycomb Discord Bot © 2023 Gordon Breuer. All rights reserved.
For inquiries or support, please email us at support@honeycombs.cloud.
Last updated: March 24, 2023