将一个方法调用放在for循环中被认为是不好的做法?(Is putting a method call in a for loop considered bad practice?)

编程入门 行业动态 更新时间:2024-10-24 12:23:36
将一个方法调用放在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.

更多推荐

本文发布于:2023-08-02 14:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:放在   做法   不好   方法   putting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!