Skip to content

Commit 1407415

Browse files
committed
1 parent 824184a commit 1407415

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

zig/references/code-review.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Systematic code review checklist organized by detection confidence level. Work t
5353
| `allocPrint` for bounded strings | Use `bufPrint` with stack buffer | [3.10](#310-stack-vs-heap-allocation) |
5454
| Runtime constant lookup | Comptime lookup table | [3.11](#311-comptime-optimization) |
5555
| `expectEqual` with strings/slices | Use `expectEqualStrings`/`expectEqualSlices` | [3.14](#314-testing-best-practices) |
56+
| `len`, `pos`, `n` for numeric vars | Use `_count`/`_index`/`_size`/`_offset` suffixes | [3.17](#317-numeric-variable-naming-indexcountoffsetsize) |
5657

5758
---
5859

@@ -71,7 +72,7 @@ Systematic code review checklist organized by detection confidence level. Work t
7172

7273
- [1. ALWAYS FLAG (100% Confidence)](#1-always-flag-100-confidence)
7374
- [2. FLAG WITH CONTEXT (High Confidence)](#2-flag-with-context-high-confidence)
74-
- [3. SUGGEST (Advisory)](#3-suggest-advisory)
75+
- [3. SUGGEST (Advisory)](#3-suggest-advisory) (incl. [3.17 Numeric Naming](#317-numeric-variable-naming-indexcountoffsetsize))
7576

7677
---
7778

@@ -839,6 +840,8 @@ fn getSection(index: SectionIndex) *Section { ... }
839840
fn getSymbol(index: SymbolIndex) *Symbol { ... }
840841
```
841842

843+
See also [3.17](#317-numeric-variable-naming-indexcountoffsetsize) for naming conventions that complement type safety.
844+
842845
### 2.7 Error Handling Selection
843846

844847
| Detect | `anyerror` return in public API, or blind `try` propagation |
@@ -1430,6 +1433,70 @@ pub fn eql(_: @This(), a: []const u8, b: []const u8) bool {
14301433
}
14311434
```
14321435

1436+
### 3.17 Numeric Variable Naming (index/count/offset/size)
1437+
1438+
| Detect | Mixed use of `length`, `size`, `count`, `index` without consistent distinction |
1439+
|--------|-------------------------------------------------------------------------------|
1440+
| Risk | Off-by-one errors, index space confusion |
1441+
1442+
Use consistent suffixes to distinguish numeric quantities (from [TigerBeetle's TigerStyle](https://github.com/tigerbeetle/tigerbeetle/blob/main/docs/TIGER_STYLE.md)):
1443+
1444+
| Suffix | Meaning | Domain | Invariant |
1445+
|--------|---------|--------|-----------|
1446+
| `_count` | Number of items | Items ||
1447+
| `_index` | Position of one item | Items | `index < count` |
1448+
| `_size` | Number of bytes | Bytes | `size = @sizeOf(T) * count` |
1449+
| `_offset` | Byte position | Bytes | `offset < size` |
1450+
1451+
Avoid `length`/`len` for new code — it's ambiguous (Rust `str::len` = byte size; Python `len(str)` = codepoint count). Note: Zig's stdlib uses `.len` on slices, which is fine — this convention applies to your own variable names.
1452+
1453+
**Wrong:**
1454+
```zig
1455+
fn process(data: []const u8, len: usize) void {
1456+
var pos: usize = 0;
1457+
var n: usize = 0;
1458+
while (pos < len) {
1459+
// Are pos and len in the same domain? Hard to tell.
1460+
n += 1;
1461+
pos += record_len; // Is this bytes or items?
1462+
}
1463+
}
1464+
```
1465+
1466+
**Right:**
1467+
```zig
1468+
fn process(data: []const u8, data_size: usize) void {
1469+
var record_index: usize = 0;
1470+
var data_offset: usize = 0;
1471+
while (data_offset < data_size) {
1472+
// Clear: offset < size (both bytes), index counts items
1473+
record_index += 1;
1474+
data_offset += record_size; // size = bytes, obviously
1475+
}
1476+
}
1477+
```
1478+
1479+
Converting between index and offset spaces should be explicit:
1480+
1481+
```zig
1482+
const node_offset = @intFromPtr(node) - @intFromPtr(pool.buffer.ptr);
1483+
const node_index = @divExact(node_offset, node_size);
1484+
// Correctness is mechanical: offset / size = index ✓
1485+
```
1486+
1487+
**Naming tips:**
1488+
- Use "big-endian" naming — qualifiers as suffixes: `source_index`, `target_index` (not `idx_source`)
1489+
- Choose dual names of equal length for visual alignment: `source`/`target` (not `src`/`destination`)
1490+
1491+
Aligned names make bugs pop out during review:
1492+
1493+
```zig
1494+
source_index += marker.literal_word_count;
1495+
target_index += marker.literal_word_count;
1496+
```
1497+
1498+
See also [2.6](#26-type-unsafe-index-usage) for enforcing index safety with distinct enum types.
1499+
14331500
---
14341501

14351502
## File References

0 commit comments

Comments
 (0)