7 Requirements as Types
In this chapter you will learn how to
- model your immutable data to minimize errors
- model your requirements as immutable data
- find problems in requirements using the compiler
- make sure your logic is always executed on valid data
Designing something just powerful enough is an art.
— Barbara Liskov
7.1 Modeling data to minimize programmer’s mistakes
In this chapter we will change the way we model data in our applications. We will use more types to minimize potential programmer’s mistakes. We will learn techniques which enhance and maintainability of our codebase. 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 artists catalogue
As always, we’ll start with a potential real-world application. We will implement a music artists 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)