DEV Community

loading...

From OO to FP

maartz profile image Maartz ・1 min read

Oyé, oyé !
Listen to me folks !

OOP is the worst thing that could have happen to programming...

Good.

Now that I’ve picked your attention, I just want your curiosity 🥳

I was wondering, is there a very big difference between data-structures and algorithms in OO and FP ?

I mean, I do a lot of exercises to be better in FP, especially in Elixir but I still feel that I’m in a rabbit hole and when the time to build « real world » stuff like a « rock star » dev I will be stuck.

It’s kinda impostor syndrome but with immutability.

What’s your best tips and ressources about this topic ?

Discussion

pic
Editor guide
Collapse
ahferroin7 profile image
Austin S. Hemmelgarn

Ultimately, almost every difference between the two comes from the fact that FP preferentially wants all data to be immutable, while OO wants it to all (or mostly all) be mutable. This in turn favors pure functions and separation of code from data for FP, and side effects and unification of code and data for OOP.

However, that has less impact than you might think on the actual algorithms and data structures that get used, it's mostly just differences in how they're implemented. The same applies to procedural code, it's largely the same algorithms and underlying data structures, just differences in how they're implemented. And that's perfectly fine, especially since you can often implement the same algorithm multiple ways without even switching paradigms.

On that note, one of the most helpful things for me as I've been learning Elixir has been to work on re-implementing things I already know how to do in other languages in Elixir. Because I already know the algorithm and have implemented it before, I don't have to focus as much on the algorithm itself, and can thus focus more on the implementation without having to worry about potentially getting the algorithm itself wrong.

Also, one big thing to remember, iterative looping doesn't inherently require mutability, despite what many FP proponents claim. You can do procedural-style loops just fine in Elixir using Enum.reduce/3 (or Enum.reduce_while/3 if you may need to bail early), it just requires a different way of thinking about the loop itself, and slightly different handling of the loop output.

Collapse
bjorngrunde profile image
Björn Grunde

A long time ago I felt the same when going from PHP spaghetti land to the wonderful paradise that was Ruby. And now I have the same feeling going from OO to functional, working with Elixir. I think this is a feeling we as developers will have quite often, especially those that are curious and driven. There is always something new and fantastic and we can't wait to jump right into several rabbit holes at once.

My best tips are quite boring, continue to work on your problem-solving. All programming paradigms takes time, some a lot of time. And people take time to learn and adapt, all differently.

Try to build something basic, like a blog (boring I know), but then make it more advanced by adding cms functionality, real-time notifications, publication schedules, and whatnot. But don't try to do a perfect application. It should be decent, good enough.

My point is that for every project you finish, for every little thing that is done, you are one step closer on your way out of the rabbit hole.

  • Edit, forgot to tip about resources *

I found the elixir/phoenix books that are recommended to be very useful, but the course Elixir Programming by Dave Thomas is fantastic and helps a lot with thinking functional.
codestool.coding-gnome.com/courses...

Collapse
maartz profile image
Maartz Author

Thanks for your advices ! Always helpful to have others POV.

It’s often hard to leave the confort zone and, to be honest, I usually don’t know where and how to begin.

Collapse
yogidevbear profile image
Andrew Jackson

If you're interested in seeing how to convert some of the OOP patterns into FP ones, I would highly recommend pragprog.com/book/mbfpp/functional...
It covers how patterns you would use in Java can be converted into FP patterns in Clojure and Scala.

Collapse
maartz profile image
Maartz Author

Thank you Andrew ! Coming from OO Java this fits totally my needs. I hope the gap between syntaxes will not be an issue.