Curly-braces should be omitted in cases where the control structurerepresents a pure-functional operation and all branches of the controlstructure (relevant to /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.
  • for - Omit braces if you have a yield clause. Otherwise,surround the contents with curly-braces, even if the contents areonly a single line.

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).

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 comprehension).

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