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_casewith prefixfeature_name_and without_tsuffix. 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_CAPSwith prefix.- Example:
typedef enum { PRINT_TARGET_BUFFER, PRINT_TARGET_STREAM } print_target_type;
- Example:
Functions
- Public Functions:
ulog_prefix,snake_case.- Example:
ulog_output_level_set,ulog_log
- Example:
- Private (static) Functions:
snake_case, with prefixfeature_name_.- Example:
color_print_start,color_print_end,level_print
- Example:
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
- Example:
- Constants/Macros:
ALL_CAPSwith underscores, always with a feature prefix if private, orULOG_prefix for public constants.- Example:
COLORS_RED,ULOG_BUILD_TIME
- Example:
Parameters
- Function Parameters:
snake_case.- Example:
int level,const char *topic_name
- Example:
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-formatto maintain consistency.