Switch 和增强型 Switch 之间除了可读性之外是否有任何区别?

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

Any difference beside readability between Switch and enhanced switch?

问题

Since we upgraded from Java 8 to Java 17, IntelliJ warns about old switch-case statements:

Switch statement can be replaced with enhanced 'switch'

Is there any difference besides readability?

Example: java 8 switch:

switch (foo) {
    case "bla": return 1;
    case "bla_bla": return 2;
}

after accepting Switch statement can be replaced with enhanced 'switch' it looks like that:

switch (foo) {
    case "bla" -> { return 1; }
    case "bla_bla" -> { return 2; }
}
英文:

Since we upgraded from Java 8 to Java 17, IntelliJ warns about old switch-case statements:
> Switch statement can be replaced with enhanced 'switch'

Is there any difference besides readability?

Example: java 8 switch:

        switch (foo) {
            case "bla": return 1;
            case "bla_bla": return 2;
        }

after accepting Switch statement can be replaced with enhanced 'switch' it looks like that:

        switch (foo) {
            case "bla" -> { return 1; }
            case "bla_bla" -> { return 2; }
        }

答案1

得分: 2

IntelliJ博文 讨论了增强开关的三个好处:

  1. 代码简洁。
  2. 可以返回一个值。
  3. 无需使用 break。在没有 break 语句的情况下,控制不会穿越开关标签,有助于避免逻辑错误。

在你的特定示例中,你已经从开关语句返回,增强开关语句可能不会提供太多好处。增强开关在旧开关使用值赋值和 break 时改善了代码。例如:

int height = 0; 
switch (size) {
    case S:
        height = 18;
        // 缺少 break(错误)
    case M:
        height = 20;
        break;
    case L:
        height = 25;
        break;
}

增强为:

int height = 0; 
switch (size) {
    case S -> height = 18;
    case M -> height = 20;
    case L -> height = 25;
}

然而,如果你不希望再收到此警告,可以在IntelliJ IDEA中将其关闭。要这样做,打开首选项,转到编辑器 - 检查(我使用的是2023.1版本)。搜索(部分)警告(Switch statement can be replaced with enhanced 'switch')并取消选中它。

英文:

IntelliJ blogpost talks about three benefits of the enhanced switch:

  1. Concise code.
  2. Can return a value.
  3. No need to use break. In absence of a break statement, the control doesn’t fall through the switch labels which helps avoid logical errors.

In your particular example, you are already returning from the switch statement and the enhanced switch statement might not offer much. The enhanced switch improves the code when the old switch uses value assignments and breaks. For example:

int height = 0; 
    switch (size) {
        case S:
            height = 18;
            // missing break (bug)
        case M:
            height = 20;
            break;
        case L:
            height = 25;
            break;
    }

which enhanced to:

int height = 0; 
    switch (size) {
        case S -> height = 18;
        case M -> height = 20;
        case L -> height = 25;
    }

However, if your wish is to no longer get this warning, it is possible to turn it off in IntelliJ IDEA. To do so, open Preferences, and go to Editor -> Inspections (I'm using version 2023.1). Search for (part of) the warning (Switch statement can be replaced with enhanced 'switch') and de-select it.

huangapple
  • 本文由 发表于 2023年4月17日 20:09:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035021.html
匿名

发表评论

匿名网友

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

确定