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

[Style Guide] Class values

8 replies
Colin Howe 2
Joined: 2009-09-12,
User offline. Last seen 42 years 45 weeks ago.
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin


David Copeland
Joined: 2009-06-16,
User offline. Last seen 42 years 45 weeks ago.
Re: [Style Guide] Class values
I think the second form is probably useful for classes with "too many" constructor args to fit on one line.  I guess "too many" could be defined as "takes longer than 70 characters" or "more than 3 parameters"?

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



On Sun, Nov 22, 2009 at 6:03 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin



daniel
Joined: 2008-08-20,
User offline. Last seen 44 weeks 14 hours ago.
Re: [Style Guide] Class values
I use the second form, but only when the number of arguments starts to extend beyond the width of my editor.  A more general guideline might be 100-120 characters in width, at which point the second style should be used.

The first style though is by far the most common case.  I think it's pretty bad practice to have that many constructor arguments in the first place.  Yes, there are cases where it is unavoidable, but by-and-large, you can do without.  Yes, I realize this is another "if/then" guideline, but I think it's well-worth it to try and keep most constructors on a single line.  Think about it, which of these is more readable?

class Test(val str: String) {
  def process = str + "foo"
}

class Test(
    val str: String) {
  def process = str + "foo"
}

The latter just pointlessly wastes a whole line worth of vertical space.  My vote: keep things on one line unless it starts to become unreadable (circa 100-120 chars), at which point, reformat the arguments into the "one per line" approach.

Daniel

On Sun, Nov 22, 2009 at 8:52 AM, David Copeland <davec@naildrivin5.com> wrote:
I think the second form is probably useful for classes with "too many" constructor args to fit on one line.  I guess "too many" could be defined as "takes longer than 70 characters" or "more than 3 parameters"?

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



On Sun, Nov 22, 2009 at 6:03 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin




davetron5000
Joined: 2009-06-07,
User offline. Last seen 2 years 31 weeks ago.
Re: [Style Guide] Class values
Here's a stab at this:

http://github.com/davetron5000/scala-style/commit/c313a4bb2350e73913838dc300e392f5d833afa4

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



On Sun, Nov 22, 2009 at 9:56 AM, Daniel Spiewak <djspiewak@gmail.com> wrote:
I use the second form, but only when the number of arguments starts to extend beyond the width of my editor.  A more general guideline might be 100-120 characters in width, at which point the second style should be used.

The first style though is by far the most common case.  I think it's pretty bad practice to have that many constructor arguments in the first place.  Yes, there are cases where it is unavoidable, but by-and-large, you can do without.  Yes, I realize this is another "if/then" guideline, but I think it's well-worth it to try and keep most constructors on a single line.  Think about it, which of these is more readable?

class Test(val str: String) {
  def process = str + "foo"
}

class Test(
    val str: String) {
  def process = str + "foo"
}

The latter just pointlessly wastes a whole line worth of vertical space.  My vote: keep things on one line unless it starts to become unreadable (circa 100-120 chars), at which point, reformat the arguments into the "one per line" approach.

Daniel

On Sun, Nov 22, 2009 at 8:52 AM, David Copeland <davec@naildrivin5.com> wrote:
I think the second form is probably useful for classes with "too many" constructor args to fit on one line.  I guess "too many" could be defined as "takes longer than 70 characters" or "more than 3 parameters"?

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



On Sun, Nov 22, 2009 at 6:03 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin





Colin Howe 2
Joined: 2009-09-12,
User offline. Last seen 42 years 45 weeks ago.
Re: [Style Guide] Class values
We might want to consider what happens when we have traits and classes on there too :-)



On Sun, Nov 22, 2009 at 3:15 PM, David Copeland <davetron5000@gmail.com> wrote:
Here's a stab at this:

http://github.com/davetron5000/scala-style/commit/c313a4bb2350e73913838dc300e392f5d833afa4

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



On Sun, Nov 22, 2009 at 9:56 AM, Daniel Spiewak <djspiewak@gmail.com> wrote:
I use the second form, but only when the number of arguments starts to extend beyond the width of my editor.  A more general guideline might be 100-120 characters in width, at which point the second style should be used.

The first style though is by far the most common case.  I think it's pretty bad practice to have that many constructor arguments in the first place.  Yes, there are cases where it is unavoidable, but by-and-large, you can do without.  Yes, I realize this is another "if/then" guideline, but I think it's well-worth it to try and keep most constructors on a single line.  Think about it, which of these is more readable?

