代码样式:如何格式if if,ifelse,else语句 [英] Code Styling: How to format if, ifelse, else statements

查看:297
本文介绍了代码样式:如何格式if if,ifelse,else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过if,ifelse,else语句(花括号{})的许多不同格式。我想知道如果将这些花括号放在特定位置,它会影响代码。如果他们不这样做,你能告诉我哪一个看起来更有组织吗?提前致谢!





例如:



I have seen many different formats for if, ifelse, else statements(The curly braces {}). I would like to know if by placing these curly braces in a specific position, it would affect the code. If they do not, could you tell me which one looks more "organized?" Thanks in advance!


examples:

if(something) {
    do this;
}
//---------
if(something)
{
do this;
}
//---------
if(something)
{do this;}

推荐答案

我的答案是:使用Visual Studio格式设置,但如果需要,可以自定义它们。



在Visual Studio中,它是最好将所有格式设置在设置中定义的格式规则上,在允许选项集的框架中。换句话说,如果您从文件中剪切所有代码并粘贴它,它将是自动格式化,这将是最好的样式。调整Visual Studio设置,而不是文本。



坦率地说,还有更多内容:评论等等。例如,我严格按照我的规则,有空行和不存在的地方,以及放置 region 标记的位置(如果有的话)。格式化规则不涉及其中一些方面。



顺便说一句,经过一些争论,我曾经为使用不同格式风格的人建立了通用规则。规则是:整洁,让每个团队成员都使用他/他自己的风格!为什么?因为任何人都可以根据自己的口味重新格式化。它不会造成任何伤害,因为即使此代码提交到修订控制代码库,其他开发人员也可以以某种不同的方式重新格式化它;它不需要时间。附加规则是:触摸文件的最后一个是主人。现在,片断,没有争论。



但是代码风格本身(不是格式化)非常重要。



-SA
My answer would be: use Visual Studio formatting settings, but customize them if you want.

In Visual Studio, it's the best to base all formatting on formatting rules defined in settings, in the framework of the sets of allowed options. In other words, if you cut all code from the file and paste it, it will be auto-formatting, and this will be the best style. Tune your Visual Studio settings, not text.

Frankly, there is more to it: comment and something else. I, for example, strictly follow my rules about there there are blank lines and where not, and where to put region markup, if any. Some of these aspects are not covered by formatting rules.

By the way, after some arguments, I once established the common rule for people using different formatting style. The rule is: be neat and let every team member use he/his own style! Why? Because anyone can reformat it by one's taste. It cannot make any harm, because, even if this code is committed to revision control code base, the other developer can reformat it in some different style; it takes no time. Additional rule is: the last one who touched the file is the master. And now, piece, no arguments.

But the code style itself (not formatting) is extremely important.

—SA


这篇关于代码样式:如何格式if if,ifelse,else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