CS 300

Logo

Computer Graphics
Fall 2021

Style Guidelines

While it is important that programs you write function correctly, it is equally important that they be readable and maintainable. This is something that you do not just for yourself, but for others who will be working with or reading the code you write. Coding style guidelines can vary with employer and programming language, but for the purposes of this class we will follow a few simple guidelines.

Indentation

In most programming languages we use indentation to help visualize nested blocks of code. Each level of indentation should be consistent:

DO: Consistent Indentation

int main() {
    for (int x = 0; x < 10; x++) {
        if (x % 2 == 0) {
            printf("%i\n", x);
        }
    }
    return 0;
}

DON’T: Mix different indentation sizes

int main() {
    for (int x = 0; x < 10; x++) {
            if (x % 2 == 0) {
                printf("%i\n", x);
        }
}
return 0;
}

Spacing

To make it easier to read a statement you should have one space between variables, operators, and literals:

DO: Proper spacing inside statements

int main() {
    for (int x = 0; x < 10; x++) {
        if (x % 2 == 0) {
            printf("%i\n", x);
        }
    }
    return 0;
}

Note the spaces after the for and the if. There should only be parenthesis immediately after a function (like the printf). It is not necessary to have spaces between the semicolons (;).

DON’T: Statements with missing spacing

int main() {
    for (int x=0; x<10; x++) {
        if (x%2==0) {
            printf("%i\n", x);
        }
    }
    return 0;
}

DON’T: Use inconsistent spacing

int main() {
    for (int x =0; x< 10; x++) {
        if (x% 2 ==0) {
            printf("%i\n",x);
        }
    }
    return 0;
}

Blocks

Curly braces are used to indicate the start and end of functions, loops, conditional, and blocks.

DO: Start curly braces on the same line a block begins

private void OnTriggerEnter(Collider other) {
    if (other.gameObject.CompareTag("PickUp")) {
        pickupSound.Play();
        ++count;
        SetCountText();
        other.gameObject.SetActive(false);
    }
}

DO: Put a space between the curly brace and any content that preceeds it.

private void OnTriggerEnter(Collider other) {
    if (other.gameObject.CompareTag("PickUp")) {
        pickupSound.Play();
        ++count;
        SetCountText();
        other.gameObject.SetActive(false);
    }
}

Note the spacing between the ending parenthesis and the curly brace at the end of the for and if conditions.

An alternative style that is also acceptable, DO: Start your curly braces on the next line

private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("PickUp"))
    {
        pickupSound.Play();
        ++count;
        SetCountText();
        other.gameObject.SetActive(false);
    }
}

DO NOT: Mix curly brace location styles

private void OnTriggerEnter(Collider other) {
    if (other.gameObject.CompareTag("PickUp"))
    {
        pickupSound.Play();
        ++count;
        SetCountText();
        other.gameObject.SetActive(false);
    }
}

Functions

Comments

Here is an example:

/*
    Detects entering a collider set as a trigger and determines the
    correct helper function(s) to call.

    Parameters:
        other - collider the player has hit
 */
private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("PickUp"))
    {
        pickupSound.Play();
        ++count;
        SetCountText();
        other.gameObject.SetActive(false);
    }
}

Variables