括号的位置会影响可读性吗?
可能的重复:
if 语句的格式
是否有最佳的标识编码风格(同一行,下一行)?
编写 stackoverflow 样式“问题”/“标签”的最佳方法' 翻转按钮
public void Method {
}
或
public void Method
{
}
除了个人喜好之外,一种样式相对于另一种样式是否有任何优点? 我曾经发誓采用第二种方法,但现在在工作和个人项目中使用第一种方法。
我所说的可读性是指想象这些方法中的代码 - if/else 等......
Possible Duplicates:
Formatting of if Statements
Is there a best coding style for identations (same line, next line)?
Best way to code stackoverflow style 'questions' / 'tags' rollover buttons
public void Method {
}
or
public void Method
{
}
Besides personal preference is there any benefit of one style over another? I used to swear by the second method though now use the first style for work and personal projects.
By readability I mean imagine code in those methods - if/else etc...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在过去,我们曾经使用第一种样式(K & R 样式),因为屏幕较小,代码通常打印在这种称为纸张的东西上。
如今我们有大屏幕,第二种方法(ANSI 风格)可以更轻松地查看括号是否匹配。
请参阅此处和此处 了解更多信息。
In the old days we used to use the first style (K & R style) because screens were smaller and code was often printed onto this stuff called paper.
These days we have big screen and the second method (ANSI style) makes it easier to see if your brackets match up.
See HERE and HERE for more information.
第一个是行数较少(也许这就是开发 Java 书籍倾向于使用该语法的原因)
第二个是,恕我直言,更容易阅读,因为您总是有两个对齐的括号。
不管怎样,这两种方法都被广泛使用,这取决于你的个人喜好。
First one is smaller in terms of number of lines (maybe that is why development -Java- books tend to use that syntax)
Second one is, IMHO easier to read as you always have two aligned brackets.
Anyway both of them are widely used, it's a matter of your personal preferences.
在这个高度情绪化的主题中,我使用 if 语句作为推理的依据。
只问 else 语句是什么样的? 上述内容的逻辑扩展是:-
可读吗? 我不这么认为,而且它也很丑陋。
行数越多,可读性就越差。 因此我会选择你的后一个选择。
I use the if statement as something to reason on in this highly emotive subject.
by just asking what does the else statement look like? The logical extension of the above is:-
Is that readable? I don't think so and its just plain ugly too.
More lines is != less readable. Hence I'd go with your latter option.
就我个人而言,我发现第二个更具可读性(对齐的卷发)。
对于团队来说,采用默认值总是最容易的,而且由于 Visual Studio 和我都同意这一点,这就是我的论点。 ;-)
Personally I find the second one more readable (aligned curlys).
Its always easiest for a team to go with the defaults, and since Visual Studio and I agree on this, thats my argument. ;-)
使用第一个选项,您的代码行数将大大减少。 :)
Your lines of code count will be considerably less with the first option. :)
Google C++ 样式指南建议
WebKit 编码风格指南 建议
不过,他们建议将其他所有内容都放在同一行上。
GNU 编码标准 建议
如您所见,每个人都有自己的意见。 就我个人而言,我更喜欢 Perl 式的大括号 on-same-line- except-for-
else
,但只要编写代码的每个人都能合作,这真的不重要。Google C++ Style Guide suggests
WebKit Coding Style Guidelines suggests
They suggest braces-on-same-line for everything else, though.
GNU Coding Standards suggests
As you can see, everybody has their own opinions. Personally, I prefer the Perl-ish braces-on-same-line-except-for-
else
, but as long as everybody working on the code can cooperate, it really doesn't matter.我认为这完全是主观的,但是,我认为为您的团队建立代码标准并让每个人都使用相同的风格很重要。 话虽这么说,我喜欢第二个(并让我的团队使用它),因为当它不是您的代码时,它似乎更容易阅读。
I think it is completely subjective, however, I think it is important to establish code standards for your team and have everyone use the same style. That being said I like the second one (and have made my team use it) because it seems easier to read when it is not your code.