class Test(val str: String) {
  def process = str + "foo"
}

class Test(
    val str: String) {
  def process = str + "foo"
}

The latter just pointlessly wastes a whole line worth of vertical space.  My vote: keep things on one line unless it starts to become unreadable (circa 100-120 chars), at which point, reformat the arguments into the "one per line" approach.

Daniel

On Sun, Nov 22, 2009 at 8:52 AM, David Copeland <davec@naildrivin5.com> wrote:
I think the second form is probably useful for classes with "too many" constructor args to fit on one line.  I guess "too many" could be defined as "takes longer than 70 characters" or "more than 3 parameters"?

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



On Sun, Nov 22, 2009 at 6:03 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin






davetron5000
Joined: 2009-06-07,
User offline. Last seen 2 years 31 weeks ago.
Re: [Style Guide] Class values
Good point.  This is the logical conclusion of that:

http://github.com/davetron5000/scala-style/commit/f283db1ec5141bb7b0675ae6dc8c7256ef4e944d

however, it is really nasty looking (esp. w/out syntax highlighting).

Nevertheless, I'm not sure how to make it look better; it's just an ugly class.

A related question is the ordering of classes being extended; it seems logical to me to have the first class extended be the most clear "superclass", and the remaining with less and less related "mixins", but I'm not sure if this would always be the case....

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



On Sun, Nov 22, 2009 at 10:30 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
We might want to consider what happens when we have traits and classes on there too :-)



On Sun, Nov 22, 2009 at 3:15 PM, David Copeland <davetron5000@gmail.com> wrote:
Here's a stab at this:

http://github.com/davetron5000/scala-style/commit/c313a4bb2350e73913838dc300e392f5d833afa4

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



On Sun, Nov 22, 2009 at 9:56 AM, Daniel Spiewak <djspiewak@gmail.com> wrote:
I use the second form, but only when the number of arguments starts to extend beyond the width of my editor.  A more general guideline might be 100-120 characters in width, at which point the second style should be used.

The first style though is by far the most common case.  I think it's pretty bad practice to have that many constructor arguments in the first place.  Yes, there are cases where it is unavoidable, but by-and-large, you can do without.  Yes, I realize this is another "if/then" guideline, but I think it's well-worth it to try and keep most constructors on a single line.  Think about it, which of these is more readable?

class Test(val str: String) {
  def process = str + "foo"
}

class Test(
    val str: String) {
  def process = str + "foo"
}

The latter just pointlessly wastes a whole line worth of vertical space.  My vote: keep things on one line unless it starts to become unreadable (circa 100-120 chars), at which point, reformat the arguments into the "one per line" approach.

Daniel

On Sun, Nov 22, 2009 at 8:52 AM, David Copeland <davec@naildrivin5.com> wrote:
I think the second form is probably useful for classes with "too many" constructor args to fit on one line.  I guess "too many" could be defined as "takes longer than 70 characters" or "more than 3 parameters"?

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



On Sun, Nov 22, 2009 at 6:03 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin







daniel
Joined: 2008-08-20,
User offline. Last seen 44 weeks 14 hours ago.
Re: [Style Guide] Class values
I would vote for indented four spaces on long strings of arguments.  This distinguishes arguments and parent types from the actual class body.

Daniel

On Sun, Nov 22, 2009 at 10:22 AM, David Copeland <davetron5000@gmail.com> wrote:
Good point.  This is the logical conclusion of that:

http://github.com/davetron5000/scala-style/commit/f283db1ec5141bb7b0675ae6dc8c7256ef4e944d

however, it is really nasty looking (esp. w/out syntax highlighting).

Nevertheless, I'm not sure how to make it look better; it's just an ugly class.

A related question is the ordering of classes being extended; it seems logical to me to have the first class extended be the most clear "superclass", and the remaining with less and less related "mixins", but I'm not sure if this would always be the case....

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



On Sun, Nov 22, 2009 at 10:30 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
We might want to consider what happens when we have traits and classes on there too :-)



On Sun, Nov 22, 2009 at 3:15 PM, David Copeland <davetron5000@gmail.com> wrote:
Here's a stab at this:

http://github.com/davetron5000/scala-style/commit/c313a4bb2350e73913838dc300e392f5d833afa4

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



