Incomprehensible Finder Kata in PHP - Collections and Functional λ

Some time ago we saw how to tackle this kata in PHP and also how to solve it using Scala. Today we are going to demonstrate that with PHP, we can also do some functional things that make our code much more cohesive, immutable, cool, and simple! 😎 Remember that we have published the repository with the solutions we made at the Software Craftsmanship Barcelona event, Pull Requests are welcome! In this iteration we present collections (and we see when and why they are useful) and we will also take our first steps in functional programming using a library with a very original name: phunctional. We remind you that the kata is about refactoring existing code that has a test base but is incomprehensible. 🙂

Collections

Collections are a simple concept that we have been using all our lives without knowing what they were called 😇. A collection is nothing more than an array of the same type of object that serves to consolidate the logic that belongs to it. Imagine that we want to obtain all users born after 1950 from a list of users. Traditionally, we would have done it like this: https://gist.github.com/rgomezcasas/5ee2dd520762c5acd53c622e40459d67#file-someusage-php Maybe we are used to doing it this way all our lives, and we have never stopped to think about the problems this brings us. Some of the issues, among others, that may arise:

  • Code Duplication: If two points in our application need the same logic, we are copying and pasting it.
  • Obfuscation of Functionality: We cannot see at a glance what operations we are performing on the sets of users (lack of cohesion).
  • Ease of Introducing Errors in the Code: Imagine that someone accidentally adds a Product instead of a User. We would start to crash the moment a method that exists in User but not in Product is called (PHP FTW 🤘).
  • Responsibility Far from Its Domain: We have user management logic in external services instead of, just like we do with Value Objects, close to where the action happens (again, cohesion).

But all this has a very simple solution: The Use of Collections. Let’s remember, a collection is a class that contains an array of the same type of object, and it has the logic responsible for managing it inside. An example of a collection for users could be: https://gist.github.com/rgomezcasas/5ee2dd520762c5acd53c622e40459d67#file-users-php Here we see some new things, among them the CodelyTip of the day: Variadic Arguments. This (User …$users) is equivalent to "all the arguments you give me, put them in an array called $users", so when we execute new Users(new User(1999), new User(1800), new User(1337)), we are creating an array called $users that contains those users we created (notice that we passed the parameters as different arguments; we never sent an array). Another thing we see that can confuse us is the type hinting of User in front of the three dots; this means that all the arguments we receive must be of type User. With this, we ensure that our array always receives users and nothing else 💪. Thanks to this, we can now obtain all users applying a specific logic in a simpler, encapsulated, and more comfortable way.

Functional

If we go back to the previous example: https://gist.github.com/rgomezcasas/5ee2dd520762c5acd53c622e40459d67#file-users-php We can see that the method bornAfter has a cyclomatic complexity that is very high. In other words, we have an assignment inside a condition, which in turn is inside a loop. This makes our code not simple at all and harder to understand. One way to help improve this is to adopt one of the widely spread principles in functional programming: Immutability. What immutability promotes is to avoid modifying state (once you have assigned a value to a variable, you do not reassign it). In this case, the function that would suit us best is filter, a function that takes an array and returns another by applying a filter (the name reflects its function 😬). Using it, our code would look like: https://gist.github.com/rgomezcasas/5ee2dd520762c5acd53c622e40459d67#file-users-filter-php A much simpler code (not easy, especially if you’re not used to it), but one that reads much better. Additionally, adopting these types of practices can be useful if we want to transition to a functional programming language like Scala. If that’s the case, don’t miss the introduction to Scala video! If you enjoyed this kata and see room for improvement, we would greatly appreciate it if you could share your thoughts through a comment on the video, Twitter, or even, a PR to the repo 🚀🦄

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