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 }
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.
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.
For Comprehensions That's because the Scala 2 version also included some proper aesthetics. Without braces, those indents are now mandatory.
"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.
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
}
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With