User Tools

Site Tools


design:coding_style_guide

This is an old revision of the document!


Coding style guide

This style guide is meant to be loosely adhered to when writing libraries for the Wonderful ecosystem.

Naming

As everyone downstream will end up using the hardware libraries' API, consistency in naming is among the most important things to pay attention to. Formatting can be fixed for free; naming changes are expensive.

Global identifiers

Regarding cases:

  • Global C function names are written with snake_case. This allows easily distinguishing them from function names in more modern languages on top of the C API, which tend to use PascalCase or camelCase.
  • Global C/C++ macro definitions are written with UPPER_SNAKE_CASE.

Regarding names:

  • Global identifiers should be prefixed with the name of the library.
    • For example, if you're working on libws, a global function name should be ws_function(void), not function(void).
    • Similarly, for libwsx, the name is wsx_function(void); even if it also targets the ws platform, the library is different, so the prefix changes likewise.
  • Following from that, categorizing functions should be done prefix-first, but beyond the category of the function, an operation should be written like a sentence.
    • For example, ws_screen_fill_tiles is appropriate, not ws_screen_tiles_fill or ws_fill_screen_tiles.
  • Some macros can refer to a type; for readability and to distinguish it from function categories, these should be suffixed.
    • For example, an I/O port will be referred to as DISPLAY_BORDER_PORT, and a mask for DISPLAY_BORDER_COLOR© will be referred to as DISPLAY_BORDER_COLOR_MASK.

Code formatting

void braces_on_the_same_line(int always) {
    if (single_line)
        no_braces_is_fine;
    
    if (multi_line) {
        braces_on_the_same_line(1);
        
        // ... but treat sizeof, etc. like a function.
        memcpy(&b, &a, sizeof(a));
    }
}

design/coding_style_guide.1746339849.txt.gz · Last modified: 2025/05/04 06:24 by asie