仅在大括号范围内缩进代码是否常见?
对于生成的代码,我可以选择在大括号处缩进或不缩进,这些大括号仅用于在范围内容纳变量。 目前它在这个级别没有缩进,我想知道是否要通过缩进来暗示嵌套结构? 常见的做法是什么?
/* loop through the total number of letter a rules */
for (a = 0; a < (number_a_rules - 1); a++)
{
/* loop through secondary position rules */
{
int a2end = arulestableend[2];
for (int a2 = arulestablestart[2]; a2 < a2end; a2++)
{
/* stuff */
}
}
} /* end for a 0 to numberarules -1 */
对比
/* loop through the total number of letter a rules */
for (a = 0; a < (number_a_rules - 1); a++)
{
/* loop through secondary position rules */
{
int a2end = arulestableend[2];
for (int a2 = arulestablestart[2]; a2 < a2end; a2++)
{
/* stuff */
}
}
} /* end for a 0 to numberarules -1 */
澄清: 使用调试器,额外的缩进意味着难以阅读的代码中的另一个级别的循环......
For generated code I have an option to either indent or or not indent at braces which are only used to house variables at scope. Currently it does not indent at this level and I am wondering if I am going to imply a nested structure by indenting it? What is the common practice?
/* loop through the total number of letter a rules */
for (a = 0; a < (number_a_rules - 1); a++)
{
/* loop through secondary position rules */
{
int a2end = arulestableend[2];
for (int a2 = arulestablestart[2]; a2 < a2end; a2++)
{
/* stuff */
}
}
} /* end for a 0 to numberarules -1 */
Versus
/* loop through the total number of letter a rules */
for (a = 0; a < (number_a_rules - 1); a++)
{
/* loop through secondary position rules */
{
int a2end = arulestableend[2];
for (int a2 = arulestablestart[2]; a2 < a2end; a2++)
{
/* stuff */
}
}
} /* end for a 0 to numberarules -1 */
Clarification:
Using a debugger the extra indent implies another level of looping in hard to read code...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为其真正的客户:编译器吐出自动生成的代码。 一些换行符通常会对编译器有所帮助,编译器仍然针对大量短行进行了优化,但除此之外,谁在乎呢? 当您想要手动检查其正确性时,有很多漂亮的打印机可以让其看起来很漂亮。
Spit out autogenerated code for its true customer: the compiler. A few dollops of newlines will usually help compilers, which are still optimized for lots of short lines, but other than that, who cares? There are plenty of pretty printers out there that can make it look nice when you want to check it out by hand for correctness.
在我看来,如果没有缩进,它会更具误导性。 我看着未缩进的版本并想“这里出了什么问题”?
真的有必要使用多余的牙套吗?
更新
我想回答 ojblass 的评论,它会比我认为的评论占用更多的空间。
C 语言不像 C++ 语言那样自由; 您要做的就是将任何新声明放在任何可执行代码之前。 因此,
您可以在这个片段中编写它
,并且它会完美地工作。 另一方面,for 循环中的声明也不是直接的 C。
现在,可能的情况是,示例中不会显示的是,提问者是否使用大括号来限制范围,以便他拥有更简单的名称空间管理; 也就是说,所以你可以有
(注意:抱歉,OJ,没有意识到你是提问者。我的眼睛受伤了,请对所读的语法进行必要的更正。)
To my eyes, it's more misleading without indenting. I look at the unindented version and think "what's wrong here"?
Is it really necessary to have the redundant braces?
Update
I want to answer ojblass's comment and it'll take more space than a comment I think.
You're not as free in C as in C++; what you have to do is put any new declarations in before any executable code. So, in this fragment
you could write it
and it would work perfectly well. On the other hand, the declaration in the for loop isn't straight C either.
Now, what is possible, and wouldn't show in the example, is if questioner is using the braces to limit scopes so he has a simpler namespace management; that is, so you could have
(NB: Sorry, OJ, didn't realize you were the questioner. My eyes hurt, please take the necessary corrections to grammar as read.)
当使用大括号作为变量作用域时,我肯定会缩进代码。
我用于生成 C(和 C++)代码的一项技术是在生成代码时完全不用担心格式。 只需将代码扔在那里即可,无需考虑间距、缩进、行结尾或任何其他内容。 生成后,通过 GNU
indent
运行即可生成代码可读用于调试目的。 这比尝试自己创建所有格式要容易得多。I would definitely always indent code when using braces for variable scoping.
One technique I've used for generated C (and C++) code is to not worry at all about the formatting when generating the code. Just throw the code out there without regard for spacing, indenting, line endings, or anything. After generation, run it through GNU
indent
to make the code readable for debugging purposes. This is much easier than trying to create all the formatting yourself.我想不出不缩进的好理由。 大括号表示一个有意义的块,无论它是否用于循环或其他条件语句。 最后,这取决于您认为更具可读性,因此请跟随您的直觉,但要保持一致。
I can't think of a good reason not to indent. The braces indicate a block which has significance whether or not it is for a loop or other conditional statement. In the end, it comes down to what you think is more readable so go with your gut but be consistent.
如果您想让人们阅读它,请缩进。 如果没有缩进,大括号就不起作用。 我不得不盯着上面的例子一段时间,以说服自己它没有被破坏。
缩进并不意味着循环; 还有许多其他结构使用它。 如果大括号是在没有任何其他内容的情况下嵌套的,我可能会感到困惑:
但是在第二个示例中使用紧随其后的大括号的注释使得很快就可以清楚地看出发生了什么。
您是否担心使用太多缩进并达到“80 个字符限制”? 如果是这样,请增加限制和/或减少缩进大小(8 个空格确实有点大)。
(如果你不想让它被人类阅读,那么谁在乎呢?)
If you intend people to read it, indent. Braces don't work without indentation. I had to stare at the top example for a while to convince myself it wasn't broken.
Indentation does not imply a loop; there are many other constructs that use it. I might have been confused if the braces had been nested without any other content:
But the use of a comment immediately followed by a brace in the second example made it quickly obvious what was going on.
Are you worried about using too many indents and hitting the ‘80 character limit’? If so, increase the limit and/or decrease the indent size (8 spaces is a bit big really).
(If you don't intend it to be read by humans, then who cares?)
在
for
语句的第一部分使用逗号运算符怎么样?How about using the comma operator in the first part of the
for
statement?