A Variable Should Have Clear and Obvious Naming
Real world coding requires spending a lot of time modifying and extending existing code rather than writing new code from scratch. Well labeled information is vitally important when returning to your old code or someone else’s code. Carefully considering your variable names will pay dividends in the future. For example
let ourPlanetName = "Earth"; is more descriptive than
let planet = "Earth";
Some good-to-follow rules are:
- Use human-readable names like userName or shoppingCart.
- Stay away from abbreviations or short names like a, b, c, unless you really know what you’re doing.
- Make names maximally descriptive and concise. Examples of bad names are data and value. Such names say nothing. It’s only okay to use them if the context of the code makes it exceptionally obvious which data or value the variable is referencing.
- Agree on terms within your team and in your own mind. If a site visitor is called a “user” then we should name related variables currentUser or newUser instead of currentVisitor or newManInTown.