C#约定 [英] C# convention

查看:79
本文介绍了C#约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到使用大括号的C#约定是在定义块的第一行后立即放置

左大括号

(类声明除外)像这样:


if(something){

stuff

}


这是为什么?有很多人这样做吗?似乎把支撑放在

单独的行上更好更容易阅读。在这本书中,我是一个读书的b $ b,它表示闭合支架与

第一行(如上所述)排列非常重要,我想是的可读性,但这是多么常见

这个?这看起来并不可读:


if(something)

{

stuff

}

I just realized that the C# convention for uses braces is to put the
opening brace immediately after the first line that defines the block
(except for class declarations) like this:

if (something) {
stuff
}

Why is that? Do a lot of people do this? It seems like putting braces on
separate lines is much nicer and easier to read. In the book I''m
reading, it says it''s important that the closing brace line up with the
first line (like above), I guess for readability, but how common is
this? It doesn''t seem as readable as this:

if (something)
{
stuff
}

推荐答案

哦亲爱的。大括号辩论。对于复活这个的人来说,有一个特殊的惩罚。

。 ;-)


我曾经对这所大学充满热情。我知道其他人对使用其他风格同样充满热情。甚至还有第三个

选项,看起来像这样:


if(something)

{stuff

...

}


二十年来,我有一个非常简单的规则:使用任何风格的IDE

免费为您提供。在像Visual Studio这样的情况下,IDE允许您选择,每个人都选择一种常见的风格并坚持下去。


真的! "可读"在旁观者的眼中,唯一不那么容易理解的代码是用不熟悉的方式格式化的代码,

强迫你做额外的精神努力阅读它。如果您的

公司的每个人都习惯于看到垂直排列的大括号,那么请使用

这种风格。如果每个人都习惯通过在行尾添加

开口大括号来节省垂直空间,那么就使用那种风格。


如果你是顾问,然后适应您的客户的风格。毕竟,那就是为什么他们付给你大笔钱的原因。 :-)


在过去15年里我曾经工作过的每个地方(因为我失去了卷曲

),我唯一能做的就是我想在这方面是一个

自动格式化程序来运行每个人的代码(包括我的),所以

它看起来都一样,而且没有,我不在乎它使用哪种惯例,

只要只有一个约定。


我的2¢值得...让游戏开始了! :-)

Oh dear. The curly brace debate. There''s a special punishment reserved
for people who resurrect this one. ;-)

I used to be passionate about this university. I knew others who were
equally passionate about using the other style. There was even a third
option, which looked like this:

if (something)
{ stuff
...
}

Twenty years on, I have a very simply rule: use whatever style the IDE
gives you for free. In cases like Visual Studio, where the IDE allows
you to choose, everybody pick one common style and stick to it.

Really! "Readable" is in the eyes of the beholder, and the only
less-readable code is code that''s formatted in an unfamiliar way, which
forces you to do extra mental work to read it. If everyone in your
company is accustomed to seeing braces lined up vertically, then use
that style. If everyone is used to saving vertical space by putting the
opening brace at the end of the line, then use that style.

If you''re a consultant, then adapt to the style of your client. That''s
why they pay you the big bucks, after all. :-)

At every place I''ve worked in the past 15 years (since I lost curly
brace religion), the only thing I''ve wanted in this regard was an
auto-formatter to run everyone''s code through (including mine), so that
it all looked the same, and no, I didn''t care which convention it used,
so long as there was just one convention.

My 2¢ worth... let the games begin! :-)


John Salerno< jo ****** @ NOSPAMgmail.com>写道:
John Salerno <jo******@NOSPAMgmail.com> wrote:
我刚才意识到使用大括号的C#约定是在定义块的第一行(类声明除外)之后立即放置
左大括号这个:

if(something){
stuff
}


你的意思是哪种惯例? MS类设计指南

图书馆开发人员似乎使用了你在下面使用的支撑,在我刚才看到的几个例子中。

为什么会这样?有很多人这样做吗?看起来像在单独的行上放括号更好更容易阅读。在我正在阅读的书中,它说闭合支架与第一行(如上所述)排列是很重要的,我想是为了可读性,但是有多常见
if(something)


}
I just realized that the C# convention for uses braces is to put the
opening brace immediately after the first line that defines the block
(except for class declarations) like this:

if (something) {
stuff
}
Which convention do you mean? The MS "Design Guidelines for Class
Library Developers" seems to use the bracing you''ve used below, in the
few examples I looked at just now.
Why is that? Do a lot of people do this? It seems like putting braces on
separate lines is much nicer and easier to read. In the book I''m
reading, it says it''s important that the closing brace line up with the
first line (like above), I guess for readability, but how common is
this? It doesn''t seem as readable as this:

if (something)
{
stuff
}




根据我的经验,这比前者(K& R)

支撑更为常见。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http: //www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件



In my experience, this is far more common than the former (K&R)
bracing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


布鲁斯伍德写道:
哦,亲爱的。大括号辩论。对于复活这个人的人来说,有一个特殊的惩罚。 ;-)
我的2¢值得...让比赛开始吧! : - )
Oh dear. The curly brace debate. There''s a special punishment reserved
for people who resurrect this one. ;-) My 2¢ worth... let the games begin! :-)




哦不,我做了什么!? :)



Oh no, what did I do!? :)


这篇关于C#约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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