Microlog Code Style and Naming Conventions

September 3, 2025 ยท View on GitHub

General Style

  • Indentation: 4 spaces, no tabs.
  • Braces: K&R style (if (cond) { ... }).
  • Line Length: 80 characters max.
  • Comments: Use // for single-line, /* ... */ for block comments. Doxygen-style for public APIs (in the header). Doxygen-style for private functions (in the source).
  • Whitespace: No trailing whitespace. One blank line between functions.

Naming Conventions

Types

  • Structs, Enums, Unions, Typedefs: Always a typedef - snake_case with prefix feature_name_ and without _t suffix. Example:
typedef struct {
    char *data;
    unsigned int curr_pos;
    size_t size;
} print_buffer;

typedef union {
    print_buffer buffer;
    FILE *stream;
} print_target_descriptor;
  • Enum Constants: ALL_CAPS with prefix.
    • Example: typedef enum { PRINT_TARGET_BUFFER, PRINT_TARGET_STREAM } print_target_type;

Functions

  • Public Functions: ulog_ prefix, snake_case.
    • Example: ulog_output_level_set, ulog_log
  • Private (static) Functions: snake_case, with prefix feature_name_.
    • Example: color_print_start, color_print_end, level_print

Variables

  • Global/Static Variables: NOT ALLOWED. Create a structure to hold state if needed.
  • Local Variables: snake_case, short is widely used or very tiny scope, otherwise full meaningful words.
    • Example: tgt, ev, buf, format, out_topic_id
  • Constants/Macros: ALL_CAPS with underscores, always with a feature prefix if private, or ULOG_ prefix for public constants.
    • Example: COLORS_RED, ULOG_BUILD_TIME

Parameters

  • Function Parameters: snake_case.
    • Example: int level, const char *topic_name

Other Conventions and Recommendations

  • Avoid implicit type conversions, as they can cause unexpected behavior.
  • Use if (something) for boolean checks.
  • Use explicit comparisons like if (something < 0) when needed.
  • Write conditions clearly to show your intent.
  • Use auto formatting tools like clang-format to maintain consistency.