Lately I’ve been looking for ways to make complicated Scala code easier to read. One way I have found to do this is with custom types. Custom types are especially useful when dealing with a complicated business domain.
Here’s a quick & contrived example of what I mean:
What’s the point here? GameHistory is just a wrapper around a Map? Why are we wrapping simple Scala primitives in excessive case classes?
The point here is to achieve simplicity and avoid complexity. I personally think that custom types are a way to achieve that - the reader can observe the Types in play and then not have to wonder about the concepts behind the primitives.
For example, let’s add some methods to our GameHistory, playing with Scala’s apply concept where a function/method is applied to a parameter value, and also lets extend the standard ++operator from the underlying Map class.
This can be dangerous however, and introduce too much complexity and convulsion. I have to remember to be careful and use this technique only when it actually is the simplest way to solve a problem