I learn Scala for some time and can\'t clearly understand the usage of Option. It helps me to avoid null checks when I chain functions (according to docs). That\'s clear for
You should avoid null as much as possible in Scala. It really only exists for interoperability with Java. So, instead of null, use Option whenever it's possible that a function or method can return "no value", or when it's logically valid for a member variable to have "no value".
With regard to your Racer example: Is a Racer really valid if it doesn't have a car, currentRace and team? If not, then you shouldn't make those member variables options. Don't just make them options because it's theoretically possible to assign them to null; do it only if logically you consider it a valid Racer object if any of those member variables has no value.
In other words, it's best to pretend as if null doesn't exist. The use of null in Scala code is a code smell.
def foo(): Option[Result] = if (something) Some(new Result()) else None
Note that Option has many useful methods to work with.
val opt = foo()
// You can use pattern matching
opt match {
case Some(result) => println("The result is: " + result)
case None => println("There was no result!")
}
// Or use for example foreach
opt foreach { result => println("The result is: " + result) }
Also, if you want to program in the functional style, you should avoid mutable data as much as possible, which means: avoid the use of var, use val instead, use immutable collections and make your own classes immutable as much as possible.