您将如何格式化/缩进这段代码?
您将如何格式化/缩进这段代码?
int ID = Blahs.Add( new Blah( -1, -2, -3) );
或
int ID = Blahs.Add( new Blah(
1,2,3,55
)
);
编辑:
我的类实际上有很多参数,因此可能会影响您的响应。
How would you format/indent this piece of code?
int ID = Blahs.Add( new Blah( -1, -2, -3) );
or
int ID = Blahs.Add( new Blah(
1,2,3,55
)
);
Edit:
My class has lots of parameters actually, so that might effect your response.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我同意帕特里克·麦克尔哈尼的观点; 没有必要嵌套它......
这里有几个小优点:
I agree with Patrick McElhaney; there is no need to nest it....
There are a couple of small advantage here:
我会选择单线。 如果真正的参数使一行太长,我会用一个变量将其分解。
I'd go with the one-liner. If the real arguments make one line too long, I would break it up with a variable.
所有数字都被添加到结果中。 无需单独注释每个数字。 注释“这些数字相加”就可以了。 我将这样做:
但如果这些数字本身具有某种含义,则每个数字都可以代表完全不同的东西,例如,如果
Blah
表示库存项目的类型。 我会和All numbers are being added to a result. No need to comment each number separately. A comment "these numbers are added together" will do it. I'm going to do it like this:
but if those numbers carry some meaning on their own, each number could stand for something entirely different, for example if
Blah
denotes the type for an inventory item. I would go with或者
我必须承认,77 次中有 76 次我会做你第一次做的事情。
or
I must confess, though, that 76 times out of 77 I do what you did the first time.
第一种方法,因为无论如何你都会内联它。
first way since you are inlining it anyway.
我将使用与第一个示例类似的格式,但在括号分隔符之前和之后没有多余的空格分隔符:
请注意,在这种情况下我也不会使用全大写的变量名称,这通常意味着一些特殊的东西,例如持续的。
I would use similar formatting as your first example, but without the redundant space delimiters before and after the parenthesis delimiters:
Note that I also wouldn't use an all upper-case variable name in this situation, which often implies something special, like a constant.
要么将其分成两行:
要么缩进 new Blah(); 调用:
除非参数很长,在这种情况下我可能会做类似的事情。
作为一个更实际的例子,在Python中我通常会执行以下任一操作:
Either split it into two lines:
Or indent the new Blah(); call:
Unless the arguments were long, in which case I'd probably do something like..
As a slightly more practical example, in Python I quite commonly do the either of the following:
一行,除非有很多数据。 我会在大约十个项目或总共六十、七十列之间划清界限,无论先到什么。
One line, unless there's a lot of data. I'd draw the line at about ten items or sixty, seventy columns in total, whatever comes first.
无论 Eclipse 的自动格式化程序给了我什么,所以当下一个开发人员在提交之前处理该代码和格式时,差异不会出现奇怪的问题。
Whatever Eclipse's auto-formatter gives me, so when the next dev works on that code and formats before committing, there aren't weird issues with the diff.
int ID = Blahs.Add(new Blah(1,2,3,55)); // 数字 n,使得 n 的 4 进制数字集合等于 n 的 6 进制数字集合。
int ID = Blahs.Add(new Blah(1,2,3,55)); // Numbers n such that the set of base 4 digits of n equals the set of base 6 digits of n.
问题
是它会扰乱你的命名空间。 如果您不需要对 Blah 的引用,则不应创建它。
The problem with
is that it messes with your namespace. If you don't need a reference to the Blah you shouldn't create it.
我要么将其作为单行代码执行,要么将
new Blah
分配给变量,具体取决于我是否需要再次直接引用该Blah
。至于几个答案通过将每个参数放在带有注释的单独行中解决的可读性问题,我将通过使用命名参数来解决这个问题。 (但不幸的是,并非所有语言都支持命名参数。)
I'd either do it as a one-liner or assign the
new Blah
to a variable, depending on whether I'll need to reference thatBlah
directly again.As far as the readability issue which a couple answers have addressed by putting each argument on a separate line with comments, I would address that by using named parameters. (But not all languages support named parameters, unfortunately.)