Using Decompose Conditional
Decompose Conditional is a refactoring technique where the logic within a conditional statement (if-else
or switch
) is extracted into separate functions. This technique helps to improve the readability of the code, making it more understandable.
The condition, if
clause, and else
clause (if it exists) all get their own function. These functions are then named according to what they do or what they are checking for. This refactoring is beneficial because it replaces code that might need comments to understand with well-named functions, making the code self-explanatory.
For example, the logic in the applyDiscountIfEligible
function can actually be simplified by this refactoring; we can extract a small function called isDiscountEligible
to replace the item.quantity > 10
check, like in the following:
function isDiscountEligible(item: Item) { return item.quantity > 10; } function applyDiscountIfEligible(item: Item, subTotal: number...