Where to wrap a line of code, especially long argu

2019-01-16 15:10发布

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);

How should that be wrapped?

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.

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?

14条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-16 15:18

First

If the args short enough and have (almost) similar length, I think below visually good enough

int variable_with_really_long_name = functionWhichDoMore(Argument1, ArgumentA2, 
                                                         ArgumentA3, Argument4, 
                                                         ArgumentA5, Argument6);

Second

When It getting worse, one column of argument really help

int variable_with_really_long_name = somefunctionWhichDoMore(Argument_Expand1, 
                                                             Argument2, 
                                                             Argument_Expand3, 
                                                             Argument_Expand4, 
                                                             Argument_Expand5, 
                                                             Argument6);

Third

But, now, How if it is worsen! What now? Try this one

int variable_with_really_long_name = someFunctionWhichDoMore
                                     (
                                       Argument_Expand_More1, 
                                       Argument_Expand_More2, 
                                       Argument_Expand3, Argument4, 
                                       Argument_Expand_More5, Argument6
                                     );

By the way, if you want a consistent look, use the third in all condition above.

Justify : Neatly put on and we know that it is a function call with lots of (6) args. And I like my code looks neat and !(so_ugly).

Critics are welcome. Please comment up.

查看更多
该账号已被封号
3楼-- · 2019-01-16 15:21

For me, it depends on just how long the argument list is. I don't like end of line layout much and it almost requires for editor support (e.g. emacs) to do it well.

If the method call is short enough to get it on one line, I'll do this:

int SomeReturnValue =
    SomeMethodWithLotsOfArguments(Argument1, Argument2, ...);

If method and variable fit on one line and arguments on another, I've done this:

int SomeReturnValue = SomeMethodWithLotsOfArguments
    (Argument1, Argument2, ... );

That makes my LISPy heart smile, but drive my colleagues nuts, so I've relented:

int SomeReturnValue = SomeMethodWithLotsOfArguments(
    Argument1, Argument2, ... );

I guess I'm just trying to say I haven't found a solution I'm really happy with, though this has some appeal for the really overlong cases due to its similarity to how we lay out curlies:

int SomeReturnValue = SomeMethodWithLotsOfArguments(
    Argument1, 
    Argument2,
);
查看更多
Lonely孤独者°
4楼-- · 2019-01-16 15:24
int SomeReturnValue = SomeMethodWithLotsOfArguments
(   Argument1,
    Argument2,
    Argument3,
    Argument4
);
查看更多
劫难
5楼-- · 2019-01-16 15:28

In functions with long parameter list, I wrap after each one or two parameters for readability (always keeping the same number of parameters on each line):

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

OR

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

depending on the list/parameter length.

查看更多
可以哭但决不认输i
6楼-- · 2019-01-16 15:28

I also use the ‘consistent indenting option’ as quoted by Edward Kmett. If there are a lot of arguments I tend to line-group them by relatedness where possible.

But for this particular example I'd probably leave it on one line, it's not that long.

I can't stand the ‘dangling wrap’ format as it can easily provide visual confusion conflicting with the (much more important) indenting. Dangling wraps are considered the ‘default’ style for many languages at the moment, but I don't know how it got that way. It's IMHO horrible.

查看更多
Melony?
7楼-- · 2019-01-16 15:29

There is no definitive answer for me on this. I do it on a case by case basis. If the function name is long, i definitely don't indent the other arguments to the same column as the previous arguments. If the function name is short, i usually indent following arguments to the same column, collecting as many arguments i can on one line (not one argument = one line). But if there is some pleasing symmetry, like in

int a = foo(a + b,
            c + d);

i would probably break that rule, and have the same number of arguments on each line.

查看更多
登录 后发表回答