Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Advantages of functional programming  RSS feed

 
Carlos Solano
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I like to know the advantages of functional programming.


Thank you.
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 103
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Carlos,

The main advantage of functional programming is the fact that programs are deterministic, meaning that for the same input, they will always produce the same output. Because of this, you can reason about programs and prove them to be correct, rather than trying to prove them incorrect through intensive testing. In imperative programming, we generally test programs, trying to make them fail. If we can't succeed, we assume they are correct, which is obviously... incorrect!

To achieve this goal, functional programs are written by composing pure functions, meaning functions that have no observable effects beside returning a value. They do no mutate any data outside their body, nor do they mutate their argument. Furthermore, they do not depend upon external data that could change during their execution. (Depending upon immutable external data is possible. That data is then considered part of the argument.)

Another particularity of functional programing is that abstraction is generally pushed to the limit. For example, functional programs have no loops inside which things are done. They generally abstract the concept of looping and then compose this concept with functions.

In order to be able to reason about programs, functional programs are composed of expressions, not statements. The difference is like the difference between the if..else structure and the ternary operator ?: The ternary operator is supposed to return a value. The if..else structure does not return anything by itself (although it is possible to use if..else functionally, as well as to make ?: have side effects!)

Functional programs do not throw exceptions because exceptions do not compose (at least not in the same way as successful results compose). In case of error, they return the error as their return value.

Functional programs generally use immutable persistent data structures using data sharing, which is much safer than in place mutation. Some believe that it is at the cost of speed, which is generally false. It is sometimes slower, and sometimes faster (and even much faster). It is important however to choose the right structure for the given task.

All these elements (and more) make functional programs more reliable. Some argue that it is at the coast of readability, but this is mainly a question of experience. Functional programs may be very difficult to understand for imperative programmers. But after all, some imperative programs are very difficult to understand for imperative programmers, sometimes even for the programmer who wrote them! Functional programing is not complicated, unless you don't know functional programing. But I guess we could say the same for imperative progamming.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!