r/functionalprogramming • u/Jiruze • Mar 01 '24
Question Functional in OOP code base
Is it practical to write functional code inside a highly OOP code base?
I'm tired of searching through every instance of a state variable to analyse the impact. OOP often hides the data flow behind procedures, which took me some additional time to understand a piece of code. I wonder if I could at least try to change how it written so it easier to understand and debug?
11
Upvotes
16
u/PointOneXDeveloper Mar 01 '24
Bringing in a shitty version of Haskells type system is a bad idea. Don’t start using Result types and such when the language doesn’t have good tools to make that ergonomic.
Using immutable patterns, trying to keep most functions as pure as possible, separating logic from IO boundary, all good ideas and not even strictly at odds with OO, but you have to be pragmatic.
Is reaching for a global singleton logger a dirty side effect? Yep, but also everyone will hate you if you start returning (T, Report[]) tuple types everywhere just to push logging out of your otherwise pure code.
8
u/Voxelman Mar 01 '24
Generally you can write functional "style" in almost any language. How well this works depends on the language syntax and the structure of the existing code.
5
u/imihnevich Mar 01 '24
Writing new modules in immutable style is usually fine, but marrying old with new is harder
4
u/[deleted] Mar 01 '24
Functional means absence of side-effect. You are more functional by using less side-effects. To do so is generally practical and i advise. This is not changed by being in an OOP environment.
So my advice: do not force yourself to eliminate all side-effects, just avoid them when you can easily do so. Do not be afraid of commiting the sin of stepping outside of your supposed paradigm.
7
u/SomewhatSpecial Mar 01 '24
It's usually not easy - you'd need buy-in from your colleagues and tech leads, but most people in OOP shops are used to OOP and not particularly inclined to make such a fundamental change. There are possibilities though - if you're the code owner on some part of the system that's not too tightly coupled with everything, you could use FP behind its public api and then leverage the benefits (robustness, stability, easy maintenance) to argue for the value of this approach.
3
u/dogweather Mar 01 '24 edited Mar 01 '24
I vote yes, it's practical to write functional code inside a highly OOP code base. :-)
The biggest caveat will be your team and their buy-in.
You don't mention a language, so I'll use Python as an example. I personally found it very easy to begin doing functional programming inside a big messy OOP Python codebase. It was so messy that the full app could only run on my manager's home computer. (!) FYI, this was no tiny startup. Starbucks was our client. They should have known better. Anyhow…
I didn't give it much thought; I just worked like normal. Mostly TDD, writing tests for the functions I wanted, then coding them using Python's standard modules and functions. I didn't need to create a
class, so I never did. (That says a lot in favor of fp IMO.)My boss was a little confused, then low-key impressed. The assignment had been something like,
So when he asked how to use my code, I showed the top-level function and replied, "Just call this,
some_module.process_cc_number()". It was a lot less work than my team-mates were used to. Normally they'd have to find a "factory", instantiate a class, remember to set the correct attributes in the right order, read the comments to see how to tell it to "run", blah blah blah…2
u/metazip Mar 01 '24
In an immutable point-free language it would look something like this: look at the definition of the name complex
2
u/wojm Mar 02 '24
Functional core, imperative shell
It might be a good phrase to get your colleagues on board. I've found it resonates with a lot of people and is a good rule of thumb
2
u/flatmap_fplamda Mar 02 '24
Scala has a lot of that
2
u/thesmartest Mar 02 '24
Not only is it practical but you should do it if your willing to put in the time. If you squint just a bit everything from SOLID is pretty much for free when working in a functional style, which is what people in OO want anyway. From experience, programs will mostly end up with 2 kinds of objects - the majority being immutable (or locally mutable) data objects and then controllers/managers that deal with open interfaces to the outside world with state management/side effects. Then you can squeeze state/side-effects to as small of a surface area as possible and have nice transparent pipelines. Above those classes, a few generic interfaces/classes to inherit from to bridge similarity in types and keep things dry, but nothing like the subclasses on subclasses or functions on functions you often see in OO code bases; focusing on composability.
Personally, I've helped teams that never heard of FP, working in common dynamic languages, to working in this sort of style with great success. Convincing your team(s) will be the hardest part, but I recommend starting with practical examples and addressing specific problems and avoiding FP lingo. From experience they will open up quickly. Be diligent (yet kind) in code review, and have patience, it absolutely can be done and everyone will benefit but it will take time.
3
u/scrubbar Mar 01 '24
If you're working in a team and you increase the complexity of the system in a way the rest of your team does not understand your not positively improving the code base.
Your team needs to be able to maintain any area of the code so you need their approval to do things like this. It's a team effort.
You should bring up the issues your having in a retrospective with your team. There might be other ways you can all agree to solve it.
2
u/RedEyed__ Mar 01 '24
Hello, I'm in such situation as well. As others said, increasing complexity to your project by adding new paradigm is not reasonable.
On the other hand, when I add new stuff, I try to write it functionally internally and then wrap it to class with minimum state, so others can use it like before. I think it is called "functional core, imperative shell", but in that case, core is tiny piece.
0
u/[deleted] Mar 01 '24
[deleted] — view deleted comment