JavaScript-概括两个类似的功能

发布于 2025-01-31 03:58:40 字数 1328 浏览 4 评论 0原文

我有两种类似的方法:

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

和:

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

如您所见,这两种方法的代码确实相似,只有数字更改。

假设计算“启发性”的数字(通过假设,没有任何形式的公式)。

我可以使用哪种模式将这两种方法概括为一种“ getFeedPlatesminmineDate”?当代码原子性高高时,我应该避免概括代码吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2025-02-07 03:58:40

我会为披萨做这样的事情。通过将IF语句中的值更改为type ==='意大利面'中的意大利面条。如果您使用的是Typescript,也可以直接注入类型值可以具有哪种类型的类型。否则,也可以选择使用枚举。

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

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);
}
兔姬 2025-02-07 03:58:40

您只需传递一个称为数据的JSON对象,带有您想要的数字或使用Switch语句或if语句的JSON对象,该对象与数据已经存在

第一个

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

选项两个

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

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;
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文