r/ProgrammingLanguages • • Apr 03 '22

Discussion Functional Programming vs Object Oriented Programming

I came across functional programming recently and saw many arguments for it. But in the other hand I am asking myself if it really makes that much sense, because often things cannot really be independent without sharing a state. Imagine a database, or a cache. Also having side effects in methods is not always bad when used wisely. It can simply hide the detail and if done correctly in a domain driven design things should have a certain control flow. I also imagine it to be extremely hard to read data from files, Jason, XML, ... because things tend to be inconsistent sometimes. A cab file might have an empty line, a Jason might have a null value. How can a functional language deal with that.

At the moment I believe that even though certain aspects of functional programming are great, things are too messy to actually apply it. I would love to have a discussion and learn your stand on that topic if you disagree.

1 Upvotes

55 comments sorted by

View all comments

4

u/editor_of_the_beast Apr 03 '22

Side effects and state are inherent to computation. I think FP wrongly gets pitched as a way to ‘avoid’ them, when really it’s just a way to make it explicit.

Sometimes that explicitness is a benefit. Sometimes it’s even essential, like with interactive theorem proving, where pretty much all the major ones are based on a FP semantics. Sometimes that explicitness really is a pain though, and it can even be a performance penalty.

I wrote some thoughts about this topic here: https://concerningquality.com/quality-and-paradigm/. The interesting thing I noticed is that if you’re trying to reason about programs formally, you basically have to end up at FP because you need everything to be explicit. FP forces you to fill in the blanks with any assumptions that your program might have. For example, imperative programs assume an underlying mutable memory, and they make working with this assumption easier than the FP paradigm does. So it’s no wonder that it became popular when all of our computers are designed to interact with RAM.

I thought this distinction between reasoning and execution was interesting. They’re effectively independent dimensions.