Finder Kata in Scala - Leaving Loops Behind ƛ

We have already seen how to approach this same refactoring kata in PHP. Basically, through small refactoring processes, we refined the code to provide it with domain semantics and greater readability. Today we bring you another solution approach based on the repository with the initial code we prepared for the dojo of Software Craftsmanship Barcelona. We encourage you to propose your solution via Pull Request, I’m sure interesting alternatives will emerge and we will learn more!

Initial State

In this approach, we will see how we start from the initial state of the code (take a deep breath before scrolling down 😅): https://gist.github.com/JavierCane/b1933d88c98aa8e4ee70ebfaecf3b2c0

Semantic Refactor

First, we move to a Paint and Polish™ iteration. We focus on class and variable names, and move some methods to foster cohesion and alleviate responsibilities from the main method. This way we introduce domain concepts that until now were not reflected in the code. In this way, we manage to feel more comfortable when working with the code as we get to know what it does. This is perfectly extrapolable to our day-to-day: First a bit of semantics to gauge the terrain and gather context, and then add the functionality/bugfix that motivated us to open that class 👌 (opportunistic refactoring, or the Boy Scout rule). Here is the branch of this first iteration if you want to do a PR with your solution based on it 🙂 https://gist.github.com/JavierCane/224879683189641b31a6f08b6c6fc749

Idiomatic Scala

Finally, what we have done is apply more idiomatic elements of Scala:

  • We replaced Java collections with Scala ones (There is not even one import! :P). With this, we gain methods that will be very useful, such as SeqLike#combinations.
  • Type Class Ordering: By using it, we can take the management of criteria for defining the best pair of people to isolated objects and promote the Open/Closed Principle of SOLID.
  • Option[PeoplePair] as a return type: This way we make explicit the possibility of not finding a pair of people under the given search criterion (None). This is one of the main gains compared to the previous iteration. The alternatives to this would be: returning null, throwing an exception, or as it was done before, returning an inconsistent instance of PeoplePair (with uninitialized class attributes).
  • We eliminated mutability thanks to the Scala collections (immutable by default), and the methods Iterator#map and GenTraversableOnce#reduce.

https://gist.github.com/JavierCane/1b70f6572277e5795fe0badee8759d64 If you liked this kata and see room for improvement, we would infinitely appreciate it if you shared it through a comment on the video, Twitter, or even if you feel motivated, a PR to the repo 🚀🦄 Soon we will continue delving into the topic of functional programming. We will soon publish the interview with Juan Manuel Serrano where we talk about this, higher kinded types, and much more 😬.

Pay according to your needs

lite (only monthly)

19 €
per month
  • Access to a subset of courses to lay the foundation for maintainable, scalable, and testable code
  • Company invoice
Popular

standard

24,92 €
Save 121
Annual payment of 299
per month
  • Full course catalog
  • Design and architecture challenges
  • Highlighted solutions videos for challenges
  • Receive job offers verified by Codely
  • Company invoice

premium

41,58 €
Save 89
Annual payment of 499
per month
  • All previous benefits
  • Early access to new courses
  • More benefits coming soon

We won’t increase the price while your subscription is active