如何预防如果……如果…… [英] How to prevent if if if if if if...?

查看:69
本文介绍了如何预防如果……如果……的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个人困扰了我一段时间.我需要检查8个值附近是否不是Nothing.我可能不需要一次全部检查它们,而是一次检查几次,然后再检查一些嵌套的If.它使代码丑陋且难以阅读,但是如何防止这么多的If或至少使其更具可读性和可读性呢?
Select Case不会,因为需要评估所有值.

那么如何使这样的代码更具可读性呢?

This one has been bothering me for a while. I need to check if somewhere around 8 values are not Nothing. I might not need to check them all at once, but a few at a time and then some nested If''s. It makes code ugly and unreadable, but how to prevent so many If''s or at least make it more readable and understandable?
A Select Case will not do, since ALL values need to be evaluated.

So how could I possibly make code like this better readable?

If Not object1.Property1 Is Nothing AndAlso Not object2.Property1 Is Nothing AndAlso Not object2.Property1.Property1 Is Nothing Then

   Dim object3 = ' LINQ Query.

   If Not object3 Is Nothing Then

      If Not object4 Is Nothing AndAlso Not object3.Property1 Is Nothing Then
      '...
      ElseIf '...
      '...
      End If

   End If

End If


我可以将其中一些放在单独的Method中,但这只会解决问题,而不能解决问题.我确信这是一个已经存在多年的问题,但是我从未找到明确的答案...


I could put some of it in a seperate Method, but that would only move the problem, not solve it. I am sure this is a problem that has existed for many many years, but I have never found a clear answer to it...

推荐答案

如果无法阻止,您可以设计使用更少的"if"的代码.您是否在这里谈到了完全数据驱动的开发风格,而应用程序代码中没有一个"if"?对我来说,这太过分了,但许多人会说这是不可能的.有可能.

以我的口味,如果仍然可以接受两个级别的if,则另一个级别是有问题的,而另一个级别则完全是不可接受的.您需要重构代码,但是最好对整体外观制定一个良好的计划.考虑大型编程级别有助于避免首先进行多级检查,但是在本地,您可以重构代码.特别是,使用本地谓词是一个非常方便的主意.

顺便说一下,本地谓词怎么样.有方法,没有本地方法之类的东西,是吗?
如果您需要一些鼓舞人心的示例,请尝试查看我的小技巧/窍门文章:
If cannot prevent it, you can design you code the way it uses much less "ifs". Did you here about totally data-driven development style where there are no a single "if" in application code? To me, this is overkill, but many would say it''s not possible. It is possible.

To my taste, two levels of if is still quite acceptable, one more is questionable and more it totally unacceptable. You need to re-factor the code, but better have a good plan on how the whole thing should look like. Thinking on the programming-in-the-large level helps to avoid multilevel checks in first place, but in local place you can re-factor the code. In particular, using local predicates is a very convenient idea.

By the way, how about local predicate. There are methods, and there is no such thing as local method, or it is?
If you need some inspirational example, try to look at my small Tips/Tricks article: Hide Ad-hoc Methods Inside the Calling Method’s Body[^].

I do not insist you use this technique, not at all. I think just the ideas of thinking could be useful for you.

—SA


我不知道VB,因此如果有帮助,将尝试使用C#进行解释.

三个级别的if/else if很好,但超过3个级别则为否.这是您可以摆脱ifs嵌套的方法.

假定以下类结构:

I do not know VB so will try to explain using C# if it helps.

Three levels of if/else if fine but more than that is a no. This is something you could do to get rid nested of ifs.

Assume following class structure:

class ParentClass
{
    public ChildClass ChildProperty { get; set; }
}
class ChildClass
{
    public int SomeProperty { get; set; }
}



说,您需要执行以下处理:



Say, you need to do this kind of processing:

ParentClass parentObj1 = new ParentClass();
ParentClass parentObj2 = new ParentClass();
ParentClass parentObj3 = new ParentClass();
if (parentObj1 != null && parentObj2.ChildProperty != null && parentObj2.ChildProperty.SomeProperty != null)
{
    ParentClass parentObj4 = new ParentClass();
    if (parentObj4 != null)
    {
        if (parentObj3 != null && parentObj4.ChildProperty != null)
        {
            if (parentObj4.ChildProperty.SomeProperty != null)
            {
            }
            else
            {
            }
        }
        else if (parentObj3 != null && parentObj3.ChildProperty != null && parentObj4.ChildProperty.SomeProperty != null)
        {
        }
        else
        {
        }
    }
}



可以这样重写:



This can be re-written as following:

if (parentObj1 != null && parentObj2.ChildProperty != null && parentObj2.ChildProperty.SomeProperty != null)
{
    ParentClass ob3 = new ParentClass();
    switch (ValidateObjects(parentObj3, ob3))
    {
        case 0:
            // Do something
            break;
        case 1:
            // Do something
            break;
        case 2:
            // Do something
            break;
        case 3:
            // Do something
            break;
        default:
            //Do something
            break;
    }
}



ValidateObjects方法可能如下所示:



ValidateObjects method could look like this:

private static int ValidateObjects(ParentClass ob4, ParentClass ob3)
{
    int result =0;
    if (ob3 != null && ob4 != null && ob4.ChildProperty != null && ob3.ChildProperty.SomeProperty != null)
    {
        result = 0;
    }
    else if (ob3 != null && ob4 != null && ob3.ChildProperty != null && ob3.ChildProperty.SomeProperty != null)
    {
        result = 1;
    }
    else if (ob3 != null && ob4 != null && ob3.ChildProperty != null)
    {
        result = 2;
    }
    else if (ob3 != null)
    {
        result = 3;
    }
    return result;
}



您也可以在上述方法中选择开关盒.

不过,您对应用程序有更好的了解,因此可以考虑更好的解决方案. :)



You can opt for switch case in the method above too.

Still, you have a better understanding of the application so can think of better solutions. :)


在不了解您的代码的更多情况下,我必须告诉您,您执行此操作的方法是唯一的方法.您可以*将比较抽象为几种方法,但必须仔细设计.
Without knowing more about your code, I have to tell you that the way you''re doing it is the only way. You *could* abstract out the comparisons into a few methods, but you''d have to design it carefully.


这篇关于如何预防如果……如果……的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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