Return to site

CODING TIP: Encapsulate ifs inside their methods, not outside them

· java

BAD EXAMPLE

if (value > 500) {
    applyDiscountForGreaterThan500();
} else if (value > 1000) {
    applyDiscountForGreaterThan1000();
}

GOOD EXAMPLE

// encapsulated ifs in the methods
applyDiscountIfValueIsGreaterThan500();
applyDiscountIfValueIsGreaterThan1000();