有没有办法重构这段代码,避免使用多个if语句?

huangapple go评论64阅读模式
英文:

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 &amp;&amp; isCurrentHourWarnings) {
            style = 4;
        } else if (isCurrentHourAlert) {
            style = 4;
        } else if (isCurrentHourWarnings) {
            style = 5;
        } else if (alert &amp;&amp; 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 =&gt; markers.some(marker =&gt; marker[prop] &gt; 0);
let style = 0;
if (markersHas(&#39;isCurrentHourAlert&#39;)) {
    style = 4;
} else if (markersHas(&#39;isCurrentHourWarnings&#39;)) {
    style = 5;
}
// etc

If you want to use switch, it'd look like

switch (true) {
  case markersHas(&#39;isCurrentHourAlert&#39;):
    style = 4;
    break;
  case markersHas(&#39;isCurrentHourWarnings&#39;):
    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(&#39;isCurrentHourAlert&#39;), 4],
    [markersHas(&#39;isCurrentHourWarnings&#39;), 5],
    // etc
];
const styleCond = styleConds.find(cond =&gt; 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?)

huangapple
  • 本文由 发表于 2023年3月12日 08:09:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定