On Sun, Nov 22, 2009 at 9:56 AM, Daniel Spiewak <djspiewak@gmail.com> wrote:
I use the second form, but only when the number of arguments starts to extend beyond the width of my editor.  A more general guideline might be 100-120 characters in width, at which point the second style should be used.

The first style though is by far the most common case.  I think it's pretty bad practice to have that many constructor arguments in the first place.  Yes, there are cases where it is unavoidable, but by-and-large, you can do without.  Yes, I realize this is another "if/then" guideline, but I think it's well-worth it to try and keep most constructors on a single line.  Think about it, which of these is more readable?

class Test(val str: String) {
  def process = str + "foo"
}

class Test(
    val str: String) {
  def process = str + "foo"
}

The latter just pointlessly wastes a whole line worth of vertical space.  My vote: keep things on one line unless it starts to become unreadable (circa 100-120 chars), at which point, reformat the arguments into the "one per line" approach.

Daniel

On Sun, Nov 22, 2009 at 8:52 AM, David Copeland <davec@naildrivin5.com> wrote:
I think the second form is probably useful for classes with "too many" constructor args to fit on one line.  I guess "too many" could be defined as "takes longer than 70 characters" or "more than 3 parameters"?

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



On Sun, Nov 22, 2009 at 6:03 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin








davetron5000
Joined: 2009-06-07,
User offline. Last seen 2 years 31 weeks ago.
Re: [Style Guide] Class values
I agree it's a bit more readable:

    class Person(
        name: String,
        age: Int,
        birthdate: Date,
        astrologicalSign: String,
        shoeSize: Int,
        favoriteColor: java.awt.Color)
      extends Entity
      with Logging
      with Identifiable
      with Serializable {

      def foo = ...
    }

The opposite might be a bit more consistent:

    class Person(
      name: String,
      age: Int,
      birthdate: Date,
      astrologicalSign: String,
      shoeSize: Int,
      favoriteColor: java.awt.Color)
        extends Entity
        with Logging
        with Identifiable
        with Serializable {
     
      def foo = ...
    }


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



On Sun, Nov 22, 2009 at 12:18 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
I would vote for indented four spaces on long strings of arguments.  This distinguishes arguments and parent types from the actual class body.

Daniel

On Sun, Nov 22, 2009 at 10:22 AM, David Copeland <davetron5000@gmail.com> wrote:
Good point.  This is the logical conclusion of that:

http://github.com/davetron5000/scala-style/commit/f283db1ec5141bb7b0675ae6dc8c7256ef4e944d

however, it is really nasty looking (esp. w/out syntax highlighting).

Nevertheless, I'm not sure how to make it look better; it's just an ugly class.

A related question is the ordering of classes being extended; it seems logical to me to have the first class extended be the most clear "superclass", and the remaining with less and less related "mixins", but I'm not sure if this would always be the case....

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



On Sun, Nov 22, 2009 at 10:30 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
We might want to consider what happens when we have traits and classes on there too :-)



On Sun, Nov 22, 2009 at 3:15 PM, David Copeland <davetron5000@gmail.com> wrote:
Here's a stab at this:

http://github.com/davetron5000/scala-style/commit/c313a4bb2350e73913838dc300e392f5d833afa4

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



On Sun, Nov 22, 2009 at 9:56 AM, Daniel Spiewak <djspiewak@gmail.com> wrote:
I use the second form, but only when the number of arguments starts to extend beyond the width of my editor.  A more general guideline might be 100-120 characters in width, at which point the second style should be used.

The first style though is by far the most common case.  I think it's pretty bad practice to have that many constructor arguments in the first place.  Yes, there are cases where it is unavoidable, but by-and-large, you can do without.  Yes, I realize this is another "if/then" guideline, but I think it's well-worth it to try and keep most constructors on a single line.  Think about it, which of these is more readable?

class Test(val str: String) {
  def process = str + "foo"
}

class Test(
    val str: String) {
  def process = str + "foo"
}

The latter just pointlessly wastes a whole line worth of vertical space.  My vote: keep things on one line unless it starts to become unreadable (circa 100-120 chars), at which point, reformat the arguments into the "one per line" approach.

Daniel

On Sun, Nov 22, 2009 at 8:52 AM, David Copeland <davec@naildrivin5.com> wrote:
I think the second form is probably useful for classes with "too many" constructor args to fit on one line.  I guess "too many" could be defined as "takes longer than 70 characters" or "more than 3 parameters"?

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



