行长度限制对我来说是一个棘手的问题,具体取决于我们代码的哪个区域,我将严格使用 80。但实际上,现代 IDE 可以处理更长的行,而不会遇到太多麻烦 但无论怎样,打印都很难实现自动化,太多的极端情况会带来麻烦,需要进行调整。
As far as I know the general wisdom is pick a style and go with it. It doesn't really matter too much what style you pick as long as you pick one. See python for a good example of the language enforcing a particular style and, while it irks some people, it works pretty well.
I guess the one's that people tend to care about are
indenting, spaces vs tabs and number of spaces (or number of spaces the tabs represent)
Where the brace goes
spaces around operators for(int i=0... vs for(int i = 0...
spaces after if before the (
Things I wouldn't be as interested in:
Sorting the members of a class
Enforcing variable naming schemes automagically (they should be in place but there are too many corner cases that cause trouble)
Line length limits are a sore issue for me. Depending on what area of our code I will strictly use 80. But really, modern IDEs can deal with longer lines without too much trouble. But then printing becomes a pain. No matter what this is very hard to automate well. Again too many corner cases that cause trouble and would need to be tweaked.
我希望我的代码尽可能清晰。 在我梦想的代码查看器中,我可以切换查看 using 声明 - 也许我什至不需要它们:我可以切换完整命名空间查看,可以隐藏局部变量声明以仅关注控制流, ...
One thing I miss in code formatters is something that gives me more hints about the structure of the code. I often find myself indenting consecutive lines of almost-equal statements to highlight their correspondances and differences, much like this:
This is totally unmaintainable: when another, longer, line is added, I lose time indenting all previous lines. And yes, I did read Code Complete and partially agree with their statement about this :)
If you can add this (quite human, and subjective) aesthetic heuristic to a viewer, I would be pleased to hear from it.
I want my code as clear as possible. In my dream code viewer, I can toggle viewing using declarations - maybe I don't even need them: I can toggle full-namespace viewing, can hide local variable declarations to just focus on the control flow, ...
我建议看一下 eclipses 代码格式化程序。 它可能适用于 Java,但它仍然有很多适用于 C++ 的选项。 否则,你目前所拥有的对我来说看起来不错,但可能有点稀疏......我会详细说明一下,但我现在没有时间,我相信其他人也会回答......另外
,Eclipse 确实支持 C++,但我以前从未看过它,但我认为它也有一个代码格式化程序。
I would suggest taking a look at eclipses code formatter. It may be for Java, but it still has a lot of options that would apply to C++. Otherwise, what you currently have looks good to me, but maybe a bit sparse... I would elaborate a bit more, but I don't have the time right now, and I'm sure someone else will answer as well...
Also, Eclipse does have support for C++, but I've never looked at it before, but I'd assume it would have a code formatter as well.
发布评论
评论(7)
看看 Vera++
Take a look at Vera++
嗯,我非常喜欢它。 它与 astyle 不同吗? Astyle 对于这种事情非常有效。
Well, I like it very much. Is it different to astyle though? Astyle works pretty well for this kind of thing.
据我所知,一般的智慧是选择一种风格并与之相配。 其实选择什么风格并不重要,只要你选择一种即可。 请参阅 python,这是该语言强制执行特定风格的一个很好的示例,虽然它让某些人感到厌烦,但它的效果非常好。
我想人们往往关心的是
for(int i=0... vs
for(int i = 0...
我不会感兴趣的事情:
As far as I know the general wisdom is pick a style and go with it. It doesn't really matter too much what style you pick as long as you pick one. See python for a good example of the language enforcing a particular style and, while it irks some people, it works pretty well.
I guess the one's that people tend to care about are
for(int i=0...
vsfor(int i = 0...
Things I wouldn't be as interested in:
样式格式化程序怎么样? ,格式根据:gnu、bsd、k&r 等
例如 http://en.wikipedia.org/wiki/Indent_style
How about a style formatter. e.g. format according to: gnu, bsd, k&r etc
e.g. http://en.wikipedia.org/wiki/Indent_style
我在代码格式化程序中错过的一件事是它为我提供了有关代码结构的更多提示。 我经常发现自己缩进几乎相等的语句的连续行来突出它们的对应关系和差异,就像这样:
这
完全是无法维护的:当添加另一条更长的行时,我会浪费时间缩进所有先前的行。 是的,我确实阅读了《Code Complete》并部分同意他们对此的声明:)
如果您可以向观众添加这种(非常人性化且主观的)审美启发式,我将很高兴听到它。
我希望我的代码尽可能清晰。 在我梦想的代码查看器中,我可以切换查看
using
声明 - 也许我什至不需要它们:我可以切换完整命名空间查看,可以隐藏局部变量声明以仅关注控制流, ...One thing I miss in code formatters is something that gives me more hints about the structure of the code. I often find myself indenting consecutive lines of almost-equal statements to highlight their correspondances and differences, much like this:
into that
This is totally unmaintainable: when another, longer, line is added, I lose time indenting all previous lines. And yes, I did read Code Complete and partially agree with their statement about this :)
If you can add this (quite human, and subjective) aesthetic heuristic to a viewer, I would be pleased to hear from it.
I want my code as clear as possible. In my dream code viewer, I can toggle viewing
using
declarations - maybe I don't even need them: I can toggle full-namespace viewing, can hide local variable declarations to just focus on the control flow, ...GNU Indent 是一个为 C 或 C++ 设计的自动缩进程序。 如果您愿意,可以查看选项或来源。
GNU Indent is an auto-indent program designed for C or C++. You can take a look at the options or the source if you'd like.
我建议看一下 eclipses 代码格式化程序。 它可能适用于 Java,但它仍然有很多适用于 C++ 的选项。 否则,你目前所拥有的对我来说看起来不错,但可能有点稀疏......我会详细说明一下,但我现在没有时间,我相信其他人也会回答......另外
,Eclipse 确实支持 C++,但我以前从未看过它,但我认为它也有一个代码格式化程序。
I would suggest taking a look at eclipses code formatter. It may be for Java, but it still has a lot of options that would apply to C++. Otherwise, what you currently have looks good to me, but maybe a bit sparse... I would elaborate a bit more, but I don't have the time right now, and I'm sure someone else will answer as well...
Also, Eclipse does have support for C++, but I've never looked at it before, but I'd assume it would have a code formatter as well.