Skip to content

Style Guide

Drahsid edited this page Aug 3, 2021 · 3 revisions

Style Guide

The rules here apply unless something else is required to produce a matching rom.

Naming

Type Style
Function Snake_PascalCase
Struct Name PascalCase
Local Variable snake_case
Global Variable gCamelCase
Static Variable sCamelCase
Enum Variable eCamelCase
Enumerators SNAKE_CASE_CAPS
Define SNAKE_CASE_CAPS
Const SNAKE_CASE_CAPS

For functions, use PascalCase if function cannot be categorized For example:

#define ABS(x) (((x) < 0) ? -x : x)

typedef struct {
    int32_t foo;
    int32_t bar;
} FooBar;

enum eFooBar {
    FOO_ONE = 1,
    BAR_TWO,
    FOO_THREE,
    BAR_FOUR
};

const char HELLO_WORLD[] = "Hello, world!";
FooBar gFoo = { FOO_ONE, BAR_TWO };
FooBar sFoo = { FOO_THREE, BAR_FOUR };

int32_t FooBar_Add(FooBar* this, FooBar* that) {
    int32_t sum_result;

    this->foo = ABS(this->foo + that->foo);
    this->bar = ABS(this->bar + that->bar);

    sum_result = this->foo + this->bar;
    return sum_result;
}

Local Variable Naming

For particular local variables, there are preferred names for consistency-sake. If there are multiple local variables that could have a name, you can derive a new name from the context.

Behavior Name
Iterator index
Pointer-to Actor actor

Indentation and Alignment

An indent is 4 spaces.

If possible, align defines, ='s, and block comment *'s with each other.

/*
 * Keep things vertically aligned when possible!
 */
#define FOO    (1)
#define BAR    (2)
#define FOOBAR (3) // makes sure your single-line comments have a space after the //!

void FooBar(void) {
    int32_t foo    = FOO;
    int32_t bar    = BAR;
    int32_t foobar = FOOBAR;
}

Braces

Keep braces on the same line with a single space. else and else if go to a new-line. Additionally, make sure that functions, and loops are not on one line unless empty.

void Hello_World() {
    uint16_t index;

    if (foo) {
    }
    else if (bar) {
    }
    else {
    }

    for (index = 0; index < 10; index++) {
        if (index == 2) continue;
    }
}

Pointers

Pointers are aligned with the type (left-sided)

int32_t* foo;

Base Types

Use the types defined in inttypes.h. For example:

int8_t foo; // instead of s8
int16_t bar; // instead of s16
int32_t foobar; // instead of s32 or int
int64_t barfoo; // instead of s64
float hello; // instead of f32
double world; // instead of f64
// likewise for unsigned types

Nonmatching Code

If you are working on some code, but it doesn't match (and you move on to something else for the time being,) use this format so that the nonmatching is not compiled:

#ifdef NON_MATCHING
// function
#else
#pragma GLOBAL_ASM("path/to/function.s")
#endif
Clone this wiki locally