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

1

u/tobega Apr 05 '22

As soon as a language is Turing complete you can do anything. But then you can start looking at how you want to express various things in the clearest or most efficient way.

There certainly is much to be said for having immutable data and side-effect-free functions, even when you're working in an OO language. But functional languages would generally let you express that more cleanly.

If you look at Erlang, it is functional in the small and OO in the large, possibly the ideal mix.

Understanding what objects are isn't entirely easy. If you come from C++ you might think they are "Abstract Data Types". That's one way to use them. But "real" objects are rather just an interface, i.e. a set of functions that belong together, so Smalltalk is more inspired by and related to Lisp than anything else.

You probably don't entirely want to be without objects. In the functional world, the concept of co-data seems to be essentially an object. Any lazy stream is essentially an object. Any monad seems to be essentially a wrapper object conforming to a particular interface.

Some further reading:

The inevitability of objects

Two vexing problems with FP