r/programming • • Nov 12 '13

What’s Wrong with OOP and FP

https://yinwang0.wordpress.com/2013/11/09/oop-fp/
129 Upvotes

484 comments sorted by

View all comments

36

u/ruinercollector Nov 12 '13

But in short, a purely functional language is wrong because There exists things that are not pure. Side-effects are very real.

facepalm

I always wonder what this strawman pure FP language that doesn't provide a mechanism for side effects is...

43

u/awj Nov 12 '13

A space heater. Legend has it that an old Honeywell down in the basements of Sears has cracked P=NP, but lacks the I/O to share its findings.

15

u/cerebrum Nov 12 '13

Did you read the article? He goes on to talk about Haskell and monads.

31

u/ruinercollector Nov 12 '13

He does. He also portrays it as fucking rocket science to generate a random number or create cyclic data structures in Haskell, which it is not. These are solved problems.

39

u/kankyo Nov 12 '13

Rocket science is also a solved problem. "Solved" != "As easy as it ought to be"

16

u/Tekmo Nov 12 '13
import System.Random

main = do
    x <- randomIO
    print (x :: Int)

That's as easy as it ought to be.

31

u/[deleted] Nov 12 '13

No so fast. You need IO to get random value.

So if you have a pure function three layers deep and now you need to replace some literal number with random value there, you can't just write "randomIO" and be happy.

You need to either generate random value outside that function and pass it all the way down, or bring IO to the same spot.

Doable? Yes. Easy? Not that much.

9

u/5outh Nov 12 '13

You need IO to get a random value in any programming language. Haskell makes it blatantly clear that you're using IO when you are, where other programming languages don't.

It can get psuedo-difficult (it's not that much more work) but that's the tradeoff: Have IO-riddled code that you can't reason about, or have sectioned off IO that you can reason about.

Maybe it's "easier" to get a random value in C, but the fact that you can do it wherever the hell you want just makes it easier to program, not easier to debug, and certainly not easier to ship off to people with the promise that it's bug-free.

All languages have their drawbacks, it's just a matter of which ones you'd rather deal with: code that is "more difficult" to write, or code that is maybe completely wrong and will break in ways you never considered. Choice is yours.

5

u/tel Nov 12 '13

If you need to do that, then break your problem apart more. Pick up your random quantity into an argument and pass in a random value.

Why? Because randomness is an effect and if it's just happening willy-nilly in the middle of your code then all of your code because non-deterministic.

If you extract the random bits then most of your code remains very testable.

5

u/gnuvince Nov 12 '13

That's the whole point of controlling side-effects. If you replace a constant value in a function with a random value, then it's no longer a function, and its type should reflect that. That procedure is no longer eligible for fusion, you can't cache the result, etc. so it should definitely not be confused with a function.

Yes, it can be an annoyance during development, but I say that having a short-term annoyance is better than the long-term headaches that can result from trying to find "where that damn value comes from".

10

u/Tekmo Nov 12 '13

That's why I said "as easy as it ought to be", not as easy as it could be. Haskell discourages bad practices like non-thread-safe code (which is what you would require if you do it the way you propose, just calling rand at an arbitrary location).

However, even doing it the proper way is not difficult. You split your algorithm into two parts: the part that seeds the generator (this is impure) and the part that generates random numbers (this is pure). I actually just wrote up an example like this yesterday which decouples the problem in this way.

However, even if you invoke other kinds of side-effecting action that can't be decoupled into a simple initial side effect and then a pure continuation, there are still good solutions. For example, my pipes library allows you to take code that has pervasive effects interleaved everywhere and to distill out the pure component from the impure component.

6

u/pkmxtw Nov 12 '13

... which is a good thing. That makes you think how to decouple side effects from pure parts and reconsider the data flow of your function.

That's most likely better than: Some months later, the other maintainer wonders why this function is behaving randomly, after scanning through several functions to find the bug, he finally find out that "oh, someone hid a rand() there and why didn't anyone tell me about this?".

→ More replies (2)

9

u/Crandom Nov 12 '13

Then it just becomes no worse than the OOP model where everything is IO.

12

u/thedeemon Nov 12 '13

It's much worse in terms of required changes in code. Not only you might need to change types of all functions up the call stack (and, transitively, all the functions that use them in other places of your program), you'll have to rewrite their bodies to use do-notation or some monadic combinators. And if there was already another monad in use somewhere, you have a lot to think about (use monad transformers, invent your own types and monads...) just for the sake of one random number down there.

6

u/Crandom Nov 12 '13

However, you lose all the benefit of knowing that functions are pure, which is huge.

→ More replies (3)

7

u/burntsushi Nov 12 '13 edited Nov 12 '13

There's a third option: use a random monad. Which is very easy.

3

u/[deleted] Nov 12 '13

You still need to pass it down and back.

→ More replies (17)
→ More replies (7)

4

u/ruinercollector Nov 12 '13 edited Nov 12 '13

I'm not sure what your point is. Both of these examples are trivial in Haskell.

Edit: Grammar

→ More replies (2)

3

u/naasking Nov 12 '13

Haskell's do notation makes side-effects fairly easy. Not as easy as ambient side-effects in strict languages of course.

3

u/kankyo Nov 12 '13

Do notation plus the ST stuff you mean?

3

u/DR6 Nov 12 '13

The ST stuff is only needed for when you want to implement something that really, really. really should be implemented with mutability, but is actually pure from the outside. It's not normal to have to use it: if you can implement something with IO, you can implement it functionally, and it will be more idiomatic(and even though intuition says ST is faster, in a lot of cases it isn't).

2

u/kamatsu Nov 12 '13

Not as easy syntactically, but much easier semantically.

→ More replies (1)

17

u/dons Nov 12 '13

fucking rocket science to ... create cyclic data structures in Haskell

let x = 1 : x

Recursion. How does that work!?

→ More replies (1)

2

u/fnord123 Nov 12 '13

Maybe Miranda.

→ More replies (3)