Skip to content

C# Coding Standard

Kieren102107262 edited this page Sep 4, 2018 · 17 revisions

The following is a list of preferred standards to follow when coding the Battleship game:

- Variable definitions should follow the format as follows when defined as private fields that are not temporary:

private string _shipName = "Submarine";

- Comments should be written as follows:

// This is a comment.
/*
This is a block comment.
It has a lot of text!... or at least it can have.
*/

- Methods should be formatted as follows, and name capitalisation should follow as well:

public Ship( string shipName, int shipSize )
{
    // Set the ship's name and size.
    _shipName = shipName;
    _shipSize = shipSize;
}

- When writing if statements, format as follows:

if (_ship.IsAt(64,64) && variable > 1)
{
    // Do something
}

- For loops are ideally done as follows:

int i;
for (i = 0; i < 50; i ++)
{
    // Do something
}

- Avoid making lines too long where viable, especially with comments. Use block comments if single line comments won’t fit:

/*
It's totally ok to do something like this if you have something that needs
a whole lot of explaining for it to make any sense. Spaghetti code happens
sometimes, and it just needs a good explanation for someone else
to fix it properly.
*/

- All in all, a sample for a simple object class would be as follows:

public class Writer
{
    private string _prefix = "Not ";
    public Writer(string textToWrite)
    {
        // Example output: "Not an apple"
        Console.WriteLine(_prefix + textToWrite);
    }
}

Clone this wiki locally