在我的第一次代码评审中(不久前),有人告诉我,在所有switch语句中包含一个default子句是一种很好的实践。我最近想起了这个建议,但不记得理由是什么了。现在我听起来很奇怪。
是否有一个合理的理由总是包含默认语句? 这是语言依赖吗?我不记得当时我在用什么语言了——也许这适用于一些语言而不是其他语言?
在我的第一次代码评审中(不久前),有人告诉我,在所有switch语句中包含一个default子句是一种很好的实践。我最近想起了这个建议,但不记得理由是什么了。现在我听起来很奇怪。
是否有一个合理的理由总是包含默认语句? 这是语言依赖吗?我不记得当时我在用什么语言了——也许这适用于一些语言而不是其他语言?
当前回答
enum所使用的开关可能不需要默认大小写。当switch包含所有值时,默认情况将永远不会执行。所以在这种情况下,这是不必要的。
其他回答
在我看来,答案是“默认”是可选的,说一个开关必须总是包含一个默认就像说每个“if-elseif”必须包含一个“其他”。 如果有一个逻辑在默认情况下完成,那么'default'语句应该在那里,但否则代码可以继续执行而不做任何事情。
如果开关值(switch(variable))不能达到默认情况,则根本不需要默认情况。即使我们保留默认情况,它也不会被执行。这是死代码。
开关情况几乎总是有一个默认情况。
使用默认值的原因
1.“捕捉”一个意外值
switch(type)
{
case 1:
//something
case 2:
//something else
default:
// unknown type! based on the language,
// there should probably be some error-handling
// here, maybe an exception
}
2. 处理“默认”操作,其中的case是针对特殊行为的。
您在菜单驱动程序和bash shell脚本中经常看到这种情况。当变量声明在switch-case之外但没有初始化时,您也可能会看到这种情况,并且每个case初始化它为不同的东西。这里的默认值也需要初始化它,以便访问变量的代码不会引发错误。
3.向阅读您的代码的人展示您已经覆盖了这种情况。
variable = (variable == "value") ? 1 : 2;
switch(variable)
{
case 1:
// something
case 2:
// something else
default:
// will NOT execute because of the line preceding the switch.
}
这是一个过于简化的例子,但关键是阅读代码的人不应该怀疑为什么变量不能是1或2以外的东西。
我能想到的唯一不使用默认值的情况是,当开关正在检查一些非常明显的东西时,其他选项都可以愉快地忽略
switch(keystroke)
{
case 'w':
// move up
case 'a':
// move left
case 's':
// move down
case 'd':
// move right
// no default really required here
}
switch语句应该总是包含一个默认子句吗? 不存在缺省情况下的开关情况,在缺省情况下,当不匹配任何其他case值时,将触发开关值开关(x)。
我不同意上面Vanwaril投票最多的回答。
任何代码都会增加复杂性。此外,还必须为此进行测试和文档编制。所以用更少的代码编程总是好的。我的观点是,我对非穷举switch语句使用default子句,而对穷举switch语句不使用default子句。为了确保我做对了,我使用了静态代码分析工具。让我们来详细了解一下:
Nonexhaustive switch statements: Those should always have a default value. As the name suggests those are statements which do not cover all possible values. This also might not be possible, e.g. a switch statement on an integer value or on a String. Here I would like to use the example of Vanwaril (It should be mentioned that I think he used this example to make a wrong suggestion. I use it here to state the opposite --> Use a default statement): switch(keystroke) { case 'w': // move up case 'a': // move left case 's': // move down case 'd': // move right default: // cover all other values of the non-exhaustive switch statement } The player could press any other key. Then we could not do anything (this can be shown in the code just by adding a comment to the default case) or it should for example print something on the screen. This case is relevant as it may happen. Exhaustive switch statements: Those switch statements cover all possible values, e.g. a switch statement on an enumeration of grade system types. When developing code the first time it is easy to cover all values. However, as we are humans there is a small chance to forget some. Additionally if you add an enum value later such that all switch statements have to be adapted to make them exhaustive again opens the path to error hell. The simple solution is a static code analysis tool. The tool should check all switch statements and check if they are exhaustive or if they have a default value. Here an example for an exhaustive switch statement. First we need an enum: public enum GradeSystemType {System1To6, SystemAToD, System0To100} Then we need a variable of this enum like GradeSystemType type = .... An exhaustive switch statement would then look like this: switch(type) { case GradeSystemType.System1To6: // do something case GradeSystemType.SystemAToD: // do something case GradeSystemType.System0To100: // do something } So if we extend the GradeSystemType by for example System1To3 the static code analysis tool should detect that there is no default clause and the switch statement is not exhaustive so we are save.
还有一件事。如果我们总是使用默认子句,那么静态代码分析工具可能无法检测穷尽性或非穷尽性switch语句,因为它总是检测到默认子句。这是非常糟糕的,因为如果我们将枚举扩展为另一个值,并且忘记将其添加到一个switch语句中,我们将不会得到通知。