What is Functional Programming

OO vs Fp

“No matter what language you work in, programming in a functional style provides benefit. you should do it whenever it is convenient, and you should think hard about the decision when it isn’t convenient.”
– John Carmack

When you apply the basic functional principles to your software, your resulting product will be more predictable, reliable and maintainable.

Clean Code: Chapter03 – Functions

  • Keep functions small
  • Don’t repeat yourself
  • One job
  • Avoid side-effects
  • 3 parameters max

OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts.

— Michael Feathers (@mfeathers) November 3, 2010

Both Object Orientation and Functional Programming try to solve the same problem, but take very different approaches. Object Orientation tries to manage the ever changing state of a system, while Functional Programming is trying to avoid that state altogether.

The main difference between Object Orientation and Functional Programming is how you compose your application.

Defining FP

Functional programming is a paradigm which concentrates on computing results rather than on performing actions.

-Dave Fancher

Central Themes of Functional Programming

1. Tamed Side Effects

Side effect in programming is anything that happens to the state of a system resulting of invoking a function.

Language Purity
Purely Functional Impure
Guarantees (except in certain limited cases) that functions do not have side effects No guarantee about functions and side effects
Functions always behave the same way regardless of the external state of the system
This allows parallel or asynchronous execution
2. Emphasizing Expressions

Everything produces a result

Statements vs Expressions

Statements Expressions
Define actions Produces Results
Executed for their side effects Execute for their results
var posAndNeg;
if(value > 0)
     posAndNeg = 'positive';
     posAndNeg = 'negative';
var posAndNeg = 
     value > 0
          ? 'positive'
          : 'negative';
  • Functional Programming is usually shorter.
  • We do not have unassigned variable
  • Fp tend to be more testable
3. Treat Functions as Data

Higher Order Functions

  • Functions which accept other Functions
  • Functions which return a Function

LINQ example

     .Where(x => x % 2 == 0)
     .OrderBy( x => x);


Pros Cons
Produces Results More Memory Allocation
Does not alter the original Source
Allows Asynchronous Runs



Express Yourself: Immutability  ->