Advantages of Our Functional Approach
Learn about the advantages of using the functional approach in the application.
Let’s think about the advantages of this approach over our earlier naive version. Before doing that, though, we can discuss some possible improvements as an exercise. For example, our rework contains no tests. Because we focused on creating pure functions and eliminating side effects, adding those would be easy.
Issues with functional approach
There’s some duplication in the code. The higher-level functions in entrypoint.ts
and anticorruption.ts
are very similar for pets and wild animals. In a larger project, we might create a function that accepts other functions for a more specific behavior, such as creating the right domain object. This is similar to the Template Pattern from OOP, thanks to the versatility of higher-order functions. However, in a small project like this, such optimizations might be premature and work to our disadvantage. What if the access or validation patterns of wild animals and pets are currently similar by accident and will grow apart in the future? If that’s the case and we’ve already moved this seemingly generic functionality to a common section, this function might become a complicated mess with numerous conditionals.
simplistic routing presents another issue, but we’ve already discussed how to fix that. Finally, the injection of an environment variable for the table name is especially ...