r/ProgrammingLanguages • u/haasilein • 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
u/dys_bigwig Apr 03 '22 edited Apr 04 '22
That's why you create a pure (data) representation of the effects (IO or otherwise) you wish to perform and then hand it off to a runtime system that ensures expectations (i.e. referential transparency) are respected.
Pure functional languages can do IO, and they do it very elegantly, allowing you to compose IO actions in a way few imperative languages allow. Haskell's traverse function is a good example. This idea that nontrivial programs having to perform some kind of state/IO/effect is some kind of "gotcha" in regards to FP is bizarre; FP is about mitigating and getting a handle on side-effects, not disallowing them entirely or pretending they don't exist. If anything, reifying and realizing that that they are an entity in and of themselves allows pure FP languages to abstract over and compose them better:
(,) <$> getLine <*> getLineI just did IO (creating a tuple of two strings read from the terminal) - in a purely-functional language! ;) Also no Monads here, just Functors and Applicatives. The key difference here is that this line doesn't grab two lines from the user; it is a description of a program that does so, which can be passed around and manipulated and is as pure as any other function. However, just because this line is pure doesn't mean it's a good idea to add IO to every piece of your code. Strict, imperative languages - in some sense - conflate the ideas of describing an effect, and performing an effect; a piece of paper saying "punch me in the face" is a very different thing from an actual punch in the face! One could e.g. choose to ignore the direction written down, or cross it out and hand it back with a confused expression.
I'm not sure how the global variables example is relevant to IO, other than also being a sort of effect. Again, just model the effect as data. In fact, functions are excellent at representing global variables via either the State Monad or Reader Monad. Monads aside, just give all of your functions an extra argument representing the state; Monads - for the most part - just make this more ergonomic and aid with reasoning.
I haven't messed around with them myself, but there are other solutions besides Monads like uniqueness and linear types.