【问题标题】:Leaving the code in an else if() blank in order to escape the if statement将 else if() 中的代码留空以转义 if 语句
【发布时间】:2020-02-07 15:26:38
【问题描述】:

对于这个奇怪的标题,我很抱歉,我找不到我想要的确切措辞,但我会尽力在这里解释我的问题。基本上我有一些这样的代码

if(both inputs are not null)
{
    Do this
}
else if(both inputs are null)
{

}
else if(one input is null and the other isn't)
{
    throw new Exception("Both inputs must have a value or neither should");
}

如果我不使用中间的else if 最后一个else if,无论一个输入还是两个输入都为null,程序都会抛出异常。我想要它,以便程序看到两个输入都是空的,并且在继续执行时什么都不做。我正在使用这些数据传递给 SQl 查询,如果其中一个输入为空,它就会起作用。我可能只是搞砸了逻辑,但我想知道这是否被认为是不好的做法。我想不出问题,因为这不可能意外执行代码。如果有更好的方法或者这被认为是不好的做法,我想听听其他方法来解决这个问题。谢谢。

编辑:澄清问题

【问题讨论】:

  • 我在您的问题中添加了 Java 语言标签。编辑您的问题并添加正确的语言标签。它将有助于在您的问题和答案中突出显示代码。

标签: java if-statement logic


【解决方案1】:

我想我在这里遗漏了一些东西。您的描述似乎与您的代码不符。 在您编写的伪代码中,如果两个输入均为空,则不应发送异常,这就是您想要的。但是您是说仍然发送异常?这里有东西。您可以发布更接近实际代码的内容吗? 您所描述的似乎更接近switch case 的行为,其中一个空的“案例”会掉到下一个。

不管怎样,您可以通过重新排序测试来避免那些空的“else if”:

if(both inputs are not null)
{
    Do this
}
else if(one input is null and the other isn't)
{
     throw new Exception("Both inputs must have a value or neither should");
}

这样,不需要额外的空else if

【讨论】:

  • 我为造成的混乱道歉。我的意思是,如果我不包括那个中间空的,否则它会抛出异常。使用空的 else if 它简单地跳过最后一个 else if 并继续执行。我想知道这是否被认为是一个坏习惯,是否是一种解决方法。谢谢。
【解决方案2】:

如果您使用的语言有异或运算符,您可以简化代码。例如在 C# 中:

string A = null;
string B = "Hello World";

if ( A != null && B != null)
{
    // Do this
}
else if ( A == null ^ B == null )
{
    throw new Exception("Both inputs must have a value or neither should");
}       

如果 x 计算结果为真且 y 计算结果为假,则 x ^ y 的结果为真,或者 x 计算结果为假且 y 计算结果为真。

【讨论】:

  • 哇这真的很有用,谢谢分享。
【解决方案3】:

在我看来,最简单的解决方法是将您的代码更改为以下

if(both inputs are not null)
{
    Do this
}
else if((input1==null && input2!=null) || (input1!=null && input2==null))
{
    throw new Exception("Both inputs must have a value or neither should");
}

【讨论】:

    【解决方案4】:

    在自己的方法中重构出这个逻辑,然后写成如下:

    private void refactoredMethod(Input i1, Input i2) {
        //Do nothing if both inputs are null.
        if (i1 == null && i2 == null)
            return;
    
        //Throw if either of them is null.
        if (input1 == null || input2 == null)
            throw ...
    
        //Neither input is null, do the normal processing.
        //so, "Do this"
    }
    

    【讨论】:

      【解决方案5】:

      您为什么不检查任何一个输入是否为空? 然后你可以抛出一个异常并在没有抛出异常的情况下继续。

      类似这样的东西(用 java 术语):

      if(firstInput == null || secondInput == null) {
          throw new IllegalArgumentException("Input must not be null");
      }
      
      // do what you want afterwards
      

      【讨论】:

      • 不应该是||吗?我不懂 Java。
      • 我选择不使用它,因为我希望程序不关心两个输入是否为空,而当只有一个输入为空时抛出异常。对困惑感到抱歉。谢谢
      【解决方案6】:

      这个怎么样:

      bool A = (input1 == null), B = (input2 == null);
      if (A != B) {
          throw new Exception("Both inputs must have a value or neither should");
      }
      

      【讨论】:

        【解决方案7】:

        我了解源代码有时在某些条件下使用空块更易于阅读。我认为这是你想要做的。示例:

        if(street!=null && zip!=null)
        {
            storeAddress(street,zip);
        }
        else if(street==null && zip==null)
        {
            ; // Do nothing
        }
        else // only one of street or zip was provided
        {
            throw new Exception("Street and zip code must be filled together or both left empty");
        }
        

        我在这里使用分号来避免来自 SpotBugs 的警告。这样我就可以告诉 Spotbugs(和其他开发人员)该块是故意为空的。

        你的例子的最后一个条件是多余的,所以我把它变成了评论。

        【讨论】:

        • 只是为了澄清离开这个中间如果空白没有害处?我认为创建这样的语句没有问题,但同时它看起来是错误的。我主要担心最佳实践,因为我知道它不会导致我的代码出现问题。
        • 不,它是无害的。 Java 语言允许空块。我不会想太多关于最好的赞美。如果它有效并且意图很明确,那为什么不呢?
        猜你喜欢
        • 2019-04-09
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 2018-03-29
        相关资源
        最近更新 更多