{"id":10877705,"date":"2022-03-25T14:11:21","date_gmt":"2022-03-25T14:11:21","guid":{"rendered":"https:\/\/wpml.org\/?p=10877705"},"modified":"2023-05-25T08:18:12","modified_gmt":"2023-05-25T08:18:12","slug":"why-we-made-the-switch-to-functional-programming-for-wpml","status":"publish","type":"post","link":"https:\/\/wpml.org\/de\/announcements\/2022\/03\/why-we-made-the-switch-to-functional-programming-for-wpml\/","title":{"rendered":"Why We Made the Switch to Functional Programming for WPML"},"content":{"rendered":"\n<p class=\"lead\">Functional programming is a hot topic amongst developers nowadays, and not without reason. <a href=\"https:\/\/wpml.org\/changelog\/2021\/10\/wpml-4-5-translate-all-of-your-sites-content-with-one-click\/\">Version 4.5 of WPML<\/a> was a large-scale project which introduced the option of automatically translating your website. For us, it was also the right moment to make the switch to functional programming. Here&#8217;s what we learned in the process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Functional Programming?<\/h2>\n\n\n\n<p>You\u2019ve probably heard that functional programming makes your code more readable and is easier to test. But to fully grasp functional programming, it\u2019s important to understand the related concepts of it.<\/p>\n\n\n\n<p>To start with, functional programming is a programming paradigm where the emphasis is on using functions to construct software and composing functions together to build more complex functions. Functional programming makes the code more declarative in nature. You express what you want rather than having a sequence of statements which update the program state.<\/p>\n\n\n\n<p>It\u2019s important to take note of the functions it uses &#8211; specifically pure functions. Pure functions receive data and return data, but don\u2019t have any other side effects. This makes the functions very easy to test because if you give the same parameters, you get the same result.&nbsp;<\/p>\n\n\n\n<p>Simply put, in functional programming the output depends on the input.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Functional Programming?<\/h2>\n\n\n\n<p>WPML is a large and complex project. Up until recently, we\u2019ve used object-oriented programming (OOP) to try to manage this complexity. One of the main challenges is writing test code (Unit tests) for all of our projects. When writing testable code, we need to ensure any changes we make don\u2019t break existing code.<\/p>\n\n\n\n<p>Unit testing OOP code can only take you so far. Code dependencies need to be mocked so classes can be tested independently. As a result, the tests end up being large and complex.<\/p>\n\n\n\n<p>The main focus of functional programming is said to be \u201cWhat to solve\u201d. In contrast, object-oriented programming takes on a \u201cHow to solve\u201d approach. With this in mind, it seemed like functional programming could help us overcome some of our challenges.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What We Did<\/h2>\n\n\n\n<p>When we talk about functional programming, PHP is not the first language that comes to mind &#8211; although it does allow us to introduce a functional style. For example, hooks and filters that allow WordPress to be extended use functions to override WordPress functionality.<\/p>\n\n\n\n<p>Unfortunately, there\u2019s no standard library for using a more functional paradigm in PHP. We reviewed many libraries but none had exactly what we needed to write expressive code in a functional style. So instead, we decided to <a href=\"https:\/\/git.onthegosystems.com\/wpml-packages\/fp\">write our own library<\/a>. We drew inspiration from different sources, but many from Javascript libraries, which we found were more mature.<\/p>\n\n\n\n<p>Functional programming has a different approach than other paradigms, so switching to a functional programming paradigm is actually a big step. It requires you to change the way you think about writing code and solve problems. It takes a lot of practice to write expressive code that is clear and concise.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Did It Work?<\/h2>\n\n\n\n<p>We definitely think it did. Functional programming has a number of features that work to our advantage:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The code structure is better&nbsp;<\/li>\n\n\n\n<li>&nbsp;The code is more expressive and concise<\/li>\n\n\n\n<li>Code duplication is reduced<\/li>\n\n\n\n<li>Functions are smaller and therefore easier to test and reuse<\/li>\n\n\n\n<li>Functions can be composed together<\/li>\n\n\n\n<li>Less mocking in tests<\/li>\n\n\n\n<li>Reusable mocking libraries were developed<\/li>\n<\/ul>\n\n\n\n<p>But of course, nothing is perfect. While functional programming is more concise and predictable, it does have disadvantages, especially if you\u2019re unfamiliar with it:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It\u2019s harder to debug<\/li>\n\n\n\n<li>It\u2019s harder to change &#8211; although this can also be seen as an advantage, because it requires you to consider changes rather than introducing quick changes<\/li>\n\n\n\n<li>The learning curve is steep<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Don\u2019t Be Afraid To Try<\/h2>\n\n\n\n<p>It goes without saying that the popularity of functional programming is more than just a trend. Because of its nature, functional programming is great for tasks such as machine learning and data analysis. As a result, companies of all sizes have started to show interest in, or even begin their transition to functional programming. This doesn\u2019t mean that you should say goodbye to object-oriented forever, but it\u2019s worth giving functional programming a try. Even if you never end up using it, chances are you\u2019ll up your game as a developer.&nbsp;<\/p>\n\n\n\n<p>If you\u2019re interested in learning more, we suggest starting with the <a href=\"https:\/\/medium.com\/javascript-scene\/composing-software-the-book-f31c77fc3ddc\">Composing Software blog post series<\/a>.<\/p>\n\n\n\n<p>Once you\u2019re ready to dive deeper, take a look at our <a href=\"https:\/\/git.onthegosystems.com\/wpml-packages\/fp\">FP project repo<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Do You Live and Breathe Code? Join Us.<\/h2>\n\n\n\n<p>We\u2019re always looking for exceptional developers to join our team. If that sounds like you, don\u2019t wait any longer &#8211; <a href=\"https:\/\/onthegosystems.com\/jobs\/\">apply today<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Functional programming is a hot topic amongst developers nowadays, and not without reason. Version 4.5 of WPML was a large-scale project which introduced the option of automatically translating your website. For us, it was also the right moment to make the switch to functional programming. Here&#8217;s what we learned in the process. What is Functional [&hellip;]<\/p>\n","protected":false},"author":538393,"featured_media":10877751,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"ep_exclude_from_search":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-10877705","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-announcements"],"_links":{"self":[{"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/posts\/10877705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/users\/538393"}],"replies":[{"embeddable":true,"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/comments?post=10877705"}],"version-history":[{"count":5,"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/posts\/10877705\/revisions"}],"predecessor-version":[{"id":13713497,"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/posts\/10877705\/revisions\/13713497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/media\/10877751"}],"wp:attachment":[{"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/media?parent=10877705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/categories?post=10877705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpml.org\/de\/wp-json\/wp\/v2\/tags?post=10877705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}