简体   繁体   English

重构空的 if 语句

[英]Refactoring empty if-statements

I currently working on a project that I need to remove a class that is being used by different other classes.我目前正在处理一个项目,我需要删除一个被其他不同类使用的类。 There are cases that I can remove the one line of code that consists of that class where it will never affect the functionality of the program, but also there are cases that the class that you want to be removed is inside an if-statement.在某些情况下,我可以删除包含该类的一行代码,它永远不会影响程序的功能,但在某些情况下,您要删除的类在 if 语句中。 The main problem is that once I removed the line of code consisting of that class where is it inside the if-statement, it will be an empty if-statement that will violates the sonar.主要问题是,一旦我删除了由该类组成的代码行,它在 if 语句中的位置,它将是一个空的 if 语句,它将违反声纳。

Is there another way to refactor an empty if-statement other that negating the condition of one of the statements?除了否定其中一个语句的条件之外,还有其他方法可以重构一个空的 if 语句吗? Because when I'm just negating the condition, the readability of the code reduced.因为当我只是否定条件时,代码的可读性降低了。

For Example:例如:

if((example_A >= 0) && (condition_A))
{
     removeThisClass();
}
else if((example_B >= )) && (condition_B))
{
     doSomething();
}
else
{
     doAnything();
}

Refactored:重构:

if(!((example_A >= 0) && (condition_A)) && ((example_B >= )) && (condition_B)))
{
     doSomething();
}
else
{
     doAnything();
}

You can put this code in separate method ( https://refactoring.com/catalog/extractFunction.html ) and write it like this:您可以将此代码放在单独的方法中( https://refactoring.com/catalog/extractFunction.html )并像这样编写:

public void DoSomeStuff() {

  if((example_A >= 0) && (condition_A))
    return;  

  if((example_B >= )) && (condition_B)) {
    doSomething();
    return;
  }

  doAnything();
}    

If I understand you right, the line removeThisClass();如果我理解正确,那行removeThisClass(); should be removed, and you don't want to be left with an empty block like this:应该删除,并且您不希望留下这样的空块:

if((example_A >= 0) && (condition_A))
{
}
else if((example_B >= )) && (condition_B))
{
    doSomething();
}
else
{
    doAnything();
}

In order to not do the "A" tests twice, you need to negate the condition, eg like this:为了不进行两次“A”测试,您需要否定条件,例如:

if ((example_A < 0) || ! (condition_A))
{
    if ((example_B >= )) && (condition_B))
    {
        doSomething();
    }
    else
    {
        doAnything();
    }
}

Your refactored code is wrong, because if the "A" condition is true, the original code would execute removeThisClass();您重构的代码是错误的,因为如果“A”条件为真,则原始代码将执行removeThisClass(); , which means it should now do nothing, but your code will call doAnything(); ,这意味着它现在应该什么都不做,但是你的代码会调用doAnything(); when "A" is true.当“A”为真时。

You can put in a comment.你可以发表评论。 Sonar should accept that and it could also help the reader.声纳应该接受这一点,它也可以帮助读者。

void doSomething() {
  for (int i = 0; i < 42; i++)        // Non-Compliant
  {
  }
  for (int i = 0; i < 42; i++);       // Compliant

  if (myVar == 4)                     // Compliant - contains a comment
  {
    // Do nothing because of X and Y
  }
  else                                // Compliant
  {
    doSomething();
  }

  try                                 // Non-Compliant
  {
  }
  catch (Exception e)                 // Compliant
  {
    // Ignore
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM