When we write messages to friends or share our stories in posts on social media, we use commas, periods, and divide the text into paragraphs. This way, we make our text more pleasant to read and easier to understand.
Programming languages are similar to regular languages. Just like we do with regular text, programmers want to write code that looks good and is easy to read. Let's figure out how to do it in Scala!
Code style
Everyone has a different opinion on what looks good, so the Scala community has agreed on some common rules for writing code. How should I name variables? How many spaces should I use? How long should the function be? The answers to these questions, and many others, are called code style.
You can see the full list of rules with explanations in the official documentation of the Scala Style Guide, but now let's concentrate on a few of them that are really crucial.
Indentation and line wrapping
You should add an indent of 2 spaces for each incomplete action that we moved to the next line. This is not the way to write:
object Main {
def main(args: Array[String]): Unit = {
println("Hello, Scala!")
}
}
This is good:
object Main {
def main(args: Array[String]): Unit = {
println("Hello, Scala!")
}
}
Long lines of code can impair readability. Therefore, it is recommended to wrap lines with an indent:
val programErrorDescription: String =
"Something went wrong. Try again later."
If the calculation is too long, leave the action at the end of the line, and move the argument to the new line:
val sum = 1 + 2 + 3 + 4 + 5 + 6 +
7 + 8 + 9 + 10 + 11 + 12 + 13 +
14 + 15 + 16
If the function has many parameters, they can be placed one per line as follows:
def storePurchase(
prodact: String,
price: Double,
deliveryAddress: String
): Unit = {
// something
}Naming conventions
Variables and functions should be named in the lowerCamelCase style. That means that the words must be joined together, with the first word starting with a lowercase letter, and the rest of the words must start with an uppercase letter.
val lowerCammelCase = 1
def thisIsAlsoLowerCammelCase(withArgument: Int) = 2Declarations
Types are always specified using a colon, a space, and a type name:
val i: Int = 1
"something beautiful": String
def shouldGetAndReturnANumber(n: Int): Int = n + 1
In Scala, names can contain any characters, but try to make them meaningful. As you can see, in the following example, it is difficult to determine the context:
val d20: Double = 0.2
While here it is immediately clear that we want to use a variable to calculate the discount:
val blackFridayDefaultDiscount: Double = 0.2
If the function contains several actions, we typically write each action in a new line inside curly brackets:
def actions(): Int = {
val first: Int = someAction()
val second: Int = someAnotherAction()
first * second
}
In comparison, the same function written in one line would look illegible:
def actions(): Int = { val first: Int = someAction(); val second: Int = someAnotherAction(); first * second }Files
The names of files usually correspond to the names of their components. For example, our program from the previous topic should be stored in the file HelloScala.scala:
object HelloScala extends App {
println("Hello, Scala!")
}Comments
There are multi-line and single-line comments. In single-line comments, slash characters are separated from the text by a single space:
// One-line comments are usually short
val something = ""
If you want to place a comment inside a function, keep the alignment at the beginning of the line:
def sum10(a: Int) = {
println(a)
// There are also 2 spaces before the comment
a + 10
}
In multi-line comments, the text is written in the lines between /* and */. In this case, the alignment occurs with the help of asterisks and spaces:
/*
* This is a multi-line comment.
*
* You can use lines without text for greater readability,
* but remember about the alignment on the left.
*/
def something() = ""Conclusion
Great! Now you know the basics of Scala code style: how to indent your code for better readability, name variables and functions, declare types and name files, and place comments to your code. Writing in the correct style allows you to create readable code and be understood by other developers, so it is key to success.