CYBOI Interpreter C Code Conventions

Indentation

  1. Use spaces and never tab characters
  2. Indent code by four spaces
  3. Make use of spaces and white lines to separate code logically

Comment

  1. Comment all code exhaustively using double slash //
  2. Document each function using JavaDoc style blocks /** ... */

Whitespace

  1. Put a blank space between a keyword followed by a parenthesis, for example "while (true)"
  2. Glue method name and its opening parenthesis without blank space, for example "test()"
  3. Follow commas by a blank space, for example "test(x, y, z);"
  4. Separate binary operators from their operands by a blank space, for example "a + b"

Naming

  1. Write out terms and avoid abbreviations
  2. Name all functions and variables as well as files in lower case letters
  3. Separate words with underscore (snake case instead of camel case), for example "test_function()"
  4. List function parametres as p1, p2, p3, ...
  5. Use just one or two letters for local variables
  6. Name constants all uppercase with words separated by underscore

Declaration

  1. Declare just one variable per line
  2. Initialise local variables right at their declaration

Implementation

  1. Put each function in its own file
  2. Put the opening bracket of a block to the end of a declaration, followed by a white line
  3. Hand over all arguments (parametres) as void pointer of type "void*"
  4. Use the void return type so that the function has no return value

Control

  1. Use "if-else" conditions only, no "switch-case"
  2. Use "while" loops only, no "do-while" or "for"
  3. Use endless loops only with the break condition placed inside the loop body
  4. Use "break" instead of "continue" to end a loop

Practice

  1. Define unchangeable values as constant in directory "src/constant/"
  2. Use item (data array with count and size) instead of null-terminated strings
  3. Check pointers for null before accessing them
  4. Avoid redundant implementation of already existing functions

Example

/**
 * This is an example function.
 *
 * It does something.
 *
 * @param p0 the integer value
 * @param p1 the wide character string
 * @param p2 the array (pointer reference)
 */
void do_something(void* p0, void* p1, void* p2) {

    // Check for null.
    if (p2 != *NULL_POINTER_STATE_CYBOI_MODEL) {

        // Cast void pointer parametre to its actual type.
        void** a = (void**) p2;

        // Check for null.
        if (p1 != *NULL_POINTER_STATE_CYBOI_MODEL) {

            // Cast void pointer parametre to its actual type.
            wchar_t* c = (wchar_t*) p1;

            // Check for null.
            if (p0 != NULL_POINTER_STATE_CYBOI_MODEL) {

                // Cast void pointer parametre to its actual type.
                int* i = (int*) p0;

                log_message_terminated((void*) INFORMATION_LEVEL_LOG_CYBOI_MODEL, (void*) L"Do something.");

                // Print first string array element by dereferencing a.
                fwprintf(stdout, L"The first string array element is: %ls\n", (wchar_t*) *a);

                // Assign unicode to dereferenced character c.
                *c = 32;

                // Calculate integer value by dereferencing i.
                *i = *i + 10;

                // The loop variable.
                int j = *NUMBER_0_INTEGER_STATE_CYBOI_MODEL;

                // Use a while endless loop.
                while (*TRUE_BOOLEAN_STATE_CYBOI_MODEL) {

                    // Break loop from within loop body.
                    if (j >= *NUMBER_10_INTEGER_STATE_CYBOI_MODEL) {

                        break;
                    }

                    // Increment loop variable.
                    j++;
                }

            } else {

                log_message_terminated((void*) ERROR_LEVEL_LOG_CYBOI_MODEL, (void*) L"Could not do something. The integer value parametre p0 is null.");
            }

        } else {

            log_message_terminated((void*) ERROR_LEVEL_LOG_CYBOI_MODEL, (void*) L"Could not do something. The wide character string parametre p1 is null.");
        }

    } else {

        log_message_terminated((void*) ERROR_LEVEL_LOG_CYBOI_MODEL, (void*) L"Could not do something. The array parametre p2 is null.");
    }
}