嵌入式if语句的替代方法? [英] Alternatives to embedded if statements?

查看:47
本文介绍了嵌入式if语句的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有编程方面的历史,但是在软件开发方面没有很多.我目前正在为我工​​作的公司编写一款软件,而我开始在代码的可读性方面挑战自己.

I have a history in programming, but not much in software development. I'm currently writing a piece of software for the company I work at, and I've come to challenge myself on the readability of my code.

我想知道这是否是嵌入式if语句的有效"替代方法,还是我可以使用的更好的方法.

I want to know whether this is a "valid" alternative to embedded if statements, or if there is anything better I could use.

假设我有以下方法:

public void someMethod()
{
    if (some condition)
    {
        if (some condition 2)
        {
            if (some condition 3)
            {
                // ...etc all the way until:
                doSomething();
            }
            else
            {
                System.err.println("Specific Condition 3 Error");
            }
        }
        else
        {
            System.err.println("Specific Condition 2 Error");
        }
    }
    else
    {
        System.err.println("Specific Condition 1 Error");
    }
}

现在我首先要指出的是,在这种情况下,不可能将条件(与&&)组合起来,因为每个条件都有一个我要报告的独特错误,并且如果我将它们组合在一起我将无法做到这一点(或者可以吗?).在任何人尖叫"SWITCH STATEMENT!"之前,我应该指出的第二件事.我的意思是,并非所有这些条件都可以通过switch语句来处理;有些是对象特定的方法调用,有些是整数比较,等等.

Now the first thing I should point out is that in this instance, combining the conditions (with &&) isn't possible, since each one has a unique error that I want to report, and if I combined them I wouldn't be able to do that (or would I?). The second thing I should point out before anyone screams "SWITCH STATEMENT!" at me is that not all of these conditions can be handled by a switch statement; some are Object specific method calls, some are integer comparisons, etc.

也就是说,以下是使上述代码更具可读性的有效方法,还是有更好的方法呢?

That said, is the following a valid way of making the above code more readable, or is there a better way of doing it?

public void someMethod()
{
    if (!some condition)
    {
        System.err.println("Specific Condition 1 Error");
        return;
    }

    if (!some condition 2)
    {
        System.err.println("Specific Condition 2 Error");
        return;
    }

    if (!some condition 3)
    {
        System.err.println("Specific Condition 3 Error");
        return;
    }

    doSomething();
}

因此,基本上,我们不检查条件并在else块中报告错误,而是检查条件的反函数,如果为真则返回.结果应该是相同的,但是有更好的方法来处理吗?

So basically, instead of checking for conditions and reporting errors in else blocks, we check for the inverse of the condition and return if it is true. The result should be the same, but is there a better way of handling this?

推荐答案

如果我是特别学究的人,我会使用类似的东西.

If I was being particularly pedantic I would use something like this.

boolean c1, c2, c3;

public void someMethod() {
  boolean ok = true;
  String err = "";

  if (ok && !(ok &= c1)) {
    err = "Specific Condition 1 Error";
  }

  if (ok && !(ok &= c2)) {
    err = "Specific Condition 2 Error";
  }

  if (ok && !(ok &= c3)) {
    err = "Specific Condition 3 Error";
  }

  if ( ok ) {
    doSomething();
  } else {
    System.out.print(err);
  }
}

您现在是单出口且单位持平.

You are now single-exit AND flat.

已添加

如果& =对您来说很困难,请使用类似的内容:

If &= is difficult for you, use something like:

  if (ok && !c3) {
    err = "Specific Condition 3 Error";
    ok = false;
  }

这篇关于嵌入式if语句的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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