news.volyx.in

My personal C coding style as of late 2023 (nullprogram.com)

557 points by zdw · 1050 days ago · 444 comments on HN

Article summary

The article discusses the author's personal C coding style, which has undergone significant changes in recent years. The author presents their approach to primitive types, macros, functions, and structures, and explains the reasoning behind their choices. The article also touches on the author's rejection of null-terminated strings and their use of a custom string type. The author's goal is to share their style and the benefits it has brought to their productivity and code organization.

Main themes

  • C coding style
  • primitive types
  • macros and functions
  • structures and error handling
  • string handling
  • performance optimization
  • code organization and readability
  • error handling and safety

What commenters say

  • Using 32-bit booleans can be beneficial for performance due to architectural optimizations for 32-bit operations.
  • The use of 32-bit booleans can lead to wasted memory in certain cases, such as when used in structs or as local variables.
  • Returning structs instead of using out parameters can improve code organization and reduce error handling complexity.
  • The choice between using structs or out parameters depends on the specific use case and personal preference.
  • Redefined keywords and macros can be useful for improving code readability, but can also lead to confusion and errors if not used carefully.
  • The C language's age and legacy features can make it difficult to write efficient and readable code, but also provide opportunities for creative solutions.
  • Error handling is a crucial aspect of programming, and different approaches, such as using option types or error codes, have their own trade-offs and advantages.
  • The use of custom string types can simplify string handling and improve code safety, but may also introduce additional complexity and overhead.