Skip to content

Commit e1682ba

Browse files
committed
quickref
Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent a75e69a commit e1682ba

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

C_CODING_STYLE.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,7 @@ This convention system creates self-documenting code where the naming pattern im
22352235
| Function Pattern | Allocation | Cleanup Required | Usage |
22362236
|------------------|------------|------------------|--------|
22372237
| `_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)` |
22392239
| `_new` / `_delete` | Heap-allocated | Yes | `user_new("Alice")`, `user_delete(obj)` |
22402240

22412241
### Pointer Type Usage
@@ -2262,19 +2262,20 @@ This convention system creates self-documenting code where the naming pattern im
22622262
| Early exit | Reduce nesting | Guard clause with braces |
22632263
| Switch | Testing one variable for 3+ values | More efficient and clear |
22642264
| 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) |
22662266
| Goto for cleanup | Resource management | Single cleanup path |
22672267

22682268
### API Design Patterns
22692269

22702270
| Pattern | Description | Example |
22712271
|---------|-------------|---------|
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)` |
22732273
| Array + length | Arrays always followed by length | `process(arr, arr_len)` |
22742274
| Error handling | Return status, output via parameters | `if (parse_int(str, &val) != ParseIntOk)` |
22752275
| Enums over bools | Self-documenting options | `UserMessageBlinking` not `true` |
22762276
| Array syntax | `[]` for multiple items, `*` for single | `void sort(int arr[], size_t len)` |
22772277
| Global state | Use context struct, not globals | `process(Request *req, GlobalContext *ctx)` |
2278+
| Struct parameters | Always pass by pointer | `void update(User *user)` |
22782279

22792280
### Type Selection
22802281

@@ -2284,7 +2285,8 @@ This convention system creates self-documenting code where the naming pattern im
22842285
| Pointer as integer | `uintptr_t` | `uintptr_t addr = (uintptr_t)ptr` |
22852286
| Pointer difference | `ptrdiff_t` | `ptrdiff_t off = p2 - p1` |
22862287
| 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)` |
22882290

22892291
### Macro Safety
22902292

@@ -2293,6 +2295,7 @@ This convention system creates self-documenting code where the naming pattern im
22932295
| Parenthesize parameters | `#define SQUARE(x) ((x) * (x))` |
22942296
| Parenthesize entire expression | `#define MAX(a, b) ((a) > (b) ? (a) : (b))` |
22952297
| Multi-statement pattern | `#define SWAP(a, b) do { ... } while (0)` |
2298+
| Avoid side effects | Don't use with `++` or function calls |
22962299

22972300
### Memory Guidelines
22982301

@@ -2303,6 +2306,8 @@ This convention system creates self-documenting code where the naming pattern im
23032306
| Allocation checks | Always check malloc | Handle or abort |
23042307
| Cleanup | Use goto pattern | Initialize pointers to NULL |
23052308
| `free(NULL)` | Safe, no check needed | Simplifies cleanup |
2309+
| Struct alignment | Largest to smallest | Minimize padding |
2310+
| Bit fields | Unsigned only | Explicit padding |
23062311

23072312
### Static Assertions
23082313

@@ -2312,13 +2317,53 @@ This convention system creates self-documenting code where the naming pattern im
23122317
| Layout assumptions | `_Static_assert(offsetof(T, field) == 8, "Layout changed")` |
23132318
| Config validation | `_Static_assert(MAX >= MIN, "Invalid config")` |
23142319
| Platform assumptions | `_Static_assert(sizeof(int) == 4, "Needs 32-bit int")` |
2320+
| Enum bounds | `_Static_assert(StateMax <= 255, "Exceeds uint8_t")` |
23152321

23162322
### Header Naming Convention
23172323

23182324
| Module Name | Header File | Not |
23192325
|-------------|-------------|-----|
23202326
| `date_time` | `date_time.h` | `datetime.h`, `DateTime.h` |
23212327
| `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` |
23222367

23232368
### Documentation Guidelines
23242369

0 commit comments

Comments
 (0)