Include units in variable names when the value isn’t obvious to avoid ambiguity and bugs.
When a variable represents a quantity with a unit (like time, length, or size), always include the unit in the variable name (e.g., timeoutInMs
, distanceInKm
).
This makes your code self-explanatory and prevents costly mistakes from unit confusion.
This code is unclear about what unit timeout
uses:
int timeout = 1000; // ⚠️ Unit unclear
Thread.Sleep(timeout);
A developer might assume the wrong unit, leading to bugs or performance issues.
Be explicit about the unit:
int timeoutInMs = 1000; // ✅ Clearly milliseconds
Thread.Sleep(timeoutInMs);
Unit confusion has caused major bugs — from NASA’s Mars Climate Orbiter loss (imperial vs. metric) to subtle timeouts in distributed systems.
Naming variables with units is a simple, effective way to prevent these issues in your codebase.
ms
, s
, min
, px
, kb
, etc.) when clearsizeInBytes
instead of sizeInB
, where ‘B’ could mean bits or bytes)