简体   繁体   English

简化陈述

[英]Simplifying statements

I am working on a project and I am using Intellij IDEA. 我正在做一个项目,并且正在使用Intellij IDEA。 While I was writing I got a notification that my if-statement could be simplified. 在我写作时,我收到一条通知,告知我可以简化if语句。 (Note that I am still new to coding) (请注意,我还是编码新手)

It says: 它说:

Reports if statements which can be simplified to single assignment or return statements. 报告if语句,可以将其简化为单个赋值或return语句。 For example: 例如:

if (foo()) {
   return true;
} else {
   return false;
}

can be simplified to 可以简化为

return foo();

How does this work? 这是如何运作的? Say foo() is the number 4, wouldn't this just return 4 instead of true? 说foo()是数字4,难道这不只是返回4而不是true吗? What am I misunderstanding? 我有什么误会?

Edit 编辑

Here is the code I am writing: 这是我正在编写的代码:

if (row > 0 && row < 4 && col > 0 && col < 4) {
    return false;
} else {
    return true;
}

and it can be simplified to: 可以简化为:

return !(row > 0 && row < 4 && col > 0 && col < 4);

I just don't understand how this is simplified. 我只是不明白如何简化。

(row > 0 && row < 4 && col > 0 && col < 4) is itself a boolean (true or false). (row > 0 && row < 4 && col > 0 && col < 4)本身就是一个布尔值(真或假)。

We can break it down as boolean && boolean && boolean && boolean as < and > operators return booleans. 我们可以将其分解为boolean && boolean && boolean && boolean因为<>运算符返回布尔值。 Likewise, a boolean and a boolean is a boolean, therefore the entire expression is a boolean and can be returned as such. 同样,布尔值和布尔值是布尔值,因此整个表达式都是布尔值,可以这样返回。

You can think about it as such: 您可以这样考虑:

  • For the row, you have a number, so you can say "how many" or "which number". 对于该行,您有一个数字,因此您可以说“多少”或“哪个数字”。 Same for the column. 列相同。
  • For the comparisons, you can ask: If x and y are numbers, then is x < y? 为了进行比较,您可以问:如果x和y是数字,那么x <y是吗? The answers possible are true or false . 答案可能是true还是false Hence boolean. 因此是布尔值。
  • For && , || 对于&&|| , or ! ! , you can ask yourself: "If a and b are statements (true or false), then "a and b are both true" is either true or false. Same for "or", or "a is not true". The results are true and false. ,您可以问自己:“如果a和b是语句(是或否),则“ a和b都为真”是对还是错。与“ or”相同,或者“ a不为真”。结果是真是假。

Now you can look at the if, and read it as "if foo is true, then return false. Otherwise, return true". 现在,您可以查看if,并将其读取为“如果foo为true,则返回false。否则为true”。 This clearly simplifies to "return the opposite of foo " or, "return not foo ". 这显然简化为“返回foo的反面”或“不返回foo ”。

For an if statement to compile, the condition between parenthesis, has to be a boolean. 要编译if语句,括号之间的条件必须为布尔值。

So, if you can put the method foo as the condition of an if, it's because it returns a boolean. 因此,如果您可以将foo方法作为if的条件,那是因为它返回一个布尔值。

Therefore, you could write: 因此,您可以编写:

return foo(); 

In your particular case, the expression !(row > 0 && row < 4 && col > 0 && col < 4) evaluates to true or false, Therefore you could write: 在您的特定情况下,表达式!(row > 0 && row < 4 && col > 0 && col < 4)计算结果为true或false,因此您可以编写:

return !(row > 0 && row < 4 && col > 0 && col < 4);

Your Foo() method returns a boolean value, so it will be true or false. 您的Foo()方法返回一个布尔值,因此它将为true或false。 Therefor, you do not need to add redundancy by adding an if statement to check for true or false, and then return true or false. 因此,您不需要通过添加if语句来检查true或false,然后返回true或false来添加冗余。

I have similar code like you: 我有类似你的代码:

if (other.getClassAbbr().equals(this.getClassAbbr())
        && other.getInstance().equals(this.getInstance())) {
    return true;
}

return false;

After thinking of it, I take the suggestion: 'if' statement can be simplified 考虑一下之后,我建议:“ if”语句可以简化

Then the code be changed to: 然后将代码更改为:

    return other.getClassAbbr().equals(this.getClassAbbr())
            && other.getInstance().equals(this.getInstance());

Yes, code gets clean. 是的,代码变得干净了。

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

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