Search code examples
javascriptdesign-patternsrefactoring

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?


Solution

  • 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);
    }