Maintainable code is defined by five key traits: understandability (intent is clear to others), intuitiveness (logic is obvious), adaptability (handles data changes without full rewrites), extensibility (architecture allows for future growth), and debuggability (provides sufficient info to locate errors).
General coding conventions for maintainability include:
- Readability: Use consistent indentation (typically 4 spaces instead of tabs) and comprehensive comments for functions/methods, large blocks of logic, complex algorithms, and browser-specific hacks.
- Naming: Variables should be nouns (e.g.,
car, person). Functions should start with verbs (e.g., getName()), and boolean-returning functions should start with is (e.g., isEnabled()). - Type Transparency: Initialize variables upon definition to imply their data type.
// Specify variable type through initialization
var found = false; // Boolean
var count = 0; // Number