What is Functional Programming - II

In previous article, we have discussed about basic concept of Functional Programming in brief. Continuing with same, today we shall discuss more about it.

Although there are many concept in Functional Programming which are different than Imperative programming style. However, following four points are very important and differentiating factors. These are:

  • True Functions
  • First Class Functions
  • High Order Functions
  • Focus on result, rather than on structure and procedure to do it

True functions, we have already discussed in previous article. These are the functions without any side effect and is a main building block for functional programming. These makes the parallel and concurrent programming easier.

First Class functions are the functions which can be presented anywhere in a program, like a variable. These can be passed as argument to any function, can be returned as value from function, can be assigned to a variable or can be stored in a data structure. These functions are the basic building block in functional programming. Java like languages does not have this feature. One possible similarity can be an anonymous class only, which can be passed as argument to any function in Java, but that is also quite limited in scope.

High Order functions are the functions which can take First Class functions as parameter or can return these as return value.

Combining above two type of function, we can write very dynamic programs. Imagine, a variable is passed to a function with another logic definition and hence added all possible dynamic behavior in same method. We can do this in Java also, by passing some commands or executors etc. But it is not that flexible. In Java, anyhow, we need to write a class even when we only need an behavior. Functional programming allows to define the functions directly without thinking about class structure and allow to pass these to other methods. A much easier and quick implementation.  

Fourth and important point is that functional programming advocates the focus on result, rather than on procedure of implementation or structure. Following this concept, Functional Programming languages provides a lot of abstract implementations for various repetitive tasks, like for iterating over collection, for parsing and reading any element from XML etc. In a language like Java, our focus used to be on good Object structure and the implementation logic. While iterating over a list, we plan whether we should use index based iteration or should use iterator. For XML parsing, we plan which parser we should use and so on. However, functional programming languages hide these implementation details from the programmer and provides high level syntax for performing such functions. Now if implementation is hidden from programmer, and is being taken over by language itself, it means that any programmer can implement the program much faster in functional programming. However, it means we are banking upon language capability to make a good decision or logic to perform these actions.

Doesn't this feel like loosing control to the language. Like in Java, we have control over every piece of code. We can open the source code and can see how List.add is working. If we are not happy with it, let us extend the list and write a custom logic to add the items in List. Hence a complete control what we want to do. So is that OK to loose all control to language and just use the high level language syntax. A big doubtful point  from a programmer working with Java kind of language, hard to digest. What if language is not perfect, or having bugs.

However, let us see the positive side of this approach. If programming language is providing the abstraction for such repetitive tasks and implementation logic, it means lesser coding for a feature implementation. Less coding also means lesser issues in implementation and more outcome, more focus on business code and hence the result. Further, in most of the cases, implementation done by language can be much better than ours. We may not be expert in all logic implementation. And as and when language implementation will improve its implementation logic for functions, our program will be benefited automatically. Any improvement in code means improvement for our application also without making any change.   Eventually languages will try to have better and better implementation with almost zero bugs and better performance. Hence if language implementation is that good, do we really need to have control on these basic logic implementation. Shouldn't we focus our energy on writing something more meaningful for business i.e. the business application functionality. Lets put all our efforts for business features.

For a Java programmer, the program structure given by functional programming languages is hard to digest. It is a altogether new structure with much different organization of objects, and classes etc. But if we think, it is just another way of writing the program and it is good if we are getting more work done by writing lesser piece of code. For example, if we are interacting with computer or machines, we always want to get more work done by machine in smallest possible instructions. If we need to send an email, we always like a single command like 'Send Email to ABC with following contents', rather than to write a complete low level program to interact with mail server. Functional languages are only that kind of step for programming approach. These can make programming much easier task if used properly. And eventually we can reach to a point when one line of instruction will perform all required functions, for which we used to write a whole 2000 line class in Java. Actually Java is also evolving in that direction with many new utilities, AOP, lambda expressions etc. So this will be the requirement of future and is worth to embrace.

I hope it will help. We shall keep discussing more concept about Functional Programming.