简体   繁体   中英

Javascript - Generalizing two similar functions

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?

I would do something like this for the pizza. Do the same for the spaghettis by changing the value in the if statement to type === 'spaghettis'. If you are using typescript, you can also directly inject what kind of types the type value can have. Otherwise, using ENUMS can also be an option.

function getFeedMinimumDate(startDate, amount, type = 'pizza') {
  // default
  let numberOfDaysToDecrase = 14;

  // 3 days if there are more than 1k pizzas
  if (type === 'pizza' && amount >= 1000) numberOfDaysToDecrase = 3;

  // 5 days if there are from 100 to 1k pizzas
  if (type === 'pizza' && amount >= 100) numberOfDaysToDecrase = 5;

  // 1 week if there are from 10 to 100 pizzas
  if (type === 'pizza' && amount >= 10) numberOfDaysToDecrase = 7;

  return substractDaysToDate(numberOfDaysToDecrase, startDate);
}

You can just pass a json object called data, with the numbers you want or use a switch statement or an if statement for json objects with the data already there

First Option

const data = {
  Pizza:10,
  Slices:20
}
function allFood(data){
  return data.Pizza*data.Slices
}

Option two

function allFood(Food){
//use switch statement or if statement
 const PData={amount:10,fed:10};  //pizza data
 const SData={amount:5,fed:15}; //spaghetti data
 if (Food ==='Pizza'){
  return PizzaData.amount*PizzaData.fed;
 }else if(Food ==='Spaghetti'){
  return SpaghettiData.amount*SpaghettiData.fed;
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM