Skip to content

Coding Conventions

André edited this page Mar 21, 2022 · 28 revisions

The coding convention helps create a consistent look for the code. Readers will understand the code more quickly and it facilitates copying, changing and maintaining the code.

Naming Conventions

In this project, we use several naming conventions.

Be aware that protected elements are treated the same way as public elements

Pascal Case

Always use pascal casing ("PascalCasing") when naming classes, structs, properties and methods.

public class MyClass { }

public struct MyStruct { }

public int MyInteger { get; set; }

private void MyMethod() { }

Camel Case

Always use camel casing ("camelCasing") when naming private fields and method parameters.

public class MyClass
{
    private bool myBoolean;

    public void MyMethod(int myParameter) { }
}

Layout Conventions

A good layout uses formatting to emphasize the structure of your code and makes the code easier to read.

  • Use four-character indents and save TABs as spaces.
  • Write only one statement per line.
  • Write only one declaration per line.
  • Continuation lines should be indented one time only.
var myObject = new MyClass(0,
    40.0f,
    "text");
  • Use parentheses to make clauses in an expression apparent, as shown in the following code:
if((value1 > value2) && (value1 > value3))
{
    // Block of code to execute if the condition is true
}

Commenting Conventions

  • Place the comment on a separate line, not at the end of a line of code!
  • Begin comment text with an uppercase letter.
  • Insert one space between the comment delimiter (//) and the comment text, as shown in the following example:
// This is a comment
// This is another comment
// You can also
// break the lines like this
// if you want!
  • Don't create formatted bl9ocks of asterisks around comments.
  • Ensure all public members have the necessary XML comments providing appropriate descriptions about their behaviour. Use <summary>:
/// <summary>
/// This method prints a message in the middle of the screen!
/// </summary>
/// <param name="message">The message</param>
/// <param name="color">Message color</param>
/// <param name="bold">Is the message text in bold?</param>
public void PrintMessage(string message, Color color, bool bold)
{
    // Implementation
}

Language Guidelines

This section describes practices related to C# language.

String Data Type

  • Always use string interpolation to concatenate strings, as shown in the following code snippet:
private string output = $"{name} is {age} year{(age == 1 ? "" : "s")} old.";

Implicitly typed local variables

  • Use implicit typing for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important.
var name = "Gil Vicente";
var age = 21;
var obj = new Object();
  • Don't use var when the type is not apparent from the right side of the assignment. Don't assume the type is clear from a method name. A variable type is considered clear if it's a new operator or an explicit cast.
int amount = MyClass.GetAmount();
MyObject myObject = MyClass.CurrentCache();

Arrays

Use concise syntax when you initialize arrays on the declaration line. In the following example, note that you can't use var instead of string[].

string[] vowels = { "a", "e", "i", "o", "u" };

If you use explicit instantiation, you can use var.

var vowels = new string[] { "a", "e", "i", "o", "u" };

If you specify an array size, you have to initialize the elements one at a time.

var vowels = new string[5];
vowels[0] = "a";
vowels[1] = "e";
// And so on.

new operator

  • Use one of the concise forms of object instantiation, as shown in the following declarations.
var myObject = new MyClass();
MyClass myObject = new();

Are the same as their former

MyClass myObject = new MyClass();

Clone this wiki locally