JavaScript-概括两个类似的功能
我有两种类似的方法:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会为披萨做这样的事情。通过将IF语句中的值更改为type ==='意大利面'中的意大利面条。如果您使用的是Typescript,也可以直接注入类型值可以具有哪种类型的类型。否则,也可以选择使用枚举。
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.
您只需传递一个称为数据的JSON对象,带有您想要的数字或使用Switch语句或if语句的JSON对象,该对象与数据已经存在
第一个
选项两个
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
Option two