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...
For further actions, you may consider blocking this person and/or reporting abuse
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)
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.
Is there such a thing as hybrid programming? Mixing both styles?
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.
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.
Yes, WASM looks intriguing.
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
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?
Correct.
const
in javascript does not mean immutable. It means the identifier can't be re-assigned.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
I'd love to see some functional programming patterns that deal with async operations, such as networking.
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.
Promises, probably, one of the most convenient patterns for async code. And they are FP as well.
I'm still reading but this tripped me up: "shift more to figuring how what things are." I think you a word.
Oof... I thought I caught everything, but definitely missed this. Thanks! I'll update that.
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?
Pick the one that can be easily tested and is most easily understood when you read it.
Thanks Milecia. I always look forward to your posts!
Thank you!
how to create validated value objects and invariants in functional programming?