Encapsulate conditionals

Encapsulate conditionals

Tags
Clean Code
Javascript
Software Development
Published
Published July 18, 2022
Author
Due Date
☑️ Topic: Functions
 
☑️ Idea: Encapsulate the expressions used in conditionals and use the new function name to indicate their intent. Boolean logic is easier to understand if it’s read in isolation.
 
☑️ Benefits: Readability, refactorability.
 
☑️ Guideline: If the expression of a conditional has multiple terms consider creating a function with them.
 
// BAD if (fsm.state === "fetching" && isEmpty(listNode)){ // ... } // GOOD function shouldShowSpinner(fsm, listNode) { return fsm.state === "fetching" && isEmpty(listNode); } if (shouldShowSpinner(fsmInstance, listNodeInstance)){ // ... }