I've heard a lot of talk about using functional languages such as Haskell as of late. What are some of the big differences, pros and cons of functional programming vs. object-oriented programming?
The biggest difference is that Imperative programming is about Control flow while Functional programming is about Data flow. Another way to say it is that functional programming only uses expressions while in imperative programming both expressions and statements are used.
For example, in imperative programming variables and loops are common when handling state, while in functional programming the state is handled via parameter passing, which avoids side-effects and assignments.
Imperative pseudo-code for a function for calculate the sum of a list (the sum is kept in a variable):
Functional pseudo-code for the same function (the sum is passed as a parameter):
I recommend the presentation Taming Effects with Functional Programming by Simon Peyton-Jones for a good introduction to functional concepts.
Functional programming is based on a declarative model and has its roots from lambda calculus. It offers a lot of great concepts which can be borrowed from more imperative languages like C++ and C#.
Some examples include referential transparency, lambda functions, first class functions, lazy and eager evaluation, and immutability.
If for nothing else learning functional programming is useful for the concepts that it contains. It will change the way you do programming and think about programming. And I would guess that in the future functional programming will be just as important as object oriented programming has been.
To get started you can chose to use a pure functional language such as Haskell, or you can use a hybrid one like F#.
Most good universities will cover functional programming and if you go to school I'd highly suggest you take that course.
Well object oriented programming is nice because it allows you to model your complex problem into hierarchies so you can simplify the problem. But it becomes very hard when you start to consider multi threaded programming while using mutable objects. In such cases you need to use heavy use of synchronization objects and it's near impossible to perfect a large application.
That's where functional programming comes in. Because of things like immutability functional programming really simplifies multi threaded programs. It makes it almost trivially easy to parallelize something when you know that given input X to a function it will always output Y. Also you know that a variable (or value in functional programming) can't change mid use from another thread.
|show 3 more comments|
Functional programming and object-oriented programming are orthogonal to each other. You can have both in the same language. Examples: Scala, F#, OCaml etc.
Maybe you meant functional vs imperative, as Jonas suggested?
There is no real versus. They can be perfectly complementary. There are FP languages, which support OOP. But the communities differ in the way they handle modularity.
Users of FP languages tend to achieve modularity through mathematical laws. And prefer proofs to show compliance with their laws.
In imperative OOP users tend to capture the behaviour of the object in test-cases, which can be rerun if the object has changed and achieve in this way modularity.
It is just a small aspect, but I think it is worth mentioning.
protected by Telastyn Feb 19 at 18:42
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.