-2

Let's say you have a signature like so:

public void MyFooBar(IExtraSuperLongTypeName param1, TypeA param2 ISomeotherReallyLongTypeName param3, TypeB param4, TypeC param5)

Formatting in on one line is ugly and hard to read. A style that resharper uses to format this is to push the parameters down like so

public void MyFooBar(IExtraSuperLongTypeName param1, TypeA param2,
    ISomeotherReallyLongTypeName param3, TypeB param4, TypeC param5)

But I still find that hard to immediately interpret what the parameters are.

I have also seen this approach used:

public void MyFooBar(IExtraSuperLongTypeName param1,
                     TypeA param2,
                     ISomeotherReallyLongTypeName param3,
                     TypeB param4,
                     TypeC param5)

I find this much easir to read, but I'm not a fan of having all that white space in the way, and I don't like how the spacing of the whitespace may not match the tab size exactly. Additionally, if you method name is exceeding long, it can still push the parameters too far to the right.

I have thought of another way to format code like so:

public void MyFooBar
(
    IExtraSuperLongTypeName param1,
    TypeA param2,
    ISomeotherReallyLongTypeName param3,
    TypeB param4,
    TypeC param5
)

To me, this is easy to read and doesn't suffer from the disadvantages of the above approach. Can anyone see any issues with the above method of formatting code? If I can't find any reasons not to write functions that way, I'll start using it in my projects for long method signatures. The same approach can be made to function calls, eg:

MyFooBar myFooBar = MyFooBar
(
    arg1,
    arg2,
    arg3,
    arg4,
    arg5
);

closed as primarily opinion-based by gnat, amon, Thomas Owens Aug 19 '16 at 14:36

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I can see that has been put on hold, due to possible opinion based answers, but where should I go to get this question answered then? Also, solid resources do exist for these sort of thing, eg styling guidelines for particular languages. – eltommo Aug 22 '16 at 2:13
3

I agree that your formatting is easy to read and I only see two minor disadvantages:

  • It uses much space (lines of code)
  • It looks like a code block (if you mix up the parentheses with braces)

The better approach is in most cases to avoid functions with many arguments. You can do this for example by putting argument variables into a struct or object. Or you can sometimes split the function up into multiple simpler functions.

  • 1
    In general, I agree with you for reducing arguments to functions. However, where I work, we tend to favor longer, more descriptive variable/parameter name over condensed ones. I think more descriptive names is well worth the cost of length, but it does lead to longer signatures even for a small number of arguments in certain circumstances. Eg public ICalculationResult CalculateResult(IMotorUnitCalculatorConfiguration configuration, MotorUnitModel model, MotorUnitVaribales variables) is a whopper with only thee parameters – eltommo Aug 19 '16 at 7:35
  • Agreed, using descriptive names is one of the most important things in programming. But with three parameters, I'd say that there is no huge readability difference between the different formatting variants. – Frank Puffer Aug 19 '16 at 7:51
0

It can be a code smell when more than say 3 parameters (they are not homogeneous like x, y, z) are added; over time were added.

Make a parameter class where most parameters go into. Look whether responsibilities are separated and such.

The reason is simple: after 3 parameters the logic becomes tangled. Now should something in the parameters need to be changed, the function signature will not change anymore, and defaults may be provided. Look whether such defaults are already in effect at the call sites.

So as an answer to the original question:

  • stay with packing the most on a line.

When you feel the itch to list them one below the other, a parameter class is imminent. Go by intuition.

In rare cases such a method might become its own class.

0

It is a coding style. Any of them (excluding the looong single line) is acceptable and are indeed used in real programming teams. There are pros and cons with each approach since it is more or less a compromise extra lines vs. autodocumentation. And as that autodocumentation can exists in comments just above or under the code if you use javadocs or python doc strings for example, none of the approaches has clearly superseded the others.

So what matters in real world is only coherence. If your co-workers use one style, do use it too. And ensure a single style is used for any single project.

Above is mainly for function declarations. Multiline function calls is seldom used, because IDEs automatically help you by showing the parameter names when you type and many compilers control parameter coherence.

-1

Your method can be used but I will agree with Frank that try to add related arguments in a struct/class and use that as the function argument. Besides this, I'll suggest you to use a comment above a function description like there is in android studio (javadoc). This way it is much easier to get a brief knowledge of what the function does, what arguments it use and the meaning of each arguments right there in a proper formatted way. Like

/* Function to do something
** @arg1 Length of list
** @arg2 Size of Square side
*/
int func (int arg1, int arg2) {
    // Do something
}
  • 1
    This type of comment is dangerous. It tends to get out of sync with the actual code when the function signature is modified. Have seen this for too many times. Better let the types and variable names speak for themselves. – Frank Puffer Aug 19 '16 at 11:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.