Seven little habits for writing better code
Briefly

Seven little habits for writing better code
"Last week, I talked about the relationship between polishing forks and writing good code, and how deep attention to detail is the key to true greatness as a developer. Writing that column set me to thinking about how, exactly, one "polishes" code to make it worthy of a Michelin star. So, here are seven little habits you can adopt that will set your code apart from the merely good code and make it truly great."
"You should always prefer enumerations over constant values. Not only are enumerations compiler-friendly, but they are more human-readable as well. For instance, get a load of this code: function calculateDiscount(customerId: number): number { // Look up loyalty tier somehow... if (customerId === 100) { return 1; } else if (customerId === 200) { return 2; } else { return 3; } } It's not at all clear from the above what 1, 2, and 3 mean. So you think, "Okay, I'll give them some values," and you do something like this: const DISCOUNT_GOLD = 1; const DISCOUNT_SILVER = 2; const DISCOUNT_BRONZE = 3; function calculateDiscount(customerId: number): number { if (customerId === 100) { return DISCOUNT_GOLD; } else if (customerId === 200) { return DISCOUNT_SILVER; } else { return DISCOUNT_BRONZE; } } Now, I'll grant that this is better. However, this code still can lead to confusion because the function could be altered to return 45 without defining what 45 means."
Focused attention to detail and disciplined habits elevate code quality and maintainability. Good habits and willpower address many common coding pitfalls. Prefer enumerations over raw constants because enumerations are compiler-friendly, more human-readable, and enforce valid return values. Numeric constants can be ambiguous and allow meaningless values to creep into code. Defining and using enumeration types makes function return types explicit and prevents accidental misuse. Small, consistent practices across a codebase reduce errors, communicate intent clearly, and turn merely good code into genuinely polished, reliable software.
Read at InfoWorld
Unable to calculate read time
[
|
]