Reviewer · Code Style
Avoid unnecessary abstraction
Prefer inlining constants, variables, and small helper functions that are used only once instead of extracting them into named symbols. Extracting single-use code adds a layer of indirection that forces readers to jump between the declaration and its single use site without providing any real reuse benefit, which hurts readability rather than improving it....
Prefer inlining constants, variables, and small helper functions that are used only once instead of extracting them into named symbols. Extracting single-use code adds a layer of indirection that forces readers to jump between the declaration and its single use site without providing any real reuse benefit, which hurts readability rather than improving it. This applies in several common situations:
- Constants defined for a single use (especially in tests): inline the literal value instead of declaring a package-level or file-level
const.// Avoid const testUpgradeContainerID = "cnt-upgrade" ... id := testUpgradeContainerID // Prefer id := "cnt-upgrade" - Helper functions/methods that wrap a single call site and add no independent value or reuse (e.g., a one-line conversion or a trivial wrapper around another call): inline the body at the call site instead of introducing a new exported method or function.
- Variables assigned far from where they are used, or whose value is only consumed deep inside a function: declare and use them close together, and prefer an early return/continue over carrying a variable (e.g. an error) across a large block of unrelated code.
if configErr != nil { // handle/skip early, instead of checking configErr far below continue } - Avoid unrelated refactors (e.g., extracting experimental or soon-to-be-removed code into a new function) when the change is orthogonal to the purpose of the current patch—keep it inline until there’s a genuine need to share it.
Only extract a name (constant, variable, or function) when it is reused in multiple places, clarifies otherwise-unclear intent, or is independently testable. Otherwise, keep the code inline and close to its point of use.