decompose-conditional
Extract complex condition logic and branches into clearly named methods.
SKILL.mdskills/code-quality/decompose-conditional/SKILL.md
GitHubDecompose Conditional
Complex conditionals make code hard to follow. Extract the condition and each branch into descriptively named methods.
When to Use
if/elsebranches contain several lines of logic- The condition itself is a complex boolean expression
- Different branches represent distinct business rules
- Code readers must mentally trace logic rather than read intent
How to Apply
- Extract the condition into a method named after what it checks
- Extract the
then-branch body into its own method - Extract the
else-branch body into its own method - Replace the original conditional with calls to these methods
- Run tests
Example
// Before
function charge(date: Date, quantity: number): number {
if (date >= SUMMER_START && date <= SUMMER_END) {
return quantity * summerRate + summerServiceCharge;
} else {
return quantity * winterRate + winterServiceCharge;
}
}
// After
function charge(date: Date, quantity: number): number {
return isSummer(date) ? summerCharge(quantity) : winterCharge(quantity);
}
function isSummer(date: Date): boolean { return date >= SUMMER_START && date <= SUMMER_END; }
function summerCharge(qty: number): number { return qty * summerRate + summerServiceCharge; }
function winterCharge(qty: number): number { return qty * winterRate + winterServiceCharge; }