This is one of the few things which I discovered along the way as I was learning Scala. Always prefer lifting a function than destructuring.
For example, I have a function which returns the capital given the two letter country code.
This function returns an
Option[String] type. If the capital exists, it returns the capital as a
String contained in the
Option monad. If not, it returns a
I want to upper case the results returned. This was what I was doing in my early days.
1 2 3 4 5
Then I realised lifting the
Option type is a more idiomatic way.
It is not necessary to destructure the type only to put it back again. Look how short it is, and clear its intention is. It did exactly the same things the earlier code snippet did.
When you see a lot of destructuring using pattern matching in a Scala code, it is almost always a code smell. Always prefer lifting over destructuring.