Skip to content

Instantly share code, notes, and snippets.

@Peaj
Created January 31, 2023 12:14
Show Gist options
  • Select an option

  • Save Peaj/34c93b4af5e95f8c05883b2eaeb0e9ff to your computer and use it in GitHub Desktop.

Select an option

Save Peaj/34c93b4af5e95f8c05883b2eaeb0e9ff to your computer and use it in GitHub Desktop.
Coding Conventions.md

This document defines a standard for code structure and format as well as patterns for Unity projects.

Simplicity

Favor clarity and readability over optimization. Optimization should only happen once the need is confirmed.

Documentation

The C# XMLdoc standard is used as it is readable by various IDEs as well as documentation generators. Use of the C# XML Documentation Comments plugin is recommended

Every class must have a summary. E.g.:

/// <summary>
/// This class handles all character movement
/// </summary>

Methods must also use XML documentation as long as it is not self-explanatory. E.g.:

/// <summary>
/// Retrieves the player with the provided uid (incremental database
/// counter) from …
/// </summary>
/// <returns>The player or null if not found</returns>
/// <param name="databaseUid">Internal unique id in the database.</param>
public Player GetPlayerByDatabaseUid(long databaseUid)

Class Layout

  1. nested types (enums, classes, etc.).
  2. static
    1. fields
    2. properties
    3. methods
  3. fields
  4. properties
  5. constructor
  6. Unity messages (Awake, Start, etc.)
  7. methods

Within each group the public elements should be at the top and the private ones at the bottom.

Naming Convention

  • Classes, methods and public fields/properties are named in pascal case (MyAwesomeClass)
  • Interfaces are prefixed with an I (IMyAwesomeInterface)
  • Private fields, properties and local variables are named in camel case (myPrivateMethod)
  • Do not use Hungarian notation or similar prefixes (e.g. bFlag or m_length)
  • Enums should be named in plural form if possible (e.g. PlayerTypes, Directions)

Variable Access

Fields should always be accessed via this this.counter;

Code Format

Brackets

  • Each curly bracket should have its own line.
  • Brackets are required to open and end on the same level as their counterpart:
private bool MyFunction(int a) 
{
    if (a == 1)
    {
        Debug.Log(Test);
        return true;
    }
    else
    {
        MyMethod();
        return false;
    }   
}

If Statements

  • If statements must use the same bracket style as methods.
  • If the statements are short they should be written in one line:
if(a == b) return true;
  • Do not use line breaks without brackets like this:
if(a == b)
    return true;

Null checks

  • Use explicit null checks:
if(temp == null)
  • Do not use implicit null checks:
if(temp)

Unity methods

  • Awake
    • Initialize the component
    • Find all required components (GetComponent)
    • Avoid calling methods or changing state of other components
  • Start
    • Interact with other components here
  • OnEnable
    • Register events and deleagates
  • OnDisable
    • Deregister events and delegates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment