您将如何格式化/缩进这段代码?

发布于 2024-07-09 18:00:59 字数 233 浏览 5 评论 0原文

您将如何格式化/缩进这段代码?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

把回忆走一遍 2024-07-16 18:00:59

我同意帕特里克·麦克尔哈尼的观点; 没有必要嵌套它......

Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );

这里有几个小优点:

  1. 您可以在第二行设置一个断点并检查“aBlah”。
  2. 如果没有嵌套语句,您的差异将会更清晰(变化更明显),例如,创建新的 Blah 是在一个独立的语句中,而不是将其添加到列表中。

I agree with Patrick McElhaney; there is no need to nest it....

Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );

There are a couple of small advantage here:

  1. You can set a break point on the second line and inspect 'aBlah'.
  2. 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.
故人爱我别走 2024-07-16 18:00:59

我会选择单线。 如果真正的参数使一行太长,我会用一个变量将其分解。

Blah blah = new Blah(1,2,3,55);
int ID = Blahs.Add( blah );

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 );
药祭#氼 2024-07-16 18:00:59

所有数字都被添加到结果中。 无需单独注释每个数字。 注释“这些数字相加”就可以了。 我将这样做:

int result = Blahs.Add( new Blah(1, 2, 3, 55) );

但如果这些数字本身具有某种含义,则每个数字都可以代表完全不同的东西,例如,如果 Blah 表示库存项目的类型。 我会和

int ID = Blahs.Add( new Blah(
    1, /* wtf is this */ 
    2, /* wtf is this */
    3, /* wtf is this */
    55 /* and huh */
));

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

int ID = Blahs.Add( new Blah(
    1, /* wtf is this */ 
    2, /* wtf is this */
    3, /* wtf is this */
    55 /* and huh */
));
此岸叶落 2024-07-16 18:00:59
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        */ 
    )          
);
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        */ 
    )          
);
时光清浅 2024-07-16 18:00:59

或者

int ID = Blahs.Add( 
            new Blah( 1, 2, 3, 55 )          
         );

我必须承认,77 次中有 76 次我会做你第一次做的事情。

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.

寄意 2024-07-16 18:00:59

第一种方法,因为无论如何你都会内联它。

first way since you are inlining it anyway.

自由范儿 2024-07-16 18:00:59

我将使用与第一个示例类似的格式,但在括号分隔符之前和之后没有多余的空格分隔符:

int id = BLahs.Add(new Blah(-1, -2, -3));

请注意,在这种情况下我也不会使用全大写的变量名称,这通常意味着一些特殊的东西,例如持续的。

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.

咽泪装欢 2024-07-16 18:00:59

要么将其分成两行:

new_Blah = new Blah(-1, -2, -3)
int ID = BLahs.Add(new_Blah);

要么缩进 new Blah(); 调用:

int ID = BLahs.Add(
    new Blah(-1, -2, -3)
);

除非参数很长,在这种情况下我可能会做类似的事情。

int ID = BLahs.Add(new Blah(
    (-1 * 24) + 9,
    -2,
    -3
));

作为一个更实际的例子,在Python中我通常会执行以下任一操作:

myArray.append(
    someFunction(-1, -2, -3)
)

myArray.append(someFunction(
    otherFunction("An Arg"),
    (x**2) + 4,
    something = True
))

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:

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:

myArray.append(
    someFunction(-1, -2, -3)
)

myArray.append(someFunction(
    otherFunction("An Arg"),
    (x**2) + 4,
    something = True
))
橙幽之幻 2024-07-16 18:00:59

一行,除非有很多数据。 我会在大约十个项目或总共六十、七十列之间划清界限,无论先到什么。

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.

回忆那么伤 2024-07-16 18:00:59

无论 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.

暖心男生 2024-07-16 18:00:59

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.

梦忆晨望 2024-07-16 18:00:59

问题

Blah aBlah = new Blah( 1, 2, 3, 55 );
int ID = Blahas.Add( aBlah );

是它会扰乱你的命名空间。 如果您不需要对 Blah 的引用,则不应创建它。

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.

三岁铭 2024-07-16 18:00:59

我要么将其作为单行代码执行,要么将 new Blah 分配给变量,具体取决于我是否需要再次直接引用该 Blah

至于几个答案通过将每个参数放在带有注释的单独行中解决的可读性问题,我将通过使用命名参数来解决这个问题。 (但不幸的是,并非所有语言都支持命名参数。)

int ID = BLahs.Add(new Blah( foo => -1, bar => -2, baz => -3 ));

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.

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 ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文