javacoding-stylecode-structure

What is an acceptable way to format a long method call in Java?


Is it good style to write:

objectName.methodWithManyParameters(someLongParameter1, someLongParameter2, someLongParameter3, someLongParameter4, someLongParameter5);

(which is obviously far to long for one line) as

objectName.methodWithManyParameters
(
    someLongParameter1, 
    someLongParameter2, 
    someLongParameter3, 
    someLongParameter4, 
    someLongParameter5
);

Another way would be:

objectName.methodWithManyParameters(someLongParameter1, someLongParameter2, 
                                    someLongParameter3, someLongParameter4,
                                    someLongParameter5);

Solution

  • If you're working with others, or in a pre-existing code base, use whatever standards they've already been using. 80 vs 100 column, option #1/2/3, etc.

    If you're working on your own, Jordi's answer nails it. Use the Oracle conventions, and likely go with 100 character line lengths; we have modern screens that fit a lot, lot more text than monitors did in 1996.