DEV Community

Cover image for Intro To Functional Programming

Intro To Functional Programming

Milecia on October 15, 2019

Functional programming has been around for a while, but it's really starting to gain traction. It's a different approach to writing programs instea...
Collapse
 
juanfrank77 profile image
Juan F Gonzalez

This is a really great intro post to Functional Programming for people that are not used to it. It's also good that you used JS to explain it!
Functional programming is love and is life :D (coincidentally I once gave a talk with that title hahaha)

Collapse
 
siy profile image
Sergiy Yevtushenko

Starting from very first example you're mixed up object oriented and imperative programming. In fact there is no contradiction between object oriented and functional programming. Object oriented code can (and often do) use immutable data and pure functions. Object oriented code is more about how code and data organized together than about how code does things.

Collapse
 
iioaia profile image
iioaia

Is there such a thing as hybrid programming? Mixing both styles?

Collapse
 
vkelman profile image
Vladimir Kelman

When we programs in a modern C# or Kotlin we definitely mix functional and object-oriented programming. Scala was specifically designed to combine improved Java OOP with functional programming. JavaScript supports both... well, in JavaScript there is virtually no limits to what you can do.

Collapse
 
mburszley profile image
Maximilian Burszley

In any modern language there's no limits to what you can do.. JavaScript only has the support it does because it's there by default in web browsers. I, for one, am happy with our upcoming wasm overlords.

Thread Thread
 
vkelman profile image
Vladimir Kelman

Yes, WASM looks intriguing.

Collapse
 
stemmlerjs profile image
Khalil Stemmler

Uncle Bob says that the best software is a mix of all paradigms.

Structured programming - this is the basis of our algorithms.
Functional programming - how we push data to the boundaries of our applications and elegantly handle program flow.
Object-Oriented programming - how we define relationships between modules / how we cross architectural boundaries with polymorphism and plugins

Collapse
 
eaich profile image
Eddie

This is a really great summary. Quick question - couldn't objects defined using const still be mutable in a sense if I set a property on it?

Collapse
 
mburszley profile image
Maximilian Burszley

Correct. const in javascript does not mean immutable. It means the identifier can't be re-assigned.

Collapse
 
peterwitham profile image
Peter Witham

This might be the first time that it's made sense in a way that I can actually apply it to real-World scenarios.

Thanks Milecia

Collapse
 
jcshartz profile image
jcshartz

I'd love to see some functional programming patterns that deal with async operations, such as networking.

Collapse
 
kvsm profile image
Kevin Smith 🏴󠁧󠁢󠁳󠁣󠁴󠁿 • Edited

The short answer to this is that those type of operations (side effects), are dealt with by pushing them out to the edges of your code, by extracting them from the core logic of your program and evaluating them lazily.

An example of this is making a network request for some data which you then have to do some processing on. We can return something like a Promise which allows us to define the processing we want to do with whatever the result might be, and only execute that Promise at a later time.

Unfortunately the short answer still leaves a lot of questions which are best answered by a more in depth look at topics like Option/Maybe types. I've just started a series on basic functional topics and hopefully I'll eventually get to covering more advanced topics too.

Collapse
 
siy profile image
Sergiy Yevtushenko

Promises, probably, one of the most convenient patterns for async code. And they are FP as well.

Collapse
 
drmandible profile image
DrMandible

I'm still reading but this tripped me up: "shift more to figuring how what things are." I think you a word.

Collapse
 
flippedcoding profile image
Milecia

Oof... I thought I caught everything, but definitely missed this. Thanks! I'll update that.

Collapse
 
drmandible profile image
DrMandible

Great article and super informative! Thanks for posting!

Do you use functional programming whenever possible? Or is there a situation where you might choose OOP even though you could've used functional?

Thread Thread
 
mburszley profile image
Maximilian Burszley

Pick the one that can be easily tested and is most easily understood when you read it.

Collapse
 
motilola profile image
Ola

Thanks Milecia. I always look forward to your posts!

Collapse
 
flippedcoding profile image
Milecia

Thank you!

Collapse
 
tombohub profile image
tombohub

how to create validated value objects and invariants in functional programming?