Code Blocks [Edit]
Opening curly braces ({
) should be on the same line
… as the declaration they represent.
// right
def foo = {
...
}
Use braces and indented code block with if
, while
and for
.
// wrong
if (condition) log.info("something interesting happened")
// wrong
if (condition)
log.info("something interesting happened")
// wrong
if (condition)
log.info("something interesting happened")
else
log.info("nothing happened, really")
// right
if (condition) {
log.info("something interesting happened")
}
// right
if (condition) {
log.info("something interesting happened")
} else {
log.info("nothing happened, really")
}
One notable exception to this rule is when an if
is used as substitute for the ternary operator (?
) — which is not available in Scala — and when the entire expression fits in a single line,
// right
val level = if (state == "dev") debugLevel else warnLevel