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

Functional Programming Advantages  RSS feed

 
Divya Shiv
Ranch Hand
Posts: 93
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would like to know about the how Functional Programming  has bought in the implementation changes in terms of structural code base. How it  improves the application implementation compared to the normal programming language ?
 
Pierre-Yves Saumont
Author
Ranch Hand
Posts: 103
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am not sure to understand what you mean exactly by "in terms of structural code base", but I'll try to answer anyway. The most important feature of functional programming is the separation between effects and functions. Effects are the part which interact with the outside world, (which can be the world outside the application, or just outside the component you are considering). The rest of the code is composed of functions, which have no other interaction than receiving values as argument and returning a result. In pure functional programming, there are no effects, which does not mean that the programs do nothing observable, but that they return something which will make the effect observable once activated. You can see it as if functional programs were returning a kind of imperative programs that can be executed. Less pure functional programs simply separate functions and effect inside the same program. To allow a maximal separation, it is necessary to push abstraction as far as possible. For example, all control structures are abstracted into functions, so you never manipulate these control structures, such as loops, conditional instructions, try..catch blocks, etc., Of course, this is not completely true if you use a non functional friendly language like Java: You have to define your own abstractions, so you must use them once.)

The benefit form separating functions from effects are that the functional parts are deterministic, so they can very easily be fully  tested, and even often proven correct. This makes programs more reliable. It also make them faster to develop, since testing time is much reduced as well as the number of implementation bugs due to the abstraction of control structures. It also makes programs more readable because abstractions are named and can be recognized. This is not obvious for beginners, because it is difficult to recognize what you don't already know. Functional programming implies a different way of thinking. One must not see programs as a control flow, with instruction changing the state of some components, but as series of expressions that may be combined to create new expressions, until obtaining a single function taking the input data as its argument and returning the output.
 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the hardest things for me in switching my mindset to functional programming was the higher levels of abstraction that you have to get used to. 

It's one thing to think of functions simply as methods. It's a whole 'nother thing to think of functions as values, things you can pass and return from other functions. There's an almost Inception-like (the Leo DiCaprio movie) feel to it when your brain tries to wrap itself around that idea. And then you have tail recursion and list handling and head and tail and all, these are things that I struggled with for quite a bit when I learned Scala and functional programming from a Coursera.org class given by Martin Odersky himself.

I consider myself lucky though because I had some previous experience with method pointers, function macros, and JavaScript functions, things that are similar in nature to higher-order functions, lambda expressions, and closures. I have to admit that when Odersky started talking about lambda calculus, my eyes started to glaze over and my brain automatically started to shut down in self-defense, for fear of exploding.
 
salvin francis
Bartender
Posts: 1562
29
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I find it cryptic   (YMMV)
Maybe once I get accustomed to it, things may change. There are benefits, no doubt. But I find the code a bit harder to read. I was once equally afraid of Generics too.
 
Divya Shiv
Ranch Hand
Posts: 93
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pierre, Junilu and Salvin for briefing on implementation of Functional Programming.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!