Skip to content

Code Conventions

WarlockGames12 edited this page Mar 20, 2024 · 26 revisions

Casing

Camel case

Also known as camel caps, camel case is the practice of writing phrases without spaces or punctuation, separating words with a single capitalized letter. The very first letter is in lowercase.

We write all variable names in camel case. For example:

examplePlayerController

maxHealthPoints

endOfFile

Pascal case

Pascal case is a variation of camel case, where the initial letter is capitalized. Use this for class and function names.

TakeDamage

OnPlayerMove

EndOfFile

Fields and variables table

Variables and Fields

Consider these rules for your variables and fields:

Use nouns for variable names: Variable names should be clear and descriptive because they represent a specific thing or state. Use a noun when naming them except when the variable is of the type bool (see below).

Prefix Booleans with a verb: These variables indicate a true or false value. Often they are the answer to a question, such as: Is the player running? Is the game over?

  • Prefix them with a verb to make their meaning more apparent. Often this is paired with a description or condition, e.g., isDead, isWalking, hasDamageMultiplier, etc.

Use meaningful names. Don’t abbreviate (unless it’s math): Your variable names will reveal their intent. Choose names that are easy to pronounce and search for.

  • While single-letter variables are fine for loops and math expressions, don’t abbreviate in other situations. Clarity is more important than any time saved from omitting a few vowels.

Avoid too many prefixes or special encoding: The variable or function name should be clear enough and easily readable. You should always define access modifiers anyway, so its not necessary to add prefixes defining them.

Specify access level modifiers consistently: If you leave off the access modifier, the compiler will assume the access level should be private. This works, but always use an access modifier for clarity.

  • Only use public if absolutely necessary.
  • Remember that you’ll need to use protected if you want this in a subclass later.

Classes and interfaces

Follow these standard rules when naming your classes and interfaces:

  • Use pascal case nouns for class names: This will keep your classes organized.
  • If you have a MonoBehaviour in a file, the source file name must match: You might have other internal classes in the file, but only one MonoBehaviour should exist per file.
  • Prefix interface names with a capital “I”: Follow this with an adjective that describes the functionality.
// EXAMPLE: Class formatting
public class ExampleClass : MonoBehaviour
{

    public int PublicField;
    public static int MyStaticField;

    private int packagePrivate;
    private int myPrivate;

    private static int myPrivate;

    protected int myProtected;

    public void DoSomething()
    {
.l
    }
}

// EXAMPLE: Interfaces
public interface IKillable
{
    void Kill();
}

public interface IDamageable<T>
{
    void Damage(T damageTaken);
}

Functions

In C#, every executed instruction is performed in the context of a function.

Functions perform actions, so apply these rules to name them accordingly:

  • Start the name with a verb: Add context if necessary (e.g., GetDirection, FindTarget, etc.)
  • Use camel case for parameters: Format parameters passed into the functions like local variables.
  • Functions returning bool should ask questions: Much like Boolean variables themselves, prefix functions with a verb if they return a true-false condition. This phrases them in the form of a question (e.g., IsGameOver, HasStartedTurn).
// EXAMPLE: Methods start with a verb.
public void SetInitialPosition(float x, float y, float z)
{
    transform.position = new Vector3(x, y, z);
}

// EXAMPLE: Methods ask a question when they return bool.
public bool IsNewPosition(Vector3 currentPosition)
{
    return (transform.position == newPosition);
}

Clone this wiki locally