DEV Community

Discussion on: Do the SOLID principles apply to Functional Programming?

Collapse
 
hugecoderguy profile image
Christian Kreiling

What a fabulous article, thanks for sharing! I have recently been trying to bridge the gap of OOP Design Patterns and functional programming myself, so I'm interested to read your next article. Maybe I can give some food for thought based on what I've been thinking about!

I program web backends in Java at my day-job but prefer the immutable & functional language Elixir for personal projects. They are vastly different languages but I find surprising parallels in how they're used. The primary Java backend I contribute to leverages the Visitor pattern to extend the behavior of objects that hold data, without having to define the behavior in their class definition. It seems like a good approach because it's made the service extensible in a variety of ways.

The more I worked within the bounds of the Visitor pattern, something became more apparent: it seems that the Visitor Pattern is actually just achieving one of the core principles of functional programming, which is to decouple structured data from its behavior! Another couple of OOP Design Patterns that I think achieve functional principles:

  • Observer/Publish-Subscribe pattern: Pub-Sub is a pretty functional concept in itself because you effecitvely register function callbacks with a centralized event bus object.
  • Strategy pattern: this pattern favors composition over inheritance for making a program extensible. The concept of composition is superfluous to functional programming!

I therefore think that some OOP Design Patterns, when applied, are actually making programs written in OOP languages more functional... I think it's really hard to think of OOP and FP as black and white; you can write FP code in an OOP language, and you can also write OOP code in an FP language. It's not worth arguing about which paradigm a program is written in or isn't, it's about which paradigm the program is more of. I'd like to hear your thoughts on this :)

Collapse
 
patferraggi profile image
Patricio Ferraggi

Hey Christian how are you? Thank you very much for that answer. It really have me something to think about. I never thought about the case of the visitor pattern, I am gonna have to check it better before my next article. I did saw the similarities you mentioned on the strategy pattern and the observer pattern, though in functional programming the benefits of the strategy pattern are much easier to achieve due to first class functions. I also see some similarities between function composition and the composite pattern. I definitely believe that a lot of the design patterns were created to give oop languages the flexibility that functional languages get for free without losing the control over data and state (at least in theory ahaha)

Collapse
 
hugecoderguy profile image
Christian Kreiling

Doing great, you've had me thinking about OOP design patterns over the last few days. I think we're saying the same thing wrt the strategy pattern; it's a means of taking some functions and composing their inputs & outputs through a centralized code module, which is what all programs written in an FP language look like. What similarities do you see between Function Composition and Composite Pattern? I see the Composite Pattern more as a means of representing a tree structure in a strongly-typed OOP language, is this what you mean when you say Composite Pattern?
Ooh, I really appreciate your observation "a lot of the design patterns were created to give oop languages the flexibility that functional languages get for free without losing the control over data and state." I've been trying to put that into words for awhile now