无间断切换

发布于 2024-12-22 01:46:05 字数 224 浏览 5 评论 0原文

我有一些 switch 语句,如下所示。请注意,没有休息。 Findbugs 仅报告第二个 case 语句的错误。 错误是:发现 Switch 语句,其中一个案例落入下一个案例。

switch(x) {

    case 0:
        // code

    case 1:
        // code

    case 2:
        // code
}

I have some switch statement as shown below. Notice there is no break.
Findbugs is reporting error on the second case statement only.
The error is : Switch statement found where one case falls through to the next case.

switch(x) {

    case 0:
        // code

    case 1:
        // code

    case 2:
        // code
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

信愁 2024-12-29 01:46:05

Findbugs 指出,如果第一个案例中有任何代码,那么从一个案例过渡到下一个案例通常不是一个好主意(尽管有时它可以起到很好的效果)。因此,当它看到第二个 case 并且没有 break 时,它会报告错误。

例如:

switch (foo) {
    case 0:
        doSomething();
    case 1:
        doSomethingElse();
    default:
        doSomeOtherThing();
}

这是完全有效的 Java,但它可能没有达到作者的预期:如果 foo0全部三个函数 doSomethingdoSomethingElsedoSomeOtherThing 运行(按顺序)。如果 foo1,则仅运行 doSomethingElsedoSomeOtherThing。如果 foo 是任何其他值,则仅运行 doSomeOtherThing

相反:

switch (foo) {
    case 0:
        doSomething();
        break;
    case 1:
        doSomethingElse();
        break;
    default:
        doSomeOtherThing();
        break;
}

这里,只有一个函数会运行,具体取决于 foo 的值。

由于忘记 break 是一种常见的编码错误,Findbugs 等工具会为您标记它。

有一个常见的用例,您连续有多个 case 语句,并且没有中间代码:

switch (foo) {
    case 0:
    case 1:
        doSomething();
        break;
    case 2:
        doSomethingElse();
        break;
    default:
        doSomeOtherThing();
        break;
}

在那里,我们想要调用 doSomething if <代码>foo是01。大多数工具不会将此标记为可能的编码错误,因为在 case 1 之前的 case 0 中没有代码,而且这是一种相当常见的模式。

Findbugs is flagging up that falling through from one case to the next is generally not a good idea if there's any code in the first one (although sometimes it can be used to good effect). So when it sees the second case and no break, it reports the error.

So for instance:

switch (foo) {
    case 0:
        doSomething();
    case 1:
        doSomethingElse();
    default:
        doSomeOtherThing();
}

This is perfectly valid Java, but it probably doesn't do what the author intended: If foo is 0, all three of the functions doSomething, doSomethingElse, and doSomeOtherThing run (in that order). If foo is 1, only doSomethingElse and doSomeOtherThing run. If foo is any other value, only doSomeOtherThing runs.

In contrast:

switch (foo) {
    case 0:
        doSomething();
        break;
    case 1:
        doSomethingElse();
        break;
    default:
        doSomeOtherThing();
        break;
}

Here, only one of the functions will run, depending on the value of foo.

Since it's a common coding error to forget the break, tools like Findbugs flag it up for you.

There's a common use-case where you have multiple case statements in a row with no intervening code:

switch (foo) {
    case 0:
    case 1:
        doSomething();
        break;
    case 2:
        doSomethingElse();
        break;
    default:
        doSomeOtherThing();
        break;
}

There, we want to call doSomething if foo is 0 or 1. Most tools won't flag this up as a possible coding error, because there's no code in the case 0 prior to the case 1 and this is a fairly common pattern.

那片花海 2024-12-29 01:46:05

我把这些写成评论,但后来就看不到了。我正在把它们变成一个答案。这实际上是 TJCrowder 的答案的扩展。

您可以在此处找到导致 Findbugs 报告错误的相关规则。

您可以通过创建包含以下内容(例如 filter.xml)的 xml 文件并使用 -exclude filter.xml 选项运行该工具来防止 Findbugs 报告此类错误。请参阅Findbugs 上的过滤器

<FindBugsFilter>
  <Match>
    <Bug category="PERFORMANCE" />
  </Match>
</FindBugsFilter>

I wrote these as comments but then it's not visible. I'm turning them into an answer. This is actually an extension to T.J.Crowder's answer.

You can find the related rule that causes Findbugs to report an error here.

You can prevent Findbugs to report this kind of errors by creating an xml file with the following content, say filter.xml and running the tool with -exclude filter.xml option. See filters on Findbugs.

<FindBugsFilter>
  <Match>
    <Bug category="PERFORMANCE" />
  </Match>
</FindBugsFilter>
从来不烧饼 2024-12-29 01:46:05

Switch 失败属于 Findbugs 的“狡猾代码”类别。我认为它只是标记 switch 语句中第一次出现的失败,以减少错误消息的数量。

Switch fall-throughs fall under the Findbugs category of "dodgy code". I think it only flags the first occurrence of a fall-through in a switch statement to cut down on the number of error messages.

幸福%小乖 2024-12-29 01:46:05

如果没有中断,它们将相互陷入,因此如果 x == 0 您将遍历每个 case 语句块中的所有代码。 Findbugs 可能对错误有误,也可能是没有中断的错误条件,即 case 0 中的某些内容导致 case 1 中的某些内容中断。

如果没有确切的代码和错误,我无法真正提供进一步的帮助。缺乏休息时间是故意的吗?

Without the break they will fall though into each other so if x == 0 you'll go through all the code in every case statement block. Findbugs could either be wrong about the error, or it could be an error condition without the break, i.e. something in case 0 causes something in case 1 to break.

Without the exact code and error I can't really help further. Is the lack of breaks deliberate?

心病无药医 2024-12-29 01:46:05

通常错误分析工具不喜欢代码中的失败,因为在大多数情况下,用户只是忘记编写中断。

我不知道是否有办法专门禁用 FindBugs 的警告,但 Checkstyle 工具可以识别特殊注释,例如 /* Fallthrough */ 假设用户确实希望执行以下代码。添加这种注释也可以提高可读性。
http://checkstyle.sourceforge.net/config_coding.html#FallThrough

Java 代码约定还提到了fallthrough注释的使用。
http://www.oracle.com/technetwork/java/ javase/documentation/codeconventions-142311.html

Usually bug analysis tools don't like fallthrough in code because in most cases, the user has just forgotten to write the break.

I don't know if there is a way to specifically disable the warning with FindBugs but Checkstyle tool recognize special comments such as /* fallthrough */ to assume that the user really wants the following code to be executed. Putting this kind of comment also improves readability.
http://checkstyle.sourceforge.net/config_coding.html#FallThrough

Java code convention also mentions the use of fallthrough comment.
http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html

‖放下 2024-12-29 01:46:05

如果您的案例没有中断,一旦触发案例,开关将遍历所有案例,直到找到案例的中断或结束。如果您有默认情况,如果您在这种情况下 foo 的开关值与任何其他情况不匹配,它将触发。

If your cases don't have break, once a case is trigger switch will go through all cases until it finds a break or end of the cases. If you have default case it will trigger if your switch value in this case foo does not match with any other cases.

扎心 2024-12-29 01:46:05

是的,也可以使用不带中断的 switch 语句。仅有语法上的变化。例如:

String fruit="Apple";
switch (fruit)
{
    case "Orange": System.out.println ("Orange");
    
    case "Apple":  System.out.println ("Apple");
    
    default:  System.out.println ("Invalid");
    
}

像这样使用 switch 语句称为 enchaned switch

Yes it is possible to use switch statement without break also. Only changes will be in syntax. For instance:

String fruit="Apple";
switch (fruit)
{
    case "Orange": System.out.println ("Orange");
    
    case "Apple":  System.out.println ("Apple");
    
    default:  System.out.println ("Invalid");
    
}

Using switch statement like this is called as enchaned switch

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文