In a C switch-case
flow control, it's required to put curly braces { } after a case
if variables are being defined in that block.
在C开关盒流控制中,如果在该块中定义了变量,则需要将大括号{}处理。
Is it bad practice to put curly braces after every case
, regardless of variable declaration?
不管变量声明是什么,在每一种情况之后都加上花括号是不是不好的做法?
For example:
例如:
switch(i) {
case 1: {
int j = 4;
...code...
} break;
case 2: { //No variable being declared! Brace OK?
...code...
} break;
}
5 个解决方案
#1
33
It's certainly not invalid to use braces in every case block, and it's not necessarily bad style either. If you have some case blocks with braces due to variable declarations, adding braces to the others can make the coding style more consistent.
在每个case块中使用大括号当然不是无效的,它也不一定是不好的样式。如果由于变量声明,您有一些带有大括号的大小写块,那么向其他大括号添加大括号可以使代码风格更加一致。
That being said, it's probably not a good idea to declare variables inside case blocks in straight C. While that might be allowed by your compiler, there's probably a cleaner solution. Mutually-exclusive case blocks may be able to share several common temporary variables, or you may find that your case blocks would work better as helper functions.
也就是说,用c直接在大小写块中声明变量可能不是一个好主意。互斥的case块可以共享几个通用的临时变量,或者您可能会发现您的case块作为helper函数工作得更好。
#2
8
Braces may be used in every case statement without any speed penalty, due to the way compilers optimize code. So it's just the style and the preference of the coder.
由于编译器优化代码的方式不同,大括号可以在每个case语句中使用,而不会造成任何速度损失。这就是编码器的风格和偏好。
-
The most preferred usage is not using braces, though the usage of them in every case during an active development may be found easier to make some additions on the code every now and then.
最理想的用法是不使用大括号,尽管在活动开发过程中使用大括号可能更容易在代码中时不时地添加一些内容。
-
It's just the easthetics; because a 'case' statement doesn't need only a single command, but will walk through the code as it works as a label. So blocks are not needed, and are not invalid.
它只是审美;因为“case”语句不需要一个命令,而是作为标签遍历代码。所以块是不需要的,而且不是无效的。
-
In 'case's with variables; braces are used just-in-case, to create contexts for variables, and it makes big sense to use them. Some compilers on different platforms show different behaviours if they are not included.
在的情况下的变量;大括号用于为变量创建上下文,并且使用它们很有意义。不同平台上的一些编译器如果不包括它们,则显示不同的行为。
#3
5
Generally it is bad practice jump over the initialization of a variable, be it with goto
or switch
. This is what happens when you don't have the the blocks per case
.
一般来说,跳过变量的初始化是不好的做法,无论是goto还是switch。当你没有每个情况下的块时,就会发生这种情况。
There is even a case in C99 where jumping over the initialization is illegal, namely variable length arrays. They must be "constructed" similarly as non-PODs in C++, their initialization is necessary for the access of the variable later. So in this case you must use the block statement.
在C99中,甚至有这样的情况:跳过初始化是非法的,即可变长度数组。它们必须像c++中的非pod一样“构造”,它们的初始化对于以后访问变量是必要的。在这种情况下,你必须使用block语句。
#4
0
I consider it bad style to use braces in each case
. Cases are labels in C, akin to goto
labels. And in the current C language, you're free to declare variables in each case
(or anywhere you like) without introducing new blocks, though some people (myself included) also consider that bad style.
我认为在每种情况下使用大括号是不好的风格。箱子是C语言的标签,类似于goto标签。在当前的C语言中,您可以在不引入新块的情况下(或您喜欢的任何地方)声明每个情况下的变量,尽管有些人(包括我自己)也认为这种方式不好。
#5
0
Just to add a minor point many editors & IDEs allow blocks to be collapsed and/or auto indented and several allow you to jump to the matching brace - I personally don't know of any that allow you to jump from a break to the matching case statement.
仅仅添加一点,许多编辑器和ide允许块被折叠和/或自动缩进,还有一些允许您跳转到匹配的括号——我个人不知道有哪个允许您从中断跳转到匹配的case语句。
When debugging, or re-factoring, other peoples, (or even your own after a few months), code that contains complex case statements the ability to both collapse sections of the code and to jump to matching cases is invaluable, especially if the code contains indentation variations.
当调试或重构其他人员(甚至在几个月后您自己的人员)时,包含复杂案例语句的代码,崩溃代码段和跳转到匹配案例的能力是无价的,特别是当代码包含缩进变体时。
That said it is almost always good advice to avoid complex case statements like the plague.
也就是说,避免像鼠疫这样复杂的病例报告几乎总是好的建议。
#1
33
It's certainly not invalid to use braces in every case block, and it's not necessarily bad style either. If you have some case blocks with braces due to variable declarations, adding braces to the others can make the coding style more consistent.
在每个case块中使用大括号当然不是无效的,它也不一定是不好的样式。如果由于变量声明,您有一些带有大括号的大小写块,那么向其他大括号添加大括号可以使代码风格更加一致。
That being said, it's probably not a good idea to declare variables inside case blocks in straight C. While that might be allowed by your compiler, there's probably a cleaner solution. Mutually-exclusive case blocks may be able to share several common temporary variables, or you may find that your case blocks would work better as helper functions.
也就是说,用c直接在大小写块中声明变量可能不是一个好主意。互斥的case块可以共享几个通用的临时变量,或者您可能会发现您的case块作为helper函数工作得更好。
#2
8
Braces may be used in every case statement without any speed penalty, due to the way compilers optimize code. So it's just the style and the preference of the coder.
由于编译器优化代码的方式不同,大括号可以在每个case语句中使用,而不会造成任何速度损失。这就是编码器的风格和偏好。
-
The most preferred usage is not using braces, though the usage of them in every case during an active development may be found easier to make some additions on the code every now and then.
最理想的用法是不使用大括号,尽管在活动开发过程中使用大括号可能更容易在代码中时不时地添加一些内容。
-
It's just the easthetics; because a 'case' statement doesn't need only a single command, but will walk through the code as it works as a label. So blocks are not needed, and are not invalid.
它只是审美;因为“case”语句不需要一个命令,而是作为标签遍历代码。所以块是不需要的,而且不是无效的。
-
In 'case's with variables; braces are used just-in-case, to create contexts for variables, and it makes big sense to use them. Some compilers on different platforms show different behaviours if they are not included.
在的情况下的变量;大括号用于为变量创建上下文,并且使用它们很有意义。不同平台上的一些编译器如果不包括它们,则显示不同的行为。
#3
5
Generally it is bad practice jump over the initialization of a variable, be it with goto
or switch
. This is what happens when you don't have the the blocks per case
.
一般来说,跳过变量的初始化是不好的做法,无论是goto还是switch。当你没有每个情况下的块时,就会发生这种情况。
There is even a case in C99 where jumping over the initialization is illegal, namely variable length arrays. They must be "constructed" similarly as non-PODs in C++, their initialization is necessary for the access of the variable later. So in this case you must use the block statement.
在C99中,甚至有这样的情况:跳过初始化是非法的,即可变长度数组。它们必须像c++中的非pod一样“构造”,它们的初始化对于以后访问变量是必要的。在这种情况下,你必须使用block语句。
#4
0
I consider it bad style to use braces in each case
. Cases are labels in C, akin to goto
labels. And in the current C language, you're free to declare variables in each case
(or anywhere you like) without introducing new blocks, though some people (myself included) also consider that bad style.
我认为在每种情况下使用大括号是不好的风格。箱子是C语言的标签,类似于goto标签。在当前的C语言中,您可以在不引入新块的情况下(或您喜欢的任何地方)声明每个情况下的变量,尽管有些人(包括我自己)也认为这种方式不好。
#5
0
Just to add a minor point many editors & IDEs allow blocks to be collapsed and/or auto indented and several allow you to jump to the matching brace - I personally don't know of any that allow you to jump from a break to the matching case statement.
仅仅添加一点,许多编辑器和ide允许块被折叠和/或自动缩进,还有一些允许您跳转到匹配的括号——我个人不知道有哪个允许您从中断跳转到匹配的case语句。
When debugging, or re-factoring, other peoples, (or even your own after a few months), code that contains complex case statements the ability to both collapse sections of the code and to jump to matching cases is invaluable, especially if the code contains indentation variations.
当调试或重构其他人员(甚至在几个月后您自己的人员)时,包含复杂案例语句的代码,崩溃代码段和跳转到匹配案例的能力是无价的,特别是当代码包含缩进变体时。
That said it is almost always good advice to avoid complex case statements like the plague.
也就是说,避免像鼠疫这样复杂的病例报告几乎总是好的建议。