Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to wrap a line of code, especially long argument lists? [closed]

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?

like image 787
coobird Avatar asked Nov 12 '08 09:11

coobird


3 Answers

int SomeReturnValue = SomeMethodWithLotsOfArguments
(   Argument1,
    Argument2,
    Argument3,
    Argument4
);
like image 170
kenny Avatar answered Nov 16 '22 12:11

kenny


The option 3 suggested

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

is a better way as it gives a good feel. If the lengths of arguments are more or less same, then we can put them together so that they line up as a table for example

int SomeReturnValue = SomeMethodWithLotsOfArguments(
    Argument1,    Argument2,    Argument3,    Argument4,
    Argument005,  Argument006,  Argument7,    Argument8
);
like image 34
Dheer Avatar answered Nov 16 '22 10:11

Dheer


I prefer this way:

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

Where the line ends closest to your current max line width (whatever that is) and the next line is indented your usual indent level (whatever that is) relative to the equals sign.

Not sure why, but I think it's the most readable option in most situations. However, I chose not to be pedantic about these things and I always prefer whatever is most readable for a given section of code, even if that may break some indenting or formatting rules (within limits, of course).

One example of this would be if the function required many arguments or the argiments where themselves complex, then I might chose something like this instead:

int SomeReturnValue = SomeMethodWithLotsOfArguments(
                        Argument1 + Expression1 + Expression2, 
                        Argument2 - Expression3 * Expression4, 
                        Argument3, 
                        Argument4 * Expression5 + Expression6 - Expression7);

Of course, if the argument expressions are very long or complex it would be better to do the calculations before the function call and use temporary values to store the results.

like image 6
Anders Sandvig Avatar answered Nov 16 '22 10:11

Anders Sandvig