Formatting multiple arguments passed to a function in Java

后端 未结 6 1032
广开言路
广开言路 2021-02-13 21:18

Often the number of arguments passed to a function can be large. Consider the following case:

calculate(dataManager.getLastUpdate().getNumberOfChildren(),
               


        
6条回答
  •  逝去的感伤
    2021-02-13 21:49

    According to the Sun's Java coding conventions, paragraph 4.1 "Wrapping Lines":

    When an expression will not fit on a single line, break it according to these general principles:

    • Break after a comma.
    • Break before an operator.
    • Prefer higher-level breaks to lower-level breaks.
    • Align the new line with the beginning of the expression at the same level on the previous line.
    • If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.

    The document also includes some examples for method calls:

    function(longExpression1, longExpression2, longExpression3,
             longExpression4, longExpression5);
    
    var = function1(longExpression1,
                    function2(longExpression2,
                              longExpression3));
    

提交回复
热议问题