将一个方法调用放在for循环中被认为是不好的做法?(Is putting a method call in a for loop considered bad practice?)
以下是否会被视为不良编码实践?
for (row = 0; row < 4; row++, printf("\n")) { for (col = 0; col <= row; col++) { printf("*"); } }Would the following be considered bad coding practice?
for (row = 0; row < 4; row++, printf("\n")) { for (col = 0; col <= row; col++) { printf("*"); } }最满意答案
是的,这被认为是不好的做法。 我会引用广泛认可的权威,而不是基于我自己的主观意见做出回复(价值有限):
MISRA-C:2004,规则13.5:
“for循环的三个语句只涉及循环控制。” / - /
“第三个表达式:循环计数器(i)的递增或递减。”
MISRA-C也完全禁止使用逗号运算符:它被认为既多余又危险。
Yes, this is considered as bad practice. Rather than making a reply (of limited value) based on my own subjective opinions, I'll cite a widely-recognized authority:
MISRA-C:2004, rule 13.5:
"The three statements of a for loop shall be concerned only with loop control." /--/
"Third expression: Increment or decrement of the loop counter (i)."
MISRA-C also bans the use of the comma operator entirely: it is considered both superfluous and dangerous.
更多推荐
发布评论