Use Explanatory Variables

Use Explanatory Variables

Tags
Clean Code
Javascript
Software Development
Published
Published May 16, 2022
Author
Due Date
☑️ Topic: Variables
 
☑️ Idea: Break the calculations up into intermediate values that are held in variables with meaningful names. The names give meaning and clarity to the code.
 
☑️ Benefit: Readability, clarity, maintainability.
 
☑️ Guideline: The more explanatory variables are used, the better.
 
// BAD const address = "One Infinite Loop, Cupertino 95014"; const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/; saveCityZipCode( address.match(cityZipCodeRegex)[1], address.match(cityZipCodeRegex)[2] ); // GOOD const address = "One Infinite Loop, Cupertino 95014"; const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/; const [_, city, zipCode] = address.match(cityZipCodeRegex) || []; saveCityZipCode(city, zipCode);