{"id":8,"date":"2018-02-10T17:35:10","date_gmt":"2018-02-10T17:35:10","guid":{"rendered":"http:\/\/efelti.com\/tech\/?p=8"},"modified":"2020-11-11T21:56:35","modified_gmt":"2020-11-11T21:56:35","slug":"introduction-of-functional-programming-concept-and-benefits","status":"publish","type":"post","link":"https:\/\/efelti.com\/tech\/?p=8","title":{"rendered":"Introduction of Functional Programming Concept and Benefits"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Background<\/h2>\n\n\n\n<p>Functional Programming has become a hot topic. Either developers talk about testable codes, modularization, lazy loading, or multi-threading \/ parallelization, functional programming often becomes a topic into the discussions. What is actually functional programming?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Functional Programming&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\"><img decoding=\"async\" src=\"https:\/\/3.bp.blogspot.com\/-yWASPfKD7ls\/WmrFxDa00VI\/AAAAAAAAPQ0\/RHrtBsIkHqAQVzvCHFNXbsZd_FjbUsm3wCPcBGAYYCw\/s1600\/function_machine.svg.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p><strong>Functional Programming<\/strong>&nbsp;(FP) is a&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">programming paradigm<\/a>, mindset, or style of building code in IT world which treats computation or programs as mathematical functions, the so called&nbsp;<strong>Pure Functions<\/strong>&nbsp;in computer science.<br>A pure function itself is a function where the output only depends on the input. Shortly explain: we treat a code or a program as a function f(x). The input is \u201cx\u201d, \u201cf\u201d is the function or how the code behaves, and \u201cy = f(x)\u201d is the output. The result \/ output \u201cy\u201d only depends on \u201cx\u201d and not anything else. Whenever the function is executed with same input \u201cx\u201d, it always produces the same output \u201cy\u201d, it does not matter how many times we execute it, does not matter when we execute it.<br>By the implication of that principle, it should avoid&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">mutability<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">side effects<\/a>, e.g. global or shared states, or mutable input since those could change the result \u201cy\u201d although we have the same initial input \u201cx\u201d by running the function \u201cf\u201d.<br>That\u2019s basically one of the most basic principle of functional programming. There are still a lot of other concepts influence the evolvement of functional programming paradigm. In this article I\u2019ll try to focus not at all but on that principle and see the benefit of it if we consider it and adopt the concept to our development.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Procedural Programming is NOT Functional Programming<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">Procedural programming<\/a>&nbsp;also encapsulates codes to the so called procedures which is also often called functions, methods, or routines. However a function as a procedure is not always the same pure function introduced by functional programming since an encapsulations of codes to a function (procedure which is introduced by procedural programming) still can contain side-effect and mutability.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\"><img decoding=\"async\" src=\"https:\/\/4.bp.blogspot.com\/-EODtQWPGw5g\/Wn7Q3HNqRSI\/AAAAAAAAPSg\/9vLwEKblMQYJInfawMXGFxQhPcJAUVg-wCLcBGAs\/s640\/pure%2Bfunction%2Bvs%2Bprocedure.jpeg\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of Functional Programming<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Testable Code (Code Which is Designed for Test Automation)<\/h3>\n\n\n\n<p>As a mathematical axiom, we can treat a capsulated program as a function:<br><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\"><\/a><\/p>\n\n\n\n<p>So for a complete requirement of that program, we can treat the requirement as a list of tupels Input and Output, mathematically:<br><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\"><\/a><br>Since in functional programming we treat a program as a function where its output \u201cO\u201d&nbsp;only depends on input \u201cI\u201d, we can create test cases by listing down the tupels (I, O) of test cases. Of course the test completeness will still depends on the&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">testability hierarchy<\/a>&nbsp;of the program but since any input \u201cI\u201d can be passed as the input parameters of the function, we can have the best completeness of the test to have the best code coverage.<br>This is not always possible if we have side effects which comes from external entities or other layer, since in that case we often do not have controls to modify those side-effects value.<br>By having it passed through input parameter instead of side effects as a pure function, we could easily mock the parameter, even to mock behaviors as parameters, by the so called&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">high order functions<\/a>&nbsp;(using functions as a parameter).<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>By encapsulating code as a pure function (or a function which behaves like a pure function) we can have a best collection set of Input Output Tupels to have an automated test suite which have best test coverage&nbsp;<\/em><\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Modularization and Black-Box (Code with Short Methods and Classes)<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\"><img decoding=\"async\" src=\"https:\/\/3.bp.blogspot.com\/-sIddbe2LddI\/WmrDF7ey77I\/AAAAAAAAPQQ\/0gNNb85UbM8iN8eMwn8X1eOwgIg-hPP-gCEwYBhgL\/s320\/black-box.png\" alt=\"Function as Black-Box\" title=\"Function as Black-Box\"\/><\/a><\/figure>\n\n\n\n<p>Since functional programming uses the concept of pure functions, the function \/ the part of the code which is encapsulated as a pure function can be easily moved to other place as a black box for re-usability or for the purpose of code readability without any huge effort of refactoring like in case we use a lot of side effects in the codes.<br>There are several ways to do pure function in&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">OOP (Object Oriented Programming)<\/a>. An example of it is by using the static method concept to mimic pure function concept. In that approach, we extract method easily to other class, keep classes always short and do what it has to do.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><em>Do you want to have short classes for your whole codes? Have a look to adoption of functional programming in OOP to encapsulate codes as black-boxes.<\/em><\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Lazy loading<\/h3>\n\n\n\n<p>The concept of functional programming is by letting us pass anything as input parameter. We pass each input parameter which causes any effects of the outputs. It includes to pass behavior as parameter, in other words it let us pass a function as an input parameter of the main function (<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">high-order function<\/a>).<br>The function as an input parameter itself can be run freely inside the main function which take it as parameter. And this is actually the concept of&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">lazy loading<\/a>, by running or invoking the consumed function when we need it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Multi-Threading \/ Parallelization<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\"><img decoding=\"async\" src=\"https:\/\/2.bp.blogspot.com\/-NHv_hNuivLM\/WmrFbPeSCoI\/AAAAAAAAPQg\/B7t1c0ttfHEdwf28I9e9nClvnbDwb5-VgCLcBGAs\/s320\/parallelization.png\" alt=\"\"\/><\/a><\/figure>\n\n\n\n<p>Since functional programming minimize side-effects, avoid shared variables, and mutability in its concept which make the implementation thread-safe based on the purity of the function&#8217;s implementation, it becomes more natural for functional programming for parallelization, multi-threading, or multi-core processing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Functional Programming Features in Programming Languages<\/h2>\n\n\n\n<p>Functional Programming is a concept and paradigm, we can adopt it to any programming language. However how far or how complete we can adopt, it depends on the functional programming support for the particular programming language.<br>Here is a list of some most used interesting features we could check, if we want to start doing functional programming paradigm into the particular programming language.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Immutability of variables<\/strong>It is good to check how mutability and immutability are implemented for language implementation. For example in Java and C#, primitive variables are immutable and Object are by default mutable.<\/li><li><strong>Higher order function (first class function of programming language)<\/strong><br>Important support for functional programming in programming language is the higher order function. A language which support this by its prioritization is called&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">first class function<\/a>&nbsp;of programming language, to have possibilities to pass function as parameters, to return function as the output, or to have data value as a function.<\/li><li><strong>Anonymous Function and Lambda expression<\/strong><br>Higher order function are often implemented in programming languages as&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">Lambda expression<\/a>&nbsp;to pass an&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">anonymous function<\/a>&nbsp;as parameter. Checking that feature might be useful.<\/li><li><strong>Generic and dynamic typed<\/strong><br>For language which is&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">weakly typed<\/a>&nbsp;(not&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">strongly typed<\/a>) like JavaScript. Passing parameters to function is easier since every kind of types including function type can be done without additional effort. For language which is strongly typed like C# or Java or even TypeScript, another approach is to be taken. In C# or TypeScript dynamic type modifier is used to make it weakly typed (&#8220;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">dynamic<\/a>&#8221; in C# or &#8220;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">any<\/a>&#8221; in TypeScript). Or we could also use&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">Generic Programming<\/a>, to make the parameterization still strongly typed whenever we pass a function contract as a parameter.<\/li><li><strong>Interface functionality for OOP<\/strong><br>Sending a function as parameter also could be done by using&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">interface concept<\/a>&nbsp;as work around in case we do not have the feature to send a function directly as a parameter, e.g. working with Java 7 or prior version. Basically the function contract will be encapsulated within an interface. The input parameter contract will contain only the interface, and the real value to pass is the implementation class of that particular interface.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Adopting Functional Programming Paradigm<\/h2>\n\n\n\n<p>Besides the pure function concept the functional programming paradigm as I mentioned before has a lot of introduced other concepts to support the pureness.<br>The functional programming paradigm itself has been evolving from the&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">declarative programming paradigm<\/a>&nbsp;which uses the concept of declarations \/ expressions&nbsp;over the statements concept introduced by&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">imperative programming<\/a>. It puts more focus in structures instead of the execution orders. Not like in the imperative programming, the functional programming prefers to use&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">recursion structures<\/a>&nbsp;over&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">iteration loops<\/a>.<br>There are several levels how we could adopt functional programming in the development. The extreme one might be the&nbsp;<a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">purely functional programming<\/a>&nbsp;where it tries to adopt the full concept of functional programming and avoid side effects and mutability completely.<br>However, not all of the concepts are really 100%-ly dependent one to each other that avoids us to adopt one and try not to adopt the others. Also there are always benefits and drawbacks of each concept introduced by the functional programming. Putting everything together and try to do everything without knowing the reason behind it is not always useful, moreover it could even cause problems. Instead we should take what necessary based on what to solve, understand the concept well, and start simple.<br>For instance, even of the elegance of recursions implementation, implement everything as recursions does not always make sense since recursions could also cause memory problems. That\u2019s why people in many cases should even go for the iterations.<br>Implementing immutable parameters completely is also not always useful. For example, if your application is single threading and require high performance processing of large amount of data, putting effort to implement pure immutability by replacing mutable data structure which has O(1) algorithm complexity for its operations with the immutable one which has at least O(log n) algorithm complexity does not make sense.<br>However it might be useful to implement them as a set of functions which have clear definition of inputs and outputs without uncontrolled side-effects, so we would have them behaving as pure functions, and the codes would be more testable also support well for test automations.<br>However in another scenario in case if you are working for multi-core application. You might need to consider a lot about immutability and how you pass input parameters. And once we could ensure them, it becomes natural that our code fits well for that situation.<\/p>\n\n\n\n<p>By understanding the concept of side-effects we try to maintain them not to have dirty unpredictable side-effects and use it whenever necessary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>As a conclusion, I would say that functional programming concept and paradigm bring a lot of benefit in code development. Use it wisely to know where and when to adopt for each concept.<br>Functional programming helps us to build testable codes. If you are a fan of code with short classes and methods, you should not miss the functional programming. Combining functional programming (FP) with object oriented programming (OOP) paradigm makes your methods encapsulation more maintainable and much more easier for refactoring later.<br>I personally love functional programming a lot. Functional programming imposes disciplines which help us to create clean and modularized codes, to create testable codes for the support of test automations. I love to combine OOP and FP together embrace their powers and strengths together in code development.<\/p>\n\n\n\n<p>&#8211; By Efelti<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">Understanding Immutability and Pure Functions (for OOP)<\/a><\/li><li><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">Object Oriented Programming (OOP) &amp; Functional Programming -What are they &amp; the Pros and Cons<\/a><\/li><li><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">The Clean Code Blog &#8211; OO vs FP (by Robert C. Martin (Uncle Bob))<\/a><\/li><li><a href=\"https:\/\/www.blogger.com\/blog\/post\/edit\/8516031187236147880\/1486047217174825048#\">DDD &#8211; Is anemic domain model an antipattern? Shoud we be using rich domain models?<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Background Functional Programming has become a hot topic. Either developers talk about testable codes, modularization, lazy loading, or multi-threading \/ parallelization, functional programming often becomes a topic into the discussions. What is actually functional programming? What is Functional Programming&nbsp; Functional Programming&nbsp;(FP) is a&nbsp;programming paradigm, mindset, or style of building code in IT world which treats [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[2,3],"class_list":["post-8","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-clean-code","tag-clean-code","tag-functional-programming"],"_links":{"self":[{"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=\/wp\/v2\/posts\/8","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8"}],"version-history":[{"count":1,"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":10,"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=\/wp\/v2\/posts\/8\/revisions\/10"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=\/wp\/v2\/media\/9"}],"wp:attachment":[{"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/efelti.com\/tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}