在哪里包装一行代码,尤其是长参数列表? [英] Where to wrap a line of code, especially long argument lists?

查看:86
本文介绍了在哪里包装一行代码,尤其是长参数列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包装代码行的首选方式是什么,特别是涉及长参数列表时?

What's your preferred way of wrapping lines of code, especially when it comes to long argument lists?

存在一些与换行有关的问题(例如编写代码时是否包装文字?行宽格式标准 ),但我一直找不到能够覆盖代码行的地方.

There has been several questions relating to wrapping lines (such as When writing code do you wrap text or not? and Line width formatting standard), but I haven't been able to find one which covers where to wrap a line of code.

假设我们有一行代码像下面的示例一样不断运行:

Let's say we have a line of code that keeps going and going like this example:

int SomeReturnValue = SomeMethodWithLotsOfArguments(Argument1, Argument2, Argument3, Argument4);

应该如何包装?

以下是我可以想到的几种方法以及它们的一些缺点:

Here's a few ways I can think of, and some of their downsides:

int SomeReturnValue = SomeMethodWithLotsOfArguments(Argument1, Argument2,
    Argument3, Argument4);

我个人不喜欢该选项,因为格式似乎在视觉上将参数列表与我尝试调用的方法分开,尤其是因为在新行.

I personally don't prefer that option because the formatting seems to visually separate the argument list from the method I am trying to call, especially since there is an assignment equals sign ("=") right above the orphanged arguments on the new line.

所以,有一段时间我采用了以下方法:

So, for a while I went with the following approach:

int SomeReturnValue = SomeMethodWithLotsOfArguments(Argument1,
                                                       Argument2,
                                                       Argument3,
                                                       Argument4);

在这里,所有参数都捆绑在一起,都位于方法的第一个参数的侧面.但是,有一个陷阱是,由于制表符缩进的空格数量,参数列表将不会始终排在第二行之后. (而且输入额外的空格进行格式化将非常耗时.)

Here, the arguments are all bundled together, all on the side of the method's first argument. However, one catch is that the argument list won't always line up in the second line onwards because of the number of spaces that the tab indents. (And typing extra spaces for formatting would be too time consuming.)

其中一个答案前面的问题建议采用以下格式:

An answer in the one of the previous questions suggested the following format:

int SomeReturnValue = SomeMethodWithLotsOfArguments(
    Argument1,
    Argument2,
    Argument3,
    Argument4
);

由于它的视觉吸引力,我实际上很喜欢这种格式,但是它也确实在视觉上将参数与列表所属的方法区分开.另外,我更喜欢一个方法调用不占用太多行.

I actually like this format, due to its visual appeal, but it also it does visually separate the arguments from the method that the list belongs to. Also, I prefer to have a single method call not take up too many lines.

所以,我的问题是,首先不涉及防止代码行太长的问题您如何建议包装代码行? strong>具体来说,当涉及到长参数列表时,在哪里打破代码行的好地方?

So, my question is, without getting into the issue of preventing a code of line from getting too long in the first place, how would you recommend wrapping lines of code? Specifically, where is a good place to break a line of code, when it comes to long argument lists?

推荐答案

int SomeReturnValue = SomeMethodWithLotsOfArguments
(   Argument1,
    Argument2,
    Argument3,
    Argument4
);

这篇关于在哪里包装一行代码,尤其是长参数列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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