Add Units to Variable Names

✅ One-liner Summary

Include units in variable names when the value isn’t obvious to avoid ambiguity and bugs.

💡 Short Explanation

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.

🚫 Bad Example

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.

✅ Good Example

Be explicit about the unit:

int timeoutInMs = 1000; // ✅ Clearly milliseconds
Thread.Sleep(timeoutInMs);

💬 Real-World Insight

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.

🧠 Key Takeaways

  • Always add units to variable names when the value isn’t dimensionless
  • Use standard abbreviations (ms, s, min, px, kb, etc.) when clear
  • Use the full unit name when abbreviations are not clear (e.g., sizeInBytes instead of sizeInB, where ‘B’ could mean bits or bytes)
  • This practice improves readability, maintainability, and safety