My personal C coding style as of late 2023
In general, the style the author has adopted is to introduce brevity where he can, and use wrappers over what would have been standard and idiomatic C code. In most situations, these conventions aren&#x27;t good in a non-solo project, because they simply aren&#x27;t as obvious to the programmer.<p>He says so himself:<p>&gt; I don’t intend to use these names in isolation, such as in code snippets (outside of this article). If I did, examples would require the typedefs to give readers the complete context. That’s not worth extra explanation. Even in the most recent articles I’ve used ptrdiff_t instead of size.<p>You require extra work to understand his basic types before reading even a short snippet, so he doesn&#x27;t use it when he wants people to read short snippets.<p>Introducing additional stuff the programmer must remember that does not add any safety is pointless busywork.<p>A non-complete summary of his conventions:<p>1. typedef standard typenames to 3-char symbols,<p>2. remove qualifiers like const,<p>3. use macros to reduce the amount of typing the programmer does,<p>4. typedef all structs (and enums too, I assume)<p>5. A macro-ized string-typed with prefixed-length.<p>&gt; Starting with the fundamentals, I’ve been using short names for primitive types. The resulting clarity was more than I had expected,<p>This <i>isn&#x27;t</i> clear: `int8_t` is a lot clearer to a C programmer than `i8`, because a C programmer has already internalised the pattern of the stdint.h types. This is going to lead to subtle bugs as well: quick, according to his convention, what is the % specifier for `byte`?<p>You can use %c, but that gives you an ascii character (which is not what we think of when we say &#x27;byte&#x27;).<p>If you use PRIu8 the compiler might give warnings because `char` might be signed. The best option is to just not use `byte` and use `uint8_t` instead (or, in his system, `u8`).<p>Same with `b32` vs `i32` - it&#x27;s a distinction without a difference and mixing these types won&#x27;t give compiler warnings, while it is almost certainly an error on the part of the developer. Use `bool` if you don&#x27;t like `_Bool`.<p>In general I try to take advantage of whatever typing C provides; I don&#x27;t try to subvert it because I <i>want</i> the compiler to warn me when my intention doesn&#x27;t match the code I wrote.<p>&gt; No const. It serves no practical role in optimization, and I cannot recall an instance where it caught, or would have caught, a mistake.<p>I disagree with dropping `const`.<p>1. It&#x27;s useful as an indicator to the caller that the returned value must&#x2F;must not be freed. It&#x27;s a convention I use that makes it easy to visually spot memory leaks.<p>Of the two functions below, it&#x27;s clear to me which one needs the returned value `free()`ed and which one doesn&#x27;t.<p><pre><code>     const char *replace_substring (const char *src, const char *pat, const char *replacement);
     char *replace_substring (const char *src, const char *pat, const char *replacement);

</code></pre>
2. It actually <i>does</i> catch a lot of problems, because the compiler warns me when I attempt to modify a value that some other code I wrote never intended to be modified. It&#x27;s about intention, and when I <i>know</i> it is safe to modify the `const` value, then I have to explicitly cast away the const to compile my program. Anyone reading the program will <i>know</i> that the modification of the const-qualifed value is intentional, and therefore safe.<p>&gt; #define s8(s) (s8){(u8 *)s, lengthof(s)}<p>This is interesting. I will try this out in my next project. I do think that there&#x27;ll be quite a few compiler warnings for sign-mismatch though. This is the second &quot;I wonder what the sign is&quot; question for programmers reading his code - it means that his code has to compile with the flags that he compiles it with (I assume he&#x27;s passing a flag to force chars to a particular sign). You can&#x27;t simply compile his code in another project unless you copy his flags, and those flags may conflict with the new projects flags.<p>I also wish that he&#x27;d showed a few examples of how having the length helps - what is presented in the post doesn&#x27;t show any additional string safety over using nul-terminated strings. All those macros, including the one that creates the struct, could be written to operate on null-terminated strings. In essence, the length can be simply unused for everything! Where&#x27;s the safety!?<p>&gt; It’s also led to a style of defining a zero-initialized return value at the top of the function, i.e. ok is false, and then use it for all return statements. On error, it can bail out with an immediate return.<p>I use a similar pattern, but I use `goto cleanup` on all errors; you can&#x27;t, as a general pattern, return early in a non-trivial C function without leaking resources. You can, as a general pattern, `goto cleanup` in every C function to clean up resources. I prefer the general pattern that I use everywhere rather than having to ensure that all resources acquired <i>up to that particular return statement</i> are released.<p>&gt;  rather than include windows.h, write the prototypes out by hand using custom types.<p>I think this is a very bad idea: you can&#x27;t depend on the headers not changing after a compiler or library update. Sure, <i>maybe</i> in practice, all the Windows types and declarations don&#x27;t change all that much, but I wouldn&#x27;t want to be the developer trying to hunt down a bug because the interface to some function has changed and the compiler isn&#x27;t giving me errors.<p>All in all, I dunno if I would look forward to working on a team with these conventions - the code is harder to read, doesn&#x27;t work in isolation, needs custom flags, and introduces a string type without introducing any string safety with it.