C#编码样式 - 行长/换行符 [英] C# coding style - line length / wrapping lines

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

问题描述

换行是否有助于代码可读性?

Does line wrapping help with code readability?

使用行继续有一个普遍接受的礼仪吗?

Is there a generally accepted etiquette for using line continuations?

为什么使用这个:

SomeMethod(int someInt, Object someObject,
   String someString, bool someBool)
{
    ...
}

p>

Instead of this:

SomeMethod(int someInt, Object someObject, String someString, bool someBool)
{
    ...
}

编辑:重写我的问题从行延续到换行

re-worded my question from line continuation to line wrapping

推荐答案

线路延续不需要在C#中使用,因为需要显式行终止符(; )。

Line continuations are not used in C#, since an explicit line terminator (;) is required.

你要问,在风格方面,是否是一个好主意,将一条线分成多行,这是有争议的。 StyleCop规则强制要么将线定义在单个线上,要么将每个元素定义在单独的线上。我个人认为这是一个很好的指南,如果它太长,不适合一个好的80-90字符宽的编辑器,我通常选择完全打破一条线。

If you're asking, in terms of style, whether it's a good idea to break a line into multiple lines, that's debatable. The StyleCop rules force a line to either be defined on a single line, or for every element to be on a separate line. I personally think this is a good guideline, and I usually choose to break a line completely into its parts if it's too long to fit into a good 80-90 character wide editor.

编辑以回复您的新问题:

Edit in response to your new question:

在这种情况下,我将遵循上述指南。个人而言,在你的具体情况下,我会把这一行留在一行:

In this case, I would follow the guidelines above. Personally, in your specific case, I'd leave this on one line:

SomeMethod(int someInt, Object someObject, String someString, bool someBool) 
{ 
    ... 
} 

这是一个很好的,短的moethod声明,我看不到分裂它的理由。如果参数的数量和类型的长度对于一行文本来说变得很长,我只会拆分它。

This is a nice, short moethod declaration, and I see no reason to split it. I'd only split it if the number of arguments, and the lengths of the types, became far to long for one line of text.

如果你拆分它,但是,我将它分成每个参数的单独的行:

If you do split it, however, I'd split it into separate lines for each argument:

SomeMethod(
    int someInt, 
    Object someObject, 
    String someString, 
    bool someBool) 
{ 
    ... 
} 

这样,至少它是明显的如何和为什么它拆分,开发人员不会意外跳过一个参数,因为两个在一行。

This way, at least it's obvious how and why it's split, and a developer won't accidentally skip one argument since two are on a single line.

这篇关于C#编码样式 - 行长/换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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