Reviewer · Naming Conventions
Semantically precise identifiers
Choose names for functions, methods, variables, constants, and packages that precisely and unambiguously describe what they do or represent, not just a loose approximation. Before naming, check for hidden side effects (mutation, locking requirements, fallback behavior, side-channel writes), units of measurement, and whether the concept already has an...
Choose names for functions, methods, variables, constants, and packages that precisely and unambiguously describe what they do or represent, not just a loose approximation. Before naming, check for hidden side effects (mutation, locking requirements, fallback behavior, side-channel writes), units of measurement, and whether the concept already has an established naming pattern elsewhere in the codebase, and reflect all of that in the name.
Specifically:
- If a function has a side effect beyond what its name implies, either rename it to reveal the effect or split it into a pure part and an effectful part. E.g. a
validate()method that also deletes invalid entries should bevalidateAndClean(); ascan()method that actually schedules/starts compaction jobs should be namedcompact()/run(); astart()method that only initializes internal state (not a service/goroutine) should beinit(). - When a function must be called under a precondition (e.g. “call only while holding a lock”), prefer an active name like
lockThenPushover a passive one likepushLocked, which can be misread as “safe to call while locked” rather than “requires the lock already held.” - Include units and qualifiers in names (variables, config flags, metrics) whenever the value could be ambiguous, e.g. use
maxRunSizeBytes/uncompressedMaxRunSizeBytesinstead ofmaxRunSize, andloki_dataobj_builder_object_size_compressed_bytesinstead ofloki_dataobj_builder_object_size_bytes. - Follow existing naming conventions for sibling/related identifiers instead of introducing a new scheme: if
Overlaps/OverlapsCloseddenotes half-open vs. closed ranges, a newCoversmethod should follow the same pattern (Covers/CoversClosed) rather than inventingCoversOpen. - Avoid two identifiers in the same scope that differ only by a prefix/suffix of the same word (e.g.
ctxandvctx, or a parameter namedindexrepresenting anindexRun), as this is easy to misread; pick clearly distinct names instead. - When a value’s meaning shifts (e.g. an enum like
KindLogsused to represent an object kind, not a cache kind), keep the name aligned with what the value actually represents, and rename if a refactor changes that meaning. - For packages, avoid stuttering or redundant names (e.g.
logqlmodel/logqlerr); prefer a short, non-redundant name such aserrors, using import aliases where collisions could occur.
Apply this as a review checklist: read the name aloud as if you were a new contributor, decide what you’d assume it does, and confirm the implementation matches that assumption exactly.