英文:
is there any way to refactor this code and avoid the use of all multiples if statement?
问题
我尝试重构这段代码,以避免使用太多的if语句,您有什么建议可以给我吗?问题在于我有4个不同的输入来给出一个答案。
const alert = markers.some((marker) => marker['hasAlerts'] > 0);
const warning = markers.some((marker) => marker['hasWarnings'] > 0);
const isCurrentHourAlert = markers.some((marker) => marker['isCurrentHourAlert'] > 0);
const isCurrentHourWarnings = markers.some((marker) => marker['isCurrentHourWarnings'] > 0);
let style = 0;
if (isCurrentHourAlert && isCurrentHourWarnings) {
style = 4;
} else if (isCurrentHourAlert) {
style = 4;
} else if (isCurrentHourWarnings) {
style = 5;
} else if (alert && warning) {
style = 2;
} else if (alert) {
style = 2;
} else if (warning) {
style = 3;
} else {
style = 1;
}
我尝试将这段代码转换为switch case语句,但仍然需要大量的验证。我想知道是否可以应用一些模式或其他结构,使代码更易阅读和重构。
英文:
I am trying to refactor this code to avoid the use of so many if statements, any suggestions you can give me? . The problem is that I have 4 different inputs to give a single answer.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
const alert= markers.some((marker) => marker['hasAlerts'] > 0);
const warning= markers.some((marker) => marker['hasWarnings'] > 0);
const isCurrentHourAlert = markers.some((marker) => marker['isCurrentHourAlert'] > 0);
const isCurrentHourWarnings = markers.some((marker) => marker['isCurrentHourWarnings'] > 0);
let style = 0;
if (isCurrentHourAlert && isCurrentHourWarnings) {
style = 4;
} else if (isCurrentHourAlert) {
style = 4;
} else if (isCurrentHourWarnings) {
style = 5;
} else if (alert && warning) {
style = 2;
} else if (alert) {
style = 2;
} else if (warning) {
style = 3;
} else {
style = 1;
}
<!-- end snippet -->
I tried converting this to a switch case statement, but it's still code with a lot of validation.
I would like to know if I can apply some pattern or other structure that is easier to read and to refactor the code.
答案1
得分: 0
以下是翻译好的部分:
一个函数,它迭代标记并检查您感兴趣的属性将会有所帮助。
无论是否为“isCurrentHourAlert”为true,您都会运行`style = 4;`,因此您可以完全消除第一个分支。
如果您想使用`switch`,它会看起来像这样:
另一个选项是使用条件/样式对的数组,然后通过数组进行迭代,以找到第一个为真的条件,并使用其关联的样式。
从更高层次的角度来看,您还可以考虑是否有办法完全避免这些神秘的数字 - 如果可能的话,最好使用更具信息性的内容。 (例如,像`style = 4;`这样的行在一瞥之间并不太有意义 - 4代表什么?)
英文:
A function that iterates over the markers and checks for the property you're interested in would help some.
You run style = 4;
regardless if isCurrentHourAlert
is true, so you can eliminate the first branch entirely.
const markersHas = prop => markers.some(marker => marker[prop] > 0);
let style = 0;
if (markersHas('isCurrentHourAlert')) {
style = 4;
} else if (markersHas('isCurrentHourWarnings')) {
style = 5;
}
// etc
If you want to use switch
, it'd look like
switch (true) {
case markersHas('isCurrentHourAlert'):
style = 4;
break;
case markersHas('isCurrentHourWarnings'):
style = 5;
break;
// etc
Another option is to use an array of conditions / style pairs, then iterate through the array to find the first truthy condition, and use its associated style.
const styleConds = [
[markersHas('isCurrentHourAlert'), 4],
[markersHas('isCurrentHourWarnings'), 5],
// etc
];
const styleCond = styleConds.find(cond => cond[0]);
const style = styleCond?.[1] ?? 0;
From a higher level perspective, you also might consider if there's a way to avoid these sorts of magic numbers entirely - better to use something more informative if at all possible. (for example, a line like style = 4;
doesn't make all that much sense at a glance - what does 4 mean?)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论