7 Requirements as types
In this chapter you will learn
- how to model your immutable data to minimize errors
- how to model your requirements as immutable data
- how to find problems in requirements using the compiler
- how to make sure your logic is always executed on valid data
—Barbara Liskov
In this chapter we will change the way we model data in our applications. We will use more types to minimize potential programmer mistakes. We will learn techniques that enhance the maintainability of our codebases. We will also make implementations smaller and less complicated! Too good to be true? Let’s see this in action using an example.
We focus a lot on maintainability in this book. As a reminder, we say that a given codebase is maintainable if it’s easy to change without introducing bugs.
Music artist catalogue
As always, we’ll start with a potential real-world application. We will implement a music artist catalogue that will help us find artists by genres, their locations, or years they were active. There will be lots of corner cases to handle, and the main focus will be on data modeling. We will be modeling artists. Each artist will have a name, a main genre, and an origin. Seems easy, right? We just need to define a product type.
case class Artist(name: String, genre: String, origin: String)
What problems do you see with this definition of an artist.