On Sun, Nov 22, 2009 at 6:03 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin









ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: [Style Guide] Class values
Four spaces is a multiple of two spaces--you might confuse it with two layers of nesting.

It's better to use an odd number (three or five) to distinguish it visually.

Also, my preferred method for supremely long classes is like so:

class myDerivedClassWithTooManyParametersAndMixins(
  // params line by line
  // lone paren tells us to watch for extends!
)
extends myFirstClass(
  new myIncreasinglyBloatedExample(
    lookWeHaveTwoLevelsOfNesting,
    isntThisAMess),
  andWeStillHave,
  many,
  parameters)
with mySecondClass
with myThirdClass[whichMayAsWellBeGeneric]  // no brace here--it goes solo to set off the code
{
  def thankGoodness = true
}

It is, admittedly, still ugly, but at least all the pieces are spaced out nicely.  Indenting all the extends/withs by two spaces works also, as long as you leave the opening brace on its own line so that you can see quickly where the code begins.  Jumping from one type of indentation to another makes for difficult visual parsing.

class dontDoThis(someParameterWtihALongName)
  extends somethingOrOther[Int]
  with thisOrThat {
  def waitThisIsNotATrait(oops:Boolean) = true
}

class dontDoThisEither(
  onceUponATimeSomeoneWroteOverlyLongParameterNames,
  andSometimesYouGetStuffThisLongInJavaLibs) {
  val canIEasilySeeThatThisIsNotAnArgument = false
}

  --Rex

On Sun, Nov 22, 2009 at 12:18 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
I would vote for indented four spaces on long strings of arguments.  This distinguishes arguments and parent types from the actual class body.

Daniel

On Sun, Nov 22, 2009 at 10:22 AM, David Copeland <davetron5000@gmail.com> wrote:
Good point.  This is the logical conclusion of that:

http://github.com/davetron5000/scala-style/commit/f283db1ec5141bb7b0675ae6dc8c7256ef4e944d

however, it is really nasty looking (esp. w/out syntax highlighting).

Nevertheless, I'm not sure how to make it look better; it's just an ugly class.

A related question is the ordering of classes being extended; it seems logical to me to have the first class extended be the most clear "superclass", and the remaining with less and less related "mixins", but I'm not sure if this would always be the case....

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



On Sun, Nov 22, 2009 at 10:30 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
We might want to consider what happens when we have traits and classes on there too :-)



On Sun, Nov 22, 2009 at 3:15 PM, David Copeland <davetron5000@gmail.com> wrote:
Here's a stab at this:

http://github.com/davetron5000/scala-style/commit/c313a4bb2350e73913838dc300e392f5d833afa4

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



On Sun, Nov 22, 2009 at 9:56 AM, Daniel Spiewak <djspiewak@gmail.com> wrote:
I use the second form, but only when the number of arguments starts to extend beyond the width of my editor.  A more general guideline might be 100-120 characters in width, at which point the second style should be used.

The first style though is by far the most common case.  I think it's pretty bad practice to have that many constructor arguments in the first place.  Yes, there are cases where it is unavoidable, but by-and-large, you can do without.  Yes, I realize this is another "if/then" guideline, but I think it's well-worth it to try and keep most constructors on a single line.  Think about it, which of these is more readable?

class Test(val str: String) {
  def process = str + "foo"
}

class Test(
    val str: String) {
  def process = str + "foo"
}

The latter just pointlessly wastes a whole line worth of vertical space.  My vote: keep things on one line unless it starts to become unreadable (circa 100-120 chars), at which point, reformat the arguments into the "one per line" approach.

Daniel

On Sun, Nov 22, 2009 at 8:52 AM, David Copeland <davec@naildrivin5.com> wrote:
I think the second form is probably useful for classes with "too many" constructor args to fit on one line.  I guess "too many" could be defined as "takes longer than 70 characters" or "more than 3 parameters"?

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



On Sun, Nov 22, 2009 at 6:03 AM, Colin Howe <colinthehowe@googlemail.com> wrote:
Hi,

I've just noticed something that is missing from the scala style guide...

class Employee(val forename : String, val surname : String) {
  // ...
}

What are people's thoughts on this?

Personally, I prefer:

class Employee(
  val forename : String,
  val surname : String) {

  // ...
}

But, I still don't like it, just the least ugly syntax I've found so far :-)


Cheers,
Colin









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