What is functional programming, and how is it different to OOP?

601 viewsEngineeringOther

I’m a mid level Swift developer and I’ve only ever worked with the Object Orientated Programming paradigm.

I’ve heard about functional programming and I tried Googling what it is, but I couldn’t understand the resources that explained it. Also, reading functional code is even more confusing.

So I was hoping that someone who understood it could break it down in incredibly basic and simple terms, and the benefits/drawbacks that it has compared to OOP.

In: Engineering

8 Answers

Anonymous 0 Comments

At it’s core it’s functions as values, a variable can represent a function. Concept of “pure functions” just makes functional programming “work” (though I guess this has a math context also :), otherwise it would be a mess. You can distill pure functions into functions that cannot fail unexpectedly or have any kind of mutating state. In functional programming immutability is favoured by default.

Advantages:

* Data manipulation with immutable datastructures and functional style apis is incredibly nice.
* No mutating state / pure functions makes the code easier to read, no surprises
* Compile time safety can be maximized. It’s not simple to define what this means but javascript is an example of the other end of the spectrum, Java is in the middle and some functional languages at the opposite end of js.
* I like errors as values vs thrown errors (in general throwing errors is avoided)
* Passing functions around gives a lot of power
* My worst functional code is better than my worst imperative, it’s harder to write truly bad code. The same applies also for my best.
* Monads

Disadvantages:

* Trail/error learning in general is not as effective as in imperative languages. Example: if you know a little python you can slug it out on some problem, with functional programming you can get simply stuck.
* Being a type astronaut, you can create most amazingly complex abstractions for simple things
* Performance is sometimes worse vs imperative
* Monads

You are viewing 1 out of 8 answers, click here to view all answers.