Advantages of functional programming

There are some exciting and cool features in FP paradigms such as composition, pipelining, and higher order functions that help to avoid writing unfunctional code. Alternatively, at least later on, this helps translate a unfunctional program into a functional style towards an imperative one. Finally, now let's see how we can define the term functional programming from the computer science perspective. Functional programming is a common computer science concept in which computations and the building structure of the program are treated as if you are evaluating mathematical functions that support immutable data and avoid state change. In functional programming, each function has the same mapping or output for the same input argument values.

With the need for a complex software comes the need for good structured programs and software that are not difficult to write and are debuggable. We also need to write extendable code that will save us programming costs in the future and can contribute to easy writing and debugging of the code; even more modular software that is easy to extend and requires less programming efforts. Due to the latter contribution of functional programming, modularity, functional programming is considered as a great advantage for software development.

In functional programming, there is a basic building block in its structure called functions without side effects (or at least very few) in most of your code. Without side effects, the order of evaluation really doesn't matter. When it comes to programming languages views, there are methods to force a particular order. In some FP languages (for example, eager languages such as Scheme), which have no evaluation order on arguments, you could nest these expressions in their own lambda forms as follows:

((lambda (val1) 
((lambda (val2)
((lambda (val3) (/ (* val1 val2) val3))
expression3)) ; evaluated third
expression2)) ; evaluated second
expression1) ; evaluated first

In functional programming, writing mathematical functions in which the execution order doesn't matter usually makes your code more readable. Sometimes, one will argue that we need functions with side effects to be there as well. Actually, this is one of the major disadvantages of most functional programming languages since it's typically difficult to write functions that don't require any I/O; on the other hand, these function that requires I/O are difficult to implement in functional programming. From Figure 1, it can be seen that Scala is also a hybrid language that evolved by taking features from imperative languages such as Java and functional language such as Lisp.

But fortunately, here we are dealing with a mixed language in which object-oriented and functional programming paradigms are allowed and hence writing such functions that require I/O is quite easy. Functional programming also has major advantages over basic programming, such as comprehensions and caching.

One of the major advantages of functional programming is brevity because with functional programming you can write more compact and concise code. Also, concurrency is considered one of the major advantages, which is done more easily in functional programming. Therefore, functional languages such as Scala provide many other features and tools that encourage coders to make an entire paradigm shift to a more mathematical way of thinking.

Figure 1: Shows a conceptual view of using functional programming concepts

By narrowing the focus to only a small number of composable abstract concepts, such as functions, function composition, and abstract algebra, FP concept provides several advantages over other paradigms. For example:

  • Closer alignment to mathematical thinking: You tend to spell out your ideas in a format close to mathematical definitions rather than iterative programs.
  • No (or at least fewer) side effects: Your functions do not influence other functions, which is great for concurrency and parallelization, and also for debugging.
  • Fewer lines of code without sacrificing conceptual clarity: Lisp is more powerful than non-functional languages. Although it's true that you need to spend a greater proportion of your project thinking than writing, you will probably find that you are more productive eventually.

For these exciting features, functional programming achieves significant expressive power. For example, machine learning algorithms can take hundreds of lines of imperative code to implement yet they can be defined in just a handful of equations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.111.49