Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding style - keep parentheses on the same line or new line? [closed]

Suppose you are calling a function, where there's clearly a need to break down the statement into few lines, for readability's sake. However there are at least two way to do it:

Would you do this:

return render(request, template,
              {
                'var1' : value1,
                'var2' : value2,
                'var3' : value3
               }
             )

Or would you rather do that:

return render \
(
    request, template,
    {
        'var1' : value1,
        'var2' : value2,
        'var3' : value3
    }
)

Or, please suggest your own formatting. Please also list reasons why would you use a particular formatting and what's wrong with the other one.

Thanks

like image 472
Art Avatar asked Mar 07 '10 08:03

Art


People also ask

What is Allman style?

Allman styleThis style puts the brace associated with a control statement on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level.

What is K & R style?

K&R style minimises the amount of vertical space which the code consumes, while maintaining that the syntax of the control structure itself does not share lines with its contents. Ensuring a control structure doesn't share lines with its content is important.

Should curly braces appear on their own line?

We strongly recommend you format your curly brackets following Java conventions: Do not put a new line before an opening curly bracket. Always start a new line after an opening curly bracket. A closing curly bracket should always belong on a separate line, except for else statements.

When writing code what is the purpose of the parentheses at the end?

In many computer programming languages, parentheses have a special purpose. For example, they are frequently used to enclose arguments to functions and methods. In languages such as Lisp, parentheses define an s-expression. In regular expressions, parentheses are used for pattern grouping and capturing.


1 Answers

I'd probably do:

return render(
    request, 
    template,
    {
        'var1' : value1,
        'var2' : value2,
        'var3' : value3
    }
)

I would keep the bracket on the same line, so that searches for render( work. And because I find it clearer. But I'd put all the arguments on new lines.

like image 165
Douglas Leeder Avatar answered Nov 15 '22 07:11

Douglas Leeder