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.
我的课实际上有很多参数,所以这可能会影响你的反应。
13 个解决方案
#1
12
I agree with Patrick McElhaney; there is no need to nest it....
我同意Patrick McElhaney;没有必要筑巢....
Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );
There are a couple of small advantage here:
这里有一些小优势:
- You can set a break point on the second line and inspect 'aBlah'.
- Your diffs will be cleaner (changes more obvious) without nesting the statements, e.g. creating the new Blah is in an independent statement from adding it to the list.
你可以在第二行设置一个断点并检查'aBlah'。
你的差异将更清晰(更改更明显),而不会嵌套语句,例如创建新的Blah是一个独立的声明,将其添加到列表中。
#2
5
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 blah = new Blah(1,2,3,55);
int ID = Blahs.Add( blah );
#3
4
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:
所有数字都被添加到结果中。无需单独评论每个号码。评论“这些数字加在一起”将做到这一点。我打算这样做:
int result = Blahs.Add( new Blah(1, 2, 3, 55) );
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
但如果这些数字本身具有某些含义,则每个数字可以代表完全不同的数字,例如,如果Blah表示库存项目的类型。我会去
int ID = Blahs.Add( new Blah(
1, /* wtf is this */
2, /* wtf is this */
3, /* wtf is this */
55 /* and huh */
));
#4
4
int ID = Blahs.Add
(
new Blah
(
1, /* When the answer is within this percentage, accept it. */
2, /* Initial seed for algorithm */
3, /* Maximum threads for calculation */
55 /* Limit on number of hours, a thread may iterate */
)
);
#5
2
or
int ID = Blahs.Add(
new Blah( 1, 2, 3, 55 )
);
I must confess, though, that 76 times out of 77 I do what you did the first time.
但是,我必须承认,77次中有76次我做了你第一次做的事情。
#6
2
first way since you are inlining it anyway.
第一种方式,因为你无论如何都要内联它。
#7
2
I would use similar formatting as your first example, but without the redundant space delimiters before and after the parenthesis delimiters:
我将使用与第一个示例类似的格式,但在括号分隔符之前和之后没有冗余空格分隔符:
int id = BLahs.Add(new Blah(-1, -2, -3));
Note that I also wouldn't use an all upper-case variable name in this situation, which often implies something special, like a constant.
请注意,在这种情况下我也不会使用全大写变量名,这通常意味着一些特殊的变量,如常量。
#8
2
Either split it into two lines:
将它分成两行:
new_Blah = new Blah(-1, -2, -3)
int ID = BLahs.Add(new_Blah);
Or indent the new Blah(); call:
或者缩进新的Blah();呼叫:
int ID = BLahs.Add(
new Blah(-1, -2, -3)
);
Unless the arguments were long, in which case I'd probably do something like..
除非争论很长,在这种情况下我可能会做类似的事情......
int ID = BLahs.Add(new Blah(
(-1 * 24) + 9,
-2,
-3
));
As a slightly more practical example, in Python I quite commonly do the either of the following:
作为一个稍微更实际的例子,在Python中我通常会执行以下任一操作:
myArray.append(
someFunction(-1, -2, -3)
)
myArray.append(someFunction(
otherFunction("An Arg"),
(x**2) + 4,
something = True
))
#9
1
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.
一行,除非有大量数据。无论什么是第一个,我都会在大约十个项目或总共六十七个列中画出这一行。
#10
1
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.
无论Eclipse的自动格式化程序给我什么,所以当下一个开发人员在提交之前对代码和格式进行处理时,差异并没有奇怪的问题。
#11
0
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.
int ID = Blahs.Add(new Blah(1,2,3,55)); //数字n使得n的基数为4的数字组等于n的基数为6的数字组。
#12
0
The problem with
这个问题
Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );
is that it messes with your namespace. If you don't need a reference to the Blah you shouldn't create it.
是它与你的命名空间混淆。如果您不需要Blah的引用,则不应创建它。
#13
0
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 that Blah
directly again.
我要么将它作为一个单行程序,要么将新的Blah分配给一个变量,这取决于我是否需要再次直接引用Blah。
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.)
至于一对答案通过将每个参数放在带注释的单独行上解决的可读性问题,我将通过使用命名参数来解决这个问题。 (但不幸的是,并非所有语言都支持命名参数。)
int ID = BLahs.Add(new Blah( foo => -1, bar => -2, baz => -3 ));
#1
12
I agree with Patrick McElhaney; there is no need to nest it....
我同意Patrick McElhaney;没有必要筑巢....
Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );
There are a couple of small advantage here:
这里有一些小优势:
- You can set a break point on the second line and inspect 'aBlah'.
- Your diffs will be cleaner (changes more obvious) without nesting the statements, e.g. creating the new Blah is in an independent statement from adding it to the list.
你可以在第二行设置一个断点并检查'aBlah'。
你的差异将更清晰(更改更明显),而不会嵌套语句,例如创建新的Blah是一个独立的声明,将其添加到列表中。
#2
5
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 blah = new Blah(1,2,3,55);
int ID = Blahs.Add( blah );
#3
4
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:
所有数字都被添加到结果中。无需单独评论每个号码。评论“这些数字加在一起”将做到这一点。我打算这样做:
int result = Blahs.Add( new Blah(1, 2, 3, 55) );
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
但如果这些数字本身具有某些含义,则每个数字可以代表完全不同的数字,例如,如果Blah表示库存项目的类型。我会去
int ID = Blahs.Add( new Blah(
1, /* wtf is this */
2, /* wtf is this */
3, /* wtf is this */
55 /* and huh */
));
#4
4
int ID = Blahs.Add
(
new Blah
(
1, /* When the answer is within this percentage, accept it. */
2, /* Initial seed for algorithm */
3, /* Maximum threads for calculation */
55 /* Limit on number of hours, a thread may iterate */
)
);
#5
2
or
int ID = Blahs.Add(
new Blah( 1, 2, 3, 55 )
);
I must confess, though, that 76 times out of 77 I do what you did the first time.
但是,我必须承认,77次中有76次我做了你第一次做的事情。
#6
2
first way since you are inlining it anyway.
第一种方式,因为你无论如何都要内联它。
#7
2
I would use similar formatting as your first example, but without the redundant space delimiters before and after the parenthesis delimiters:
我将使用与第一个示例类似的格式,但在括号分隔符之前和之后没有冗余空格分隔符:
int id = BLahs.Add(new Blah(-1, -2, -3));
Note that I also wouldn't use an all upper-case variable name in this situation, which often implies something special, like a constant.
请注意,在这种情况下我也不会使用全大写变量名,这通常意味着一些特殊的变量,如常量。
#8
2
Either split it into two lines:
将它分成两行:
new_Blah = new Blah(-1, -2, -3)
int ID = BLahs.Add(new_Blah);
Or indent the new Blah(); call:
或者缩进新的Blah();呼叫:
int ID = BLahs.Add(
new Blah(-1, -2, -3)
);
Unless the arguments were long, in which case I'd probably do something like..
除非争论很长,在这种情况下我可能会做类似的事情......
int ID = BLahs.Add(new Blah(
(-1 * 24) + 9,
-2,
-3
));
As a slightly more practical example, in Python I quite commonly do the either of the following:
作为一个稍微更实际的例子,在Python中我通常会执行以下任一操作:
myArray.append(
someFunction(-1, -2, -3)
)
myArray.append(someFunction(
otherFunction("An Arg"),
(x**2) + 4,
something = True
))
#9
1
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.
一行,除非有大量数据。无论什么是第一个,我都会在大约十个项目或总共六十七个列中画出这一行。
#10
1
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.
无论Eclipse的自动格式化程序给我什么,所以当下一个开发人员在提交之前对代码和格式进行处理时,差异并没有奇怪的问题。
#11
0
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.
int ID = Blahs.Add(new Blah(1,2,3,55)); //数字n使得n的基数为4的数字组等于n的基数为6的数字组。
#12
0
The problem with
这个问题
Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );
is that it messes with your namespace. If you don't need a reference to the Blah you shouldn't create it.
是它与你的命名空间混淆。如果您不需要Blah的引用,则不应创建它。
#13
0
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 that Blah
directly again.
我要么将它作为一个单行程序,要么将新的Blah分配给一个变量,这取决于我是否需要再次直接引用Blah。
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.)
至于一对答案通过将每个参数放在带注释的单独行上解决的可读性问题,我将通过使用命名参数来解决这个问题。 (但不幸的是,并非所有语言都支持命名参数。)
int ID = BLahs.Add(new Blah( foo => -1, bar => -2, baz => -3 ));