This page is no longer maintained — Please continue to the home page at www.scala-lang.org

[Style Guide] alignment of parameters for long methods

3 replies
David Copeland
Joined: 2009-06-16,
User offline. Last seen 42 years 45 weeks ago.
http://davetron5000.github.com/scala-style/indentation/methods_with_numerous_arguments.html

I think most editors would have some trouble with this alignment technique.  To me, it's easier to just indent the parameters and not line them up with the opening paren:

foo(
  some,
  veryLarge,
  numberOf,
  "parameters")

This seems to be the easiest thing to construct and maintain (e.g. you don't need to change the indent of the params when you change the name of the method).

Dave

---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: [Style Guide] alignment of parameters for long methods
I would go one further and recommend

foo(
  some,
  veryLarge,
  numberOf,
  "parameters"
)

given that we're used to multi-line { } working this way.

I can't think of any compelling reason to keep ) on the same line but wrap } to the next line.
  --Rex


On Sat, Nov 21, 2009 at 4:48 PM, David Copeland <davec@naildrivin5.com> wrote:
http://davetron5000.github.com/scala-style/indentation/methods_with_numerous_arguments.html

I think most editors would have some trouble with this alignment technique.  To me, it's easier to just indent the parameters and not line them up with the opening paren:

foo(
  some,
  veryLarge,
  numberOf,
  "parameters")

This seems to be the easiest thing to construct and maintain (e.g. you don't need to change the indent of the params when you change the name of the method).

Dave

---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com


daniel
Joined: 2008-08-20,
User offline. Last seen 44 weeks 14 hours ago.
Re: [Style Guide] alignment of parameters for long methods
The reason to keep ) on the same line is precisely because we wrap } to the next line.  Parentheses and braces serve very different purposes within the language.  Unfortunately (or fortunately), braces can be used in many situations where parentheses are also called for.  Thus, when scanning code, it's easy to get them visually confused, especially given the minimal distinction between the two characters.  Keeping the closing parens on the same line as the last argument helps to reinforce that it's not a brace.  It's a visually jarring thing to do.

It also looks sort-of like Lisp, which always gives me a warm and fuzzy feeling inside.

I'll buy the "first arg, next line" idiom though.  It seems to be a much more scalable way of doing things, especially in the case of longer method names.

Daniel

On Sat, Nov 21, 2009 at 7:58 PM, Rex Kerr <ichoran@gmail.com> wrote:
I would go one further and recommend

foo(
  some,
  veryLarge,
  numberOf,
  "parameters"
)

given that we're used to multi-line { } working this way.

I can't think of any compelling reason to keep ) on the same line but wrap } to the next line.
  --Rex


On Sat, Nov 21, 2009 at 4:48 PM, David Copeland <davec@naildrivin5.com> wrote:
http://davetron5000.github.com/scala-style/indentation/methods_with_numerous_arguments.html

I think most editors would have some trouble with this alignment technique.  To me, it's easier to just indent the parameters and not line them up with the opening paren:

foo(
  some,
  veryLarge,
  numberOf,
  "parameters")

This seems to be the easiest thing to construct and maintain (e.g. you don't need to change the indent of the params when you change the name of the method).

Dave

---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com



ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: [Style Guide] alignment of parameters for long methods
On Sat, Nov 21, 2009 at 9:06 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
The reason to keep ) on the same line is precisely because we wrap } to the next line.  Parentheses and braces serve very different purposes within the language.  Unfortunately (or fortunately), braces can be used in many situations where parentheses are also called for.

In many of these cases, braces and parentheses do exactly the same thing:
  List(1,2) foreach { println(_) }
  List(1,2).foreach( println(_) )

The advantage of a separate line comes when you're doing complicated things like so:
  val a = List(
    myVal.myMethod(foo).bar ,
    "This is some string that goes here ,
    if (condition) myVal.someOtherMethod else func( "My number is " + (5+7) )
  )

because if you need to add more lines, you just add comma and line, and don't have to worry about matching parens.  Otherwise, that last list element would go
    if (condition) myVal.someOtherMethod else func( "My number is " + (5+7) ) )
and it's easier to get confused where the end of the List( ... ) is.

Both styles work okay; I'm just unconvinced that you gain anything by making parens work differently than braces.  And in that case, unless the Scala libs use one variant extremely heavily (at which point it's kind of the recommended style by default), I'd recommend "use either, be consistent" as the guide, not "do it this way".

What sort of easy-to-fall-into trouble could one get into if one mistook paired braces for paired parens?

  --Rex
 

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland