Tag: Option

Keep your code clean with algebraic data types (ADTs)

Recently, Daniel Westheide wrote an interesting post about the abuse of the Option type in Scala. You can find it here. I couldn’t agree more with Daniel.

This short story is another example that demonstrates how using Option is not always the best option (pun intended).

I’m developing an advertising service for a customer using Scala. A simplified version of the Ad data structure is the following:

final case class Ad(
    headline: String, 
    description1: String, 
    description2: String
)

At some point they told me we need to support, by adding the headline2 field, two types of ad: standard and expanded. They said: “If headline, description1, and description2 are used, it is a standard ad. If headline, headline2, and description1 are used it is an expanded one. Users won’t include headline2 when the ad is intended to be standard, and won’t include description2 when the ad is intended to be expanded.”

Read more...