I have two similar methods:
function getFeedPizzasMinimumDate(startDate, totalPizzas) {
// 2 weeks if there are less than 10 pizzas
let numberOfDaysToDecrase = 14;
// 3 days if there are more than 1k pizzas
if (totalPizzas >= 1000) numberOfDaysToDecrase = 3;
// 5 days if there are from 100 to 1k pizzas
else if (totalPizzas >= 100) numberOfDaysToDecrase = 5;
// 1 week if there are from 10 to 100 pizzas
else if (totalPizzas >= 10) numberOfDaysToDecrase = 7;
return substractDaysToDate(numberOfDaysToDecrase, startDate);
}
and:
function getFeedSpaghettisMinimumDate(startDate, totalSpaghettis) {
// 3 weeks if there are less than 10 spaghettis
let numberOfDaysToDecrase = 21;
// 5 days if there are more than 1k spaghettis
if (totalSpaghettis >= 1000) numberOfDaysToDecrase = 5;
// 1 week if there are from 100 to 1k spaghettis
else if (totalSpaghettis >= 100) numberOfDaysToDecrase = 7;
// 2 weeks if there are from 10 to 100 pizzas
else if (totalSpaghettis >= 10) numberOfDaysToDecrase = 14;
return substractDaysToDate(numberOfDaysToDecrase, startDate);
}
As you can see, the code of both methods is really similar, only the numbers change.
Suppose that the numbers where calculates "heuristacally" (via suppositions, without any kind of formula).
Which pattern can I use to generalize both methods into one "getFeedPlatesMinimumDate"? Should I avoid generalizing code when code atomicity is as high?
Aucun commentaire:
Enregistrer un commentaire