如何聪明地处理标志以避免警告 “This expression always evaluates to true”。

huangapple go评论124阅读模式
英文:

How to handle flags smartly to avoid warning "This expression always evaluates to true"

问题

我无法理解为什么下面的方法在Java Eclipse中会给我一个警告,我同意它看起来有点愚蠢,但无法想出一个聪明处理它的方法。

具体而言,我得到了以下建议:

"删除这个始终评估为真的表达式"

指的是 flag 变量,而在 else 块内部,我得到了

"删除对局部变量 flag 的这个无用的赋值"

boolean flag = true;
int counter = 1;
while(flag){
    // 做一些操作
    if(condition){
        counter ++;
    }else{
        flag = false;
        break;
    }
}
英文:

I can´t understand why the following method is giving me a warning in java eclipse, I agree it looks kind of silly but cannot figure a way of handling it smartly.

Especifically I am getting the following suggestion:

> "Remove this expression which always evaluates to true"

referring to the flag variable, and inside the else block I am getting

> "Remove this useless assignment to local variable flag"

boolean flag = true;
int counter = 1;
while(flag){
    // do something
    if(condition){
        counter ++;
    }else{
        flag = false;
        break;
    }
}

答案1

得分: 2

按照以下方式重写你的循环:

// 初始设置条件的一些代码在这里。
while (condition) {
    counter++;
    // 更新条件的一些代码在这里。
}

这应该满足Java的要求,并且有一个很好的副作用,可以简化你的代码。这是Java中while循环的标准形式,所以除非你有很充分的理由,不应该以其他方式编写它。

英文:

Rewrite your loop like this:

// some code that sets condition initially goes here.
while(condition)
{
    counter++;
    // some code that updates condition goes here.
}

This should satisfy Java, and it has the nice side-effect of simplifying your code. This is the canonical form of a while loop in Java, so unless you have a very good reason, you should not write it otherwise.

答案2

得分: 1

问题在于如果 flag 改变并变为 false,它就变得有用;如果它从不改变或从未被使用,它就变得无用。由于你还使用了 break 指令,一旦循环在变为 false 后停止,就不会再使用 flag,因此你可以选择使用 flagbreak

int counter = 1;
while (true) {
    // 做一些事情
    if (condition) {
        counter++;
    } else {
        break;
    }
}

或者

boolean flag = true;
int counter = 1;
while (flag) {
    // 做一些事情
    if (condition) {
        counter++;
    } else {
        flag = false;
    }
}

改进:

另外,我建议设置一个较高的上限,以避免无限循环,即使你百分之百确定你的 condition 在某一时刻会变为 false,这是一种无代价的安全措施。此外,根据你提供的信息,似乎你可以直接将 condition 用作 while 循环的条件,并使用 do/while 变体。

int counter = 1, limit = 1000000;
do {
    // 做一些事情
    counter++;
} while (condition && counter < limit);
英文:

The thing is that flag is useful if it changes and becomes false, if it never changes or is never used, it becomes useless. As you also use the break instruction, the loop will stop without using flag after being false, so yo may choose flag or break

int counter = 1;
while(true){
    // do something
    if(condition){
        counter ++;
    }else{
        break;
    }
}

or

boolean flag = true;
int counter = 1;
while(flag){
    // do something
    if(condition){
        counter ++;
    }else{
        flag = false;
    }
}

Improvement

Also I'd suggest a high limit, to avoid an infinite loop even if you're 100% sure your condition will be false at a moment, that's a safety that costs nothing. Also, from the info you give it seems you could use condition directly as the while loop condition, using the do/while variant

int counter = 1, limit = 1000000;
do{
    // do something
    counter ++;
}while(condition &amp;&amp; counter &lt; limit);

答案3

得分: 1

虽然您没有说明condition是什么,但在我看来,您的while循环可以简单地重写为while (condition),而无需使用标志。

警告出现的原因是,正如其所述,flag没有被以任何有意义的方式使用。通常这意味着您根本不需要它,正如其他人所指出的那样。如果您出于其他未提及的原因确实需要它,那么您需要重新考虑您如何构造这段代码。

英文:

While you haven't stated what condition is, it appears to me that your while loop could simply be rewritten as while (condition), without using a flag at all.

The warning appears because, as it states, flag is not being used in any meaningful way. This is usually a sign that you don't need it at all, as others have pointed out -- and if you do for some other reason you haven't given, then you need to reconsider how you're structuring this code.

huangapple
  • 本文由 发表于 2020年10月28日 05:43:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64563313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定