优化一大组 IF 语句

发布于 2024-12-04 07:37:21 字数 1416 浏览 1 评论 0原文

我有一大组嵌套 IF 语句,我想知道是否有人对如何优化速度、大小和可读性有任何建议。

下面是一个 if 语句及其嵌套语句的示例。文件中大约有 25-30 个这样的内容。

if( $row["inlet_moisture"] > $row["inlet_moisture_high_warning"] ) {
    if( $row["inlet_moisture"] > $row["inlet_moisture_high_critical"] ) {
        if( $row["inlet_high_critical"] == 0 ) {
            if( $row["email_notification"] == 1 ) {

            }
            if( $row["mobile_notification"] == 1 ) {

            }
        }
    } else {
        if( $row["inlet_high_warning"] == 0 ) {
            if( $row["email_notification"] == 1 ) {

            }
            if( $row["mobile_notification"] == 1 ) {

            }
        }
    }
} else if( $row["inlet_moisture"] < $row["inlet_moisture_low_warning"] ) {
    if( $row["inlet_moisture"] < $row["inlet_moisture_low_critical"] ) {
        if( $row["inlet_low_critical"] == 0 ) {
            if( $row["email_notification"] == 1 ) {

            }
            if( $row["mobile_notification"] == 1 ) {

            }
        }
    } else {
        if( $row["inlet_low_warning"] == 0 ) {
            if( $row["email_notification"] == 1 ) {

            }
            if( $row["mobile_notification"] == 1 ) {

            }
        }
    }
}

这个想法是;我有一个读数(温度/速度/湿度),我需要检查它是否达到任何限制(高警告/高临界/低警告/低临界),如果是,我首先需要检查我是否已经为此发出警报。如果没有发送警报,我需要检查用户是否请求了警报通知(手机/电子邮件/两者),

目前这是有效的。我只是不喜欢它有多重?我可以在这方面改进吗?

谢谢。

I have a large group of nested IF statements and I was wondering If anyone had any suggestions on how to optimize for speed, size, and readability.

Below is a sample of ONE of the if statements and its' nested statements. There will be approximately 25-30 of these in the document.

if( $row["inlet_moisture"] > $row["inlet_moisture_high_warning"] ) {
    if( $row["inlet_moisture"] > $row["inlet_moisture_high_critical"] ) {
        if( $row["inlet_high_critical"] == 0 ) {
            if( $row["email_notification"] == 1 ) {

            }
            if( $row["mobile_notification"] == 1 ) {

            }
        }
    } else {
        if( $row["inlet_high_warning"] == 0 ) {
            if( $row["email_notification"] == 1 ) {

            }
            if( $row["mobile_notification"] == 1 ) {

            }
        }
    }
} else if( $row["inlet_moisture"] < $row["inlet_moisture_low_warning"] ) {
    if( $row["inlet_moisture"] < $row["inlet_moisture_low_critical"] ) {
        if( $row["inlet_low_critical"] == 0 ) {
            if( $row["email_notification"] == 1 ) {

            }
            if( $row["mobile_notification"] == 1 ) {

            }
        }
    } else {
        if( $row["inlet_low_warning"] == 0 ) {
            if( $row["email_notification"] == 1 ) {

            }
            if( $row["mobile_notification"] == 1 ) {

            }
        }
    }
}

The idea is; I have a reading (temp/speed/moisture) and I need to check if it hits any of the limits (high warning / high critical / low warning / low critical), if it does I first need to check if I have already sent an alarm for that. If no alarm has been sent I then need to check if the user has requested alarm notification (mobile/email/both)

Currently this works. I Just don't like how heavy it is? Can I improve on this?

Thanks.

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

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

发布评论

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

