Hasty Briefsbeta

Brent's Encapsulated C Programming Rules (2020)

2 days ago
  • #encapsulation
  • #best-practices
  • #C-programming
  • Brent's rules for encapsulated C programming emphasize pure encapsulation and performance considerations.
  • Pure encapsulation hides struct members to prevent direct manipulation, ensuring safer code usage.
  • Performance can be impacted by pure encapsulation; exposing struct members with 'const' can help optimize.
  • Memory ownership should be clear: the creator of memory should also free it, avoiding ambiguity.
  • Avoid 'void*' for better type safety and compile-time checks; use specific structs instead.
  • Strings should remain simple; avoid unnecessary typedefs and stick to 'char*' for UTF-8 compatibility.
  • Use UTF-8 strings exclusively for compatibility and simplicity; avoid wide character types.
  • For memory arrays, prefer 'uint8_t*' over 'char*' to clearly distinguish between text and binary data.
  • Use standard 'bool' from 'stdbool.h' instead of custom definitions for true/false.
  • Avoid static and global variables to ensure thread safety and control over mutable state.
  • Prefer inline functions over macros for better readability and IDE support.
  • Test functions thoroughly; C's simplicity allows for straightforward testing without complex frameworks.
  • Functions should do one thing well, promoting modularity and reusability.
  • Write modular, UNIX-like pieces instead of monolithic systems for better maintainability.
  • Treat all compiler warnings as errors to maintain high code quality.
  • Use standard library functions and types (e.g., 'int32_t') for consistency and portability.
  • Check floating-point numbers against epsilon (e.g., 'FLT_EPSILON') instead of zero for precision.
  • Zero-initialize structs, especially pointers, to avoid undefined behavior.
  • Order struct members by size (largest first) to avoid padding issues and ensure alignment.