All control structures should be written with a space following thedefining keyword:

  1. // right!
  2. if (foo) bar else baz
  3. for (i <- 0 to 10) { ... }
  4. while (true) { println("Hello, World!") }
  5. // wrong!
  6. if(foo) bar else baz
  7. for(i <- 0 to 10) { ... }
  8. while(true) { println("Hello, World!") }

Curly-Braces

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

  • if - Omit braces if you have an else clause. Otherwise, surroundthe contents with curly braces even if the contents are only asingle line.
  • while - Never omit braces (while cannot be used in apure-functional manner).
  • for - Omit braces if you have a yield clause. Otherwise,surround the contents with curly-braces, even if the contents areonly a single line.
  • case - Always omit braces in case clauses.
  1. val news = if (foo)
  2. goodNews()
  3. else
  4. badNews()
  5. if (foo) {
  6. println("foo was true")
  7. }
  8. news match {
  9. case "good" => println("Good news!")
  10. case "bad" => println("Bad news!")
  11. }

Comprehensions

Scala has the ability to represent for-comprehensions with more thanone generator (usually, more than one <- symbol). In such cases, thereare two alternative syntaxes which may be used:

  1. // wrong!
  2. for (x <- board.rows; y <- board.files)
  3. yield (x, y)
  4. // right!
  5. for {
  6. x <- board.rows
  7. y <- board.files
  8. } yield (x, y)

While the latter style is more verbose, it is generally consideredeasier to read and more “scalable” (meaning that it does not becomeobfuscated as the complexity of the comprehension increases). You shouldprefer this form for all for-comprehensions of more than onegenerator. Comprehensions with only a single generator (e.g.for (i <- 0 to 10) yield i) should use the first form (parenthesesrather than curly braces).

The exceptions to this rule are for-comprehensions which lack ayield clause. In such cases, the construct is actually a loop ratherthan a functional comprehension and it is usually more readable tostring the generators together between parentheses rather than using thesyntactically-confusing } { construct:

  1. // wrong!
  2. for {
  3. x <- board.rows
  4. y <- board.files
  5. } {
  6. printf("(%d, %d)", x, y)
  7. }
  8. // right!
  9. for (x <- board.rows; y <- board.files) {
  10. printf("(%d, %d)", x, y)
  11. }

Finally, for comprehensions are preferred to chained calls to map,flatMap, and filter, as this can get difficult to read (this is oneof the purposes of the enhanced for comprehension).

Trivial Conditionals

There are certain situations where it is useful to create a shortif/else expression for nested use within a larger expression. InJava, this sort of case would traditionally be handled by the ternaryoperator (?/:), a syntactic device which Scala lacks. In thesesituations (and really any time you have a extremely brief if/elseexpression) it is permissible to place the “then” and “else” branches onthe same line as the if and else keywords:

  1. val res = if (foo) bar else baz

The key here is that readability is not hindered by moving both branchesinline with the if/else. Note that this style should never be usedwith imperative if expressions nor should curly braces be employed.