评论(2

浅忆 2024-12-11 07:37:21

在我看来,这似乎更清楚,尽管你可以组合嵌套的 if,但我更喜欢这个

if( $row["inlet_moisture"] > $row["inlet_moisture_high_critical"] ) {
  if( $row["inlet_high_critical"] == 0 ) {
   $message = 'the pertinent message';
  }
}
else if( $row["inlet_moisture"] > $row["inlet_moisture_high_warning"] ) {
  if( $row["inlet_high_warning"] == 0 ) {
   $message = 'the pertinent message';
  }
}
else if( $row["inlet_moisture"] < $row["inlet_moisture_low_critical"] ) {
  if( $row["inlet_low_critical"] == 0 ) {
   $message = 'the pertinent message';
  }
}
else if( $row["inlet_moisture"] < $row["inlet_moisture_low_warning"] ) {
  if( $row["inlet_low_warning"] == 0 ) {
   $message = 'the pertinent message';
  }
}


if( $row["email_notification"] == 1 ) {
  sendMessage($message, $email);
}
if( $row["mobile_notification"] == 1 ) {
  sendMessage($message, $mobile);    
}

this seems to me much more clear, even though you could combine the nested ifs I'd rather prefer this one

if( $row["inlet_moisture"] > $row["inlet_moisture_high_critical"] ) {
  if( $row["inlet_high_critical"] == 0 ) {
   $message = 'the pertinent message';
  }
}
else if( $row["inlet_moisture"] > $row["inlet_moisture_high_warning"] ) {
  if( $row["inlet_high_warning"] == 0 ) {
   $message = 'the pertinent message';
  }
}
else if( $row["inlet_moisture"] < $row["inlet_moisture_low_critical"] ) {
  if( $row["inlet_low_critical"] == 0 ) {
   $message = 'the pertinent message';
  }
}
else if( $row["inlet_moisture"] < $row["inlet_moisture_low_warning"] ) {
  if( $row["inlet_low_warning"] == 0 ) {
   $message = 'the pertinent message';
  }
}


if( $row["email_notification"] == 1 ) {
  sendMessage($message, $email);
}
if( $row["mobile_notification"] == 1 ) {
  sendMessage($message, $mobile);    
}
最后的乘客 2024-12-11 07:37:21

过早优化是万恶之源 - 对于我们在这里处理的内容,无论您做什么这样做不会对性能产生太大/任何明显的影响。

话虽如此,大量 if 语句通常可以替换为一个或多个 switch 结构,尽管这是否会提高性能或提高可读性尚不清楚有争议的。您还可以为重复的代码创建一些函数,尽管这实际上可能会对性能产生负面影响。

根据您上面的评论...创建具有更好名称的变量对性能的影响几乎为零。如果会稍微增加您的内存使用量,但处理时间的影响将接近于零。而且,如果您将值评估为布尔值,则无需将它们显式转换为布尔值,因为 1 仍评估为 TRUE,0 仍评估为 FALSE。但是,如果您确实想要这样做

$email_notification = $row["email_notification"] == 1 ? true : false;

...不必要的冗长,您可以执行以下操作:

$email_notification = $row["email_notification"] == 1;

...或...

$email_notification = (bool) $row["email_notification"];

...并且它将具有相同的效果。

Premature optimisation is the root of all evil - with what we are dealing with here, no matter what you do it won't have much/any noticable impact on performance.

Having said that, a large amount of if statements can often be replaced with one or more switch structures, although whether this improves performance or readability is debatable. You may also be able to create some functions for repeated bits of code, although this may actually negatively impact performance.

From your comment above... creating variables with nicer names will have pretty much zero impact on performance. If will slightly increase your memory usage, but the processing time impact will be next to zero. And, if you are evaluating the values as booleans, you don't need to convert them to booleans explicitly, because 1 still evaluates to TRUE, and 0 to FALSE. However, if you do want to do it

$email_notification = $row["email_notification"] == 1 ? true : false;

...is unnecessarily long winded, you can do either this:

$email_notification = $row["email_notification"] == 1;

...or...

$email_notification = (bool) $row["email_notification"];

...and it will have the same effect.

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