DEV Community

loading...

Discussion on: Functional VS Object-Oriented Programming

Collapse
robconery profile image
Rob Conery

Hi Kurt. Listen... this:

Object-Oriented Programming uses Classes, objects and methods to achieve it's lofty goals. Functional Programming on the other hand makes use of an army of pure functions and variables to to build a larger whole.

Needs some work. I encourage everyone to explore and to find out whatever thing scratches their itch but OO and FP are not differentiated by classes, methods, objects and pure functions. Yes, these are a few differences but I would encourage you to look deeper (or, if you already have, to describe those differences here) as there's a world of different thought in there!

Currying, for example, is mind-blowing as are functors and monads. For instance, dig this:

const nightOut = who => what => where => {
  return `Out with ${who} having fun ${what} at ${where}`;
};

That's a lambda in JavaScript which is "Curried" - instead of 3 arguments to a single function we've chained together 3 functions with a single argument. This allows reuse and composition:

const funTime = nightOut("Dancing")("wife")("Club 9");

The interesting thing is that I can capture/reuse these things functionally thus:

const dancing = nightOut("Dancing");
const dancingWithWife = dancing("wife");
const funTime = dancingWithWife("Club 9");

This is just the surface of functional programming and all the interesting things you can do with immutability and functional composition.

Monads (which you can arguably think of as chained promises) allow you to transform data within a transactional process. That's really the thrust of functional programming: data transformation that doesn't involve changing other data (side effects).

OK - I don't want to sound like a jerk! There's so much more here and I encourage people to look deeper :).

Collapse
stereobooster profile image
stereobooster

This sounds a bit misleading.

Yes, currying is nice, but there is nothing magical about it. You can achieve the same with objects ¯\_(ツ)_/¯.

all the interesting things you can do with immutability and functional composition.

Immutability is not something that adds capabilities to the system it rather restricts capabilities (you don't have the mutation). It is a trade-off. Developer agrees not to mutate things (as the result they can worry less about some edge cases), but the same time you need more memory (not necessary much more) and new tools (for example, lenses). There is a middle ground, like local mutation allowed and global mutations prohibited, for example in Pony language.

Collapse
krtb profile image
Kurt Bauer Author

Absolutely not, Rob, not a jerk at all! I always value opinions backed up with evidence, it helps us all learn and grow. Personally I know that I actually need to spend more time looking into currying and monads, and it seems like that information would have helped me to write this article. Thanks for providing so that others who read this can reference your comment and keep on falling into the beautiful rabbit hole that is coding.