- August 12, 2019
- Posted by: Bharat Patel
- Categories: ColdFusion, Lucee/Railo
Being a coder, I can say most of from us don’t want to walk much on a street. In the concern of cleaning written code that working perfectly fine, sometimes things are getting done with simple copy n paste that we did before. Simple and very easy, we normally do. There might be some solid reason to do like time not permit to find a better way, on-demand work, etc. I am not blaming anyone but it’s all my experience said, nobody likes to write the same code again and again and also would love to write less line of code to get the same amount of result, If so, It’s never too late to start doing the right thing. Let’s start digging, what the term functional programming is?
What is Functional Programming (FP)?
The term Functional Programming(FP) means to classify a program in a structured manner where you can avoid changing the state of variable scope or you can say mutable data whereas imperative programming changes the state of variables scope (we have to very careful) and has more side effects than FP.
In FP, you can give the style of the building structure of your program, FP returns immutable data so the scope doesn’t change accidentally. It also reduces the line of code, clean code and also says what it is. To explain, the higher-order functions such as filter, map, reduce give direct hint about the line of code. a filter says “I am filtering”, a map says “I am manipulating” likewise a reduce says “I am reducing data into one”. Pretty easy.
Basically, FP includes higher-order functions, pure functions, recursion and many more.
A higher-order function is a function that accepts a function as a parameter or returns a result.
Pure function meaning a function that doesn’t have any side effects and the return result is constant with respect to giving arguments.
Mainly, you heard higher-order functions with ES6 JavaScript, NodeJs and in some other langs. Now, those functions are supported in Lucee & ColdFusion as well. Probably, you need the nearly latest version of Lucee or ColdFusion since I’ve checked from which version it started supporting.
In CFML, we have different categories to hold values, like List, Array, Struct & Query. I’m happy to see all the categories have higher-order functions. For example, QueryReduce, ArrayFilter…
Let’s start implementing FP in the day-to-day and get some space to manage code in a better way.
Happy coding! 😊