...

/

Indentation

Indentation

In this lesson, we will go over indentation in Scala.

Number of Spaces

Each level of indentation is two spaces, hence tabs are not used. This is a bit different from the indentation conventions in most programming languages. The Scala programming language strongly encourages two spaces as it allows a startling number of nested scopes and functions. If the first line of each inner scope is indented four spaces from the first line of its outer scope, the code will become wider and wider and move further away from the outer-most scope.

Press + to interact
// WRONG!
class Foo {
def fourspaces = {
val x = 4
..
}
}
Press + to interact
// RIGHT!
class Foo {
def twospaces = {
val x = 2
..
}
}

Line Wrapping

There are times when a single expression reaches a length where it becomes unreadable if kept confined to a single line. In Scala, that length is 80 characters. For such cases, the preferred approach is to split the expression up into multiple smaller expressions by assigning intermediate results to values. However, this is not always a practical solution.

When it is absolutely necessary to wrap an expression across more than one line, each successive line should be indented two spaces from the first. Also, remember that Scala requires each “wrap line” to either have an unclosed parenthetical or to end with an infix method in which the right parameter is not given.

Press + to interact
val result = 1 + 2 + 3 + 4 + 5 + 6 +
7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 +
15 + 16 + 17 + 18 + 19 + 20

Without this trailing method, Scala will infer a semi-colon at the end of a line which was intended to wrap, throwing off the compilation sometimes without even so much as a warning.

Methods with Numerous Arguments

When calling a method which takes numerous arguments (in the range of five or more), it is often necessary to wrap the method invocation onto multiple lines. In such cases, put each argument on a line by itself, indented two spaces from the current indent level.

Press + to interact
foo(
someVeryLongFieldName,
andAnotherVeryLongFieldName,
"this is a string",
3.1415)

This way, all parameters line up, but you don’t need to re-align them if you change the name of the method later on.

Great care should be taken to avoid these sorts of invocations well into the length of the line. More specifically, such an invocation should be avoided when each parameter would have to be indented more than 50 spaces to achieve alignment. In such cases, the invocation itself should be moved to the next line and indented two spaces.

Press + to interact
// WRONG!
val myLongFieldNameWithNoRealPoint = foo(someVeryLongFieldName,
andAnotherVeryLongFieldName,
"this is a string",
3.1415)
Press + to interact
// RIGHT!
val myLongFieldNameWithNoRealPoint =
foo(
someVeryLongFieldName,
andAnotherVeryLongFieldName,
"this is a string",
3.1415)

Better yet, just try to avoid any method which takes more than two or three parameters!