嵌套“如果” - “别人的语句 [英] Nested 'if'-'else' statements

查看:91
本文介绍了嵌套“如果” - “别人的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码很凌乱与如果 - 其他检验它是否在做什么。分支和嵌套分支的量是相当大的(超过20如果 - 否则,如果并嵌套太)。它使我的代码难于阅读,并可能是一个性能猪。对于很多它会从用户等应用程序获取条件,我的应用程序检查,必须检查所有的时间针对不同的情况,例如:

I have code that's very messy with the if - else if checks it is doing. The amount of branching and nested branching is quite big (over 20 if - else if and nested too). It's making my code harder to read and will probably be a performance hog. My application checks for a lot of conditions it gets from the user and so the application must check all the time for different situations, for example:

如果文本框中的文本不0,继续下一个...

If the textbox text is not 0, continue with the next...

if ((StartInt != 0) && (EndInt != 0))   
{

和然后在这里它检查用户是否已经选择日期:

And then here it checks to see whether the user have choosen dates:

if ((datePickerStart.SelectedDate == null) || (datePickerEnd.SelectedDate == null)) 
{
    MessageBox.Show("Please Choose Dates");
}



在这里,如果datepickers不为null,则继续代码...

Here, if the datepickers aren't null it continues with code...

else if ((datePickerStart.SelectedDate != null) && (datePickerEnd.SelectedDate != null))
{
    // CONDITIONS FOR SAME STARTING DAY AND ENDING DAY.
    if (datePickerStart.SelectedDate == datePickerEnd.SelectedDate)
    {
        if (index1 == index2)
        {
            if (StartInt == EndInt)
            {
                if (radioButton1.IsChecked == true)
                {
                    printTime3();
                }
                else
                {
                    printTime();
                }
            }

这是检查的只是一小部分正在取得。其中有些是泛函和一些输入验证的东西。

This is just a small part of the checks being made. Some of them are functionals and some are for input validation stuff.

有什么办法,使之更具可读性和更小的性能猪的?

Is there any way to make it more readable and less of a performance hog?

推荐答案

这不是一个性能猪的。关于如何解决这些常见问题的一个伟大的博客文章是的 压扁箭头代码

It's not that of a performance hog. A great blog post on how to fix those common problems is Flattening Arrow Code.

这篇关于嵌套“如果” - “别人的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