Often the number of arguments passed to a function can be large. Consider the following case:
calculate(dataManager.getLastUpdate().getNumberOfChildren(),
dataManager.getLastUpdate().getNumberOfParents(),
dataManager.getLastUpdate().getNumberOfGrandChildren(),
long milliseconds,
int somethingelse)
Is there a guideline in Java
that offers a way to align the arguments? Fitting all arguments in a line would not look pretty.
I wholeheartedly agree with your example of having one argument per line, all lined up under each other.
It makes it very easy to scan down the list to see what is there or what is missing.
It also makes it easier to document null values as being "// user id" or something similar.
I find it's particularly easy to visually parse, rather than having several long lines of densely packed values that may often look alike.
When I have to call a method like this I like to put the arguments on their own line, like so:
Obviously this is a personal preference, but if you're working with others on code, try to conform to the conventions already set forth.
Referring to your example, Eclipse and other IDEs format it the way you have above (1 argument per line, all left aligned) and usually that looks pretty good.
I'll put my little sand grain here, long time ago some developer named Esteban suggested me this kind of formatting, which I 1st thought it was ugly after a while no other way of doing it is enough pleasent for me:
I find this really clear, very easy to add/delete new arguments, the # of arguments clear, only one argument per line, method call end really clear, etc...
Similar pattern for defining the method too
And finally same pattern for nested calls, StringBuilder typicall sequence
The only problem I found is that IDE formatters never allow this 'comma at the beginning' approach which is really interesting, and a lot more readable than any other I've tried.
Hope it adds something interesting
I might assign the return values of the getNumberOf*() methods to variables:
According to the Sun's Java coding conventions, paragraph 4.1 "Wrapping Lines":
The document also includes some examples for method calls: