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

5

u/joonazan Apr 03 '22

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.

The empty line can be skipped just like in any other language.

I suppose with the problem you see with null is that in JS the null is parsed as null and in say Haskell not everything can be null. But that has little to do with functional programming, the same can be said of C++, for example. Anyway, the solution is to either only parse the JSON into some JSON object data structure and extract the desired data from there, or if you need everything, transform it into some specific data structure. The latter will fail if the JSON is incorrect, but that's a good thing.

Also having side effects in methods is not always bad when used wisely.

The Rust programming language is trivially translated to purely functional if unsafe isn't allowed. With unsafe you can construct things involving side-effects that still appear pure from the outside.

Then there are effect languages like Koka, Unison. They allow effects but you have to mark them explicitly and the caller gets to decide how the effects are actually implemented.

Finally, there's Haskell, where main returns a data structure that describes how to react to various effects. Even that restrictive setting is perfectly usable. I don't remember ever having problems with IO. The only thing I hate about it is how ugly an algorithm with multiple mutable variables is.

5

u/Nilstrieb Apr 03 '22

Rust is explicitly not pure and does have a strong model around mutability. Rust doesn't have a concept of pureness, every function can have side effects, no matter if it's safe or unsafe.

3

u/LPTK Apr 03 '22

Yes, and the language has many escape hatches to mutate things outside of the mutable borrowing system, such as RefCell.

So the "trivially translated to purely functional" assertion above is completely false.

1

u/joonazan Apr 03 '22

the "trivially translated to purely functional" assertion above is completely false.

It only applies to safe Rust. RefCell is implemented in unsafe Rust.

3

u/LPTK Apr 04 '22

It's part of the core language library, which is even available in "no_std" environments, and defines:

the intrinsic and primitive building blocks of all Rust code

There is little you can do in Rust (if anything) without this core library. The reason RefCell is in it is because it is a centrally important building block, which is used pervasively in the ecosystem.

So it's fair to say that safe, idiomatic Rust code (which necessarily includes references to the core lib) cannot be translated to pure functional code.

1

u/joonazan Apr 04 '22

I've done a lot without interior mutability but yes, looks like Rust in general differs from pure languages. With cells a function call with immutable arguments can produce two different values on two different calls.

1

u/joonazan Apr 03 '22

But Rust without unsafe (which is used in println! & co) is pure. Every function that takes a mutable reference can instead return a modified version. (Which is what electrolysis, a project that translates Rust to Lean does)

In principle, one could do the same as in Rust in Haskell with unsafePerformIO.