Prompt
Complex expressions and repeated code should be extracted into well-named variables to improve readability and maintainability. This applies to:
- Repeated string/value combinations
- Complex conditional logic
- Array transformations
- URL or path constructions
Example - Instead of:
if ([options.path, responseDetails.headers.location].every(item => item === '/foo')) {
// ...
}
Better approach:
const isPathMatching = options.path === '/foo';
const isLocationMatching = responseDetails.headers.location === '/foo';
if (isPathMatching && isLocationMatching) {
// ...
}
This practice:
- Makes code self-documenting through meaningful variable names
- Reduces cognitive load when reading complex logic
- Makes debugging easier by providing clear intermediate values
- Prevents duplicate calculations