alvin's blog

Scala best practice: How to use the Option/Some/None pattern

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.6, “Scala best practice: How to use the Option/Some/None pattern.”

Problem

For a variety of reasons, including removing null values from your code, you want to use what I call the Option/Some/None pattern. Or, if you’re interested in a problem (exception) that occurred while processing code, you may want to return Try/Success/Failure from a method instead of Option/Some/None.

Scala best practice: Eliminate null values from your code

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.5, “Scala best practice: Eliminate null values from your code.”

Problem

Tony Hoare, inventor of the null reference way back in 1965, refers to the creation of the null value as his “billion dollar mistake.” In keeping with modern best practices, you want to eliminate null values from your code.

Scala best practice: Use match expressions and pattern matching

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.4, “Scala best practice: Use match expressions and pattern matching.”

Problem

Match expressions (and pattern matching) are a major feature of the Scala programming language, and you want to see examples of the many ways to use them.

Solution

Match expressions (match/case statements) and pattern matching are a major feature of the Scala language. If you’re coming to Scala from Java, the most obvious uses are:

Scala best practice: Think “Expression-Oriented Programming”

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.3, “Scala best practice: Think "Expression-Oriented Programming".”

Problem

You’re used to writing statements in another programming language, and want to learn how to write expressions in Scala, and the benefits of the Expression-Oriented Programming (EOP) philosophy.

Scala programming best practice: Prefer immutable variables (values)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.2, “Scala programming best practice: Prefer immutable variables (values).”

Problem

You want to reduce the use of mutable objects and data structures in your code.

Solution

Begin with this simple philosophy, stated in the book, Programming in Scala:

“Prefer vals, immutable objects, and methods without side effects. Reach for them first.”

Scala best practices (idioms) (from the Scala Cookbook)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is the introduction to Chapter 20, _Idioms_ (Scala best practices).

When I first came to Scala from Java, I was happy with the small things, including eliminating a lot of ;, (), and {} characters, and writing more concise, Ruby-like code. These were nice little wins that made for “a better Java.”

Examples of how to use types in your Scala classes (generics, call-by-name parameters)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 19.8, “Examples of how to use types in your Scala classes.”

To put what you’ve learned in this chapter to use, let’s create two examples. First, you’ll create a “timer” that looks like a control structure and works like the Unix time com‐ mand. Second, you’ll create another control structure that works like the Scala 2.10 Try/ Success/Failure classes.

Syndicate content