@@ -2235,7 +2235,7 @@ This convention system creates self-documenting code where the naming pattern im
2235
2235
| Function Pattern | Allocation | Cleanup Required | Usage |
2236
2236
| ------------------| ------------| ------------------| --------|
2237
2237
| ` _init ` | Stack-friendly | No | ` date_time_init(&obj, 2025, 4, 25) ` |
2238
- | ` _create ` / ` _destroy ` | Stack-friendly | Yes | ` buffer_create(&obj, 1024) ` , ` buffer_destroy(&obj) ` |
2238
+ | ` _create ` / ` _destroy ` | Stack with resources | Yes | ` buffer_create(&obj, 1024) ` , ` buffer_destroy(&obj) ` |
2239
2239
| ` _new ` / ` _delete ` | Heap-allocated | Yes | ` user_new("Alice") ` , ` user_delete(obj) ` |
2240
2240
2241
2241
### Pointer Type Usage
@@ -2262,19 +2262,20 @@ This convention system creates self-documenting code where the naming pattern im
2262
2262
| Early exit | Reduce nesting | Guard clause with braces |
2263
2263
| Switch | Testing one variable for 3+ values | More efficient and clear |
2264
2264
| Empty lines in switch | Cases > 3 lines | Improves readability |
2265
- | Increment operators | Avoid return value usage | ` i++; b = i; ` not ` b = ++i ` |
2265
+ | Increment operators | Post-increment preferred | ` i++ ` (stylistic choice) |
2266
2266
| Goto for cleanup | Resource management | Single cleanup path |
2267
2267
2268
2268
### API Design Patterns
2269
2269
2270
2270
| Pattern | Description | Example |
2271
2271
| ---------| -------------| ---------|
2272
- | Parameter order | ` target, inputs, outputs, options, environment ` | ` parse_csv(data, len, &table, opts, env) ` |
2272
+ | Parameter order | target, inputs, outputs, options, environment | ` parse_csv(data, len, &table, opts, env) ` |
2273
2273
| Array + length | Arrays always followed by length | ` process(arr, arr_len) ` |
2274
2274
| Error handling | Return status, output via parameters | ` if (parse_int(str, &val) != ParseIntOk) ` |
2275
2275
| Enums over bools | Self-documenting options | ` UserMessageBlinking ` not ` true ` |
2276
2276
| Array syntax | ` [] ` for multiple items, ` * ` for single | ` void sort(int arr[], size_t len) ` |
2277
2277
| Global state | Use context struct, not globals | ` process(Request *req, GlobalContext *ctx) ` |
2278
+ | Struct parameters | Always pass by pointer | ` void update(User *user) ` |
2278
2279
2279
2280
### Type Selection
2280
2281
@@ -2284,7 +2285,8 @@ This convention system creates self-documenting code where the naming pattern im
2284
2285
| Pointer as integer | ` uintptr_t ` | ` uintptr_t addr = (uintptr_t)ptr ` |
2285
2286
| Pointer difference | ` ptrdiff_t ` | ` ptrdiff_t off = p2 - p1 ` |
2286
2287
| Fixed sizes | ` uint32_t ` , ` int64_t ` | ` uint32_t crc32 ` |
2287
- | Binary data | ` uint8_t * ` | ` hash_compute(uint8_t *hash) ` |
2288
+ | Binary data | ` uint8_t * ` | ` hash_compute(uint8_t data[]) ` |
2289
+ | Generic types | typedef | ` typedef void (*callback_t)(int) ` |
2288
2290
2289
2291
### Macro Safety
2290
2292
@@ -2293,6 +2295,7 @@ This convention system creates self-documenting code where the naming pattern im
2293
2295
| Parenthesize parameters | ` #define SQUARE(x) ((x) * (x)) ` |
2294
2296
| Parenthesize entire expression | ` #define MAX(a, b) ((a) > (b) ? (a) : (b)) ` |
2295
2297
| Multi-statement pattern | ` #define SWAP(a, b) do { ... } while (0) ` |
2298
+ | Avoid side effects | Don't use with ` ++ ` or function calls |
2296
2299
2297
2300
### Memory Guidelines
2298
2301
@@ -2303,6 +2306,8 @@ This convention system creates self-documenting code where the naming pattern im
2303
2306
| Allocation checks | Always check malloc | Handle or abort |
2304
2307
| Cleanup | Use goto pattern | Initialize pointers to NULL |
2305
2308
| ` free(NULL) ` | Safe, no check needed | Simplifies cleanup |
2309
+ | Struct alignment | Largest to smallest | Minimize padding |
2310
+ | Bit fields | Unsigned only | Explicit padding |
2306
2311
2307
2312
### Static Assertions
2308
2313
@@ -2312,13 +2317,53 @@ This convention system creates self-documenting code where the naming pattern im
2312
2317
| Layout assumptions | ` _Static_assert(offsetof(T, field) == 8, "Layout changed") ` |
2313
2318
| Config validation | ` _Static_assert(MAX >= MIN, "Invalid config") ` |
2314
2319
| Platform assumptions | ` _Static_assert(sizeof(int) == 4, "Needs 32-bit int") ` |
2320
+ | Enum bounds | ` _Static_assert(StateMax <= 255, "Exceeds uint8_t") ` |
2315
2321
2316
2322
### Header Naming Convention
2317
2323
2318
2324
| Module Name | Header File | Not |
2319
2325
| -------------| -------------| -----|
2320
2326
| ` date_time ` | ` date_time.h ` | ` datetime.h ` , ` DateTime.h ` |
2321
2327
| ` csv_parser ` | ` csv_parser.h ` | ` csvparser.h ` , ` CSVParser.h ` |
2328
+ | ` buffer_pool ` | ` buffer_pool.h ` | ` BufferPool.h ` , ` buffer-pool.h ` |
2329
+
2330
+ ### Common Unit Suffixes
2331
+
2332
+ | Category | Suffixes | Examples |
2333
+ | ----------| ----------| ----------|
2334
+ | Time | ` _ms ` , ` _us ` , ` _ns ` , ` _sec ` , ` _min ` | ` delay_ms(100) ` , ` timeout_sec ` |
2335
+ | Distance | ` _mm ` , ` _cm ` , ` _m ` , ` _km ` | ` distance_km ` , ` gap_mm ` |
2336
+ | Angles | ` _deg ` , ` _rad ` | ` rotate_deg(90) ` , ` angle_rad ` |
2337
+ | Data | ` _bytes ` , ` _kib ` , ` _mib ` , ` _gib ` | ` buffer_size_bytes ` , ` cache_mib ` |
2338
+ | Frequency | ` _hz ` , ` _khz ` , ` _mhz ` | ` sample_rate_khz ` , ` clock_mhz ` |
2339
+ | Speed | ` _rpm ` , ` _kmh ` , ` _mps ` | ` motor_rpm ` , ` velocity_mps ` |
2340
+
2341
+ ### Typedef and Forward Declarations
2342
+
2343
+ | Pattern | Usage | Example |
2344
+ | ---------| -------| ---------|
2345
+ | Public API types | Use typedef in headers | ` typedef struct { ... } DateTime; ` |
2346
+ | Internal types | Use explicit struct keyword | ` struct ParserState state; ` |
2347
+ | Function pointers | Always use typedef | ` typedef void (*handler_t)(Event *); ` |
2348
+ | Opaque types | Forward declare in headers | ` typedef struct Buffer Buffer; ` |
2349
+ | Circular dependencies | Use forward declarations | ` typedef struct Node Node; ` |
2350
+
2351
+ ### External Data Handling
2352
+
2353
+ | Issue | Solution | Example |
2354
+ | -------| ----------| ---------|
2355
+ | Byte order | Convert to host order | ` value = ntohl(network_value) ` |
2356
+ | Alignment | Use memcpy for safety | ` memcpy(&val, &buffer[off], 4) ` |
2357
+ | Packed structs | Compiler-specific attributes | ` __attribute__((packed)) ` |
2358
+
2359
+ ### Comment Tags
2360
+
2361
+ | Tag | Usage | Example |
2362
+ | -----| -------| ---------|
2363
+ | TODO | Planned improvements | ` // TODO: Add IPv6 support ` |
2364
+ | FIXME | Known bugs | ` // FIXME: Buffer overflow risk ` |
2365
+ | HACK | Temporary workarounds | ` // HACK: Sleep to avoid race ` |
2366
+ | WORKAROUND | Permanent fixes for external issues | ` // WORKAROUND: Library bug #123 ` |
2322
2367
2323
2368
### Documentation Guidelines
2324
2369
0 commit comments