Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala coding styles and conventions?

Tags:

scala

I think Scala goes too far from simplicity, like its syntax. For example Martin Odersky wrote the method in his book :

def calculate(s: String): Int =
  if (cache.contains(s))
    cache(s)
  else {
    val acc = new ChecksumAccumulator
    for (c <- s)
      acc.add(c.toByte)
    val cs = acc.checksum()
    cache += (s -> cs)
    cs
  }

If the methods grows, it becomes very painful to read the code, I can't match curly braces, can't fold the method in IDE. Is there any Scala coding conventions out there? I feel it's too flexible to express a simple method:

def add(b: Byte): Unit = {
  sum += b
}

def add(b: Byte): Unit = sum += b

def add(b: Byte) { sum += b }
like image 233
Sawyer Avatar asked Sep 15 '10 14:09

Sawyer


People also ask

Which programming style is followed by Scala?

We mostly follow Java's and Scala's standard naming conventions. Classes, traits, objects should follow Java class convention, i.e. PascalCase style. Packages should follow Java package naming conventions, i.e. all-lowercase ASCII letters. Methods/functions should be named in camelCase style.

Does Scala follow camel case?

Naming Conventions:- Scala uses “camel case” naming. That is, each word is capitalized, except possibly the first word. Scala prefer to not to use the Underscore in names because Scala has different definition of underscore.

Does Scala need indentation?

For Comprehensions That's because the Scala 2 version also included some proper aesthetics. Without braces, those indents are now mandatory.


3 Answers

"If the method grows it becomes very painful to read the code". I think part of the answer is that methods should not grow. The functional programing style is to have many small methods.The calculate method is already on the large side.

To answer the more general questions about style guides for Scala programing: Here's a representative example.

like image 129
Martin Odersky Avatar answered Oct 13 '22 00:10

Martin Odersky


Here is a link to the Scala Style Guide.


The Curly Braces section says:

Curly-Braces:

Curly-braces should be omitted in cases where the control structure represents a pure- functional operation and all branches of the control structure (relevant to if/else) are single-line expressions. Remember the following guidelines:

  • if - Omit braces if you have an else clause. Otherwise, surround the contents with curly braces even if the contents are only a single line.

  • while - Never omit braces (while cannot be used in a pure-functional manner).

  • for - Omit braces if you have a yield clause. Otherwise, surround the contents with curly-braces, even if the contents are only a single line.

  • case - Omit braces if the case expression ts on a single line. Otherwise, use curly braces for clarity (even though they are not required by the parser).

    val news = if (foo)
      goodNews()
    else
      badNews()
    
    if (foo) {
      println("foo was true")
    }
    
    news match {
      case "good" => println("Good news!")
      case "bad" => println("Bad news!")
    }
    

I wish people followed this style guide :(


Please note that I don't agree with "Omit braces if if has an else clause" part. I'd much prefer to see the code like this:

def calculate(s: String): Int = {
  if (cache.contains(s)) {
    cache(s)
  } else {
    val acc = new ChecksumAccumulator
    for (c <- s) {
      acc.add(c.toByte)
    }
    val cs = acc.checksum()
    cache += (s -> cs)
    cs
  }
}
like image 39
missingfaktor Avatar answered Oct 13 '22 00:10

missingfaktor


The official guide is at https://docs.scala-lang.org/style/ (adopted from now removed http://davetron5000.github.io/scala-style/index.html, see Web Archive).

like image 29
Alexey Romanov Avatar answered Oct 12 '22 22:10

Alexey Romanov