3 ms·
"Object-oriented programming" seems to have a "blind man and the elephant" problem. People differ radically in their opinions of it based on what they think "OO
by michaelochurch 16y ago
"Object-oriented programming" seems to have a "blind man and the elephant" problem. People differ radically in their opinions of it based on what they think "OOP" is. The problem with OO as commonly practiced is that it encourages the promiscuous, distributed proliferation of mutable state. Sometimes this is the right model, especially when involving processes that might be on separate machines, but not usually.
For example, you can write a Die class with a sides field (integer), a topFace field (integer) and a roll method that sets the value of topFace to a random integer between 1 and sides, inclusive. To use the Die, call roll and getTopFace. Want to roll 5 d10s? Then create Die(10) five times, then call .roll and .getTopFace on each of them. By the way, you now have five pieces of mutable state in your program and, if you ever forget to call .roll and only call .getTopFace on your dice (maybe you've been tempted to keep the Die objects around for "efficiency") you get erroneous results.
Clearly, that's the wrong way to solve the problem. It's better to just call (rand 10) five times: in general, you only care about the results of the rolls, not the "dice". (This approach still isn't "purely functional" as written, because it still uses a randomizer, e.g. state, but it's close enough for most purposes. Passing around a PRNG state is pretty heavyweight.)
Sometimes you need the full power of pi-calculus (communication, message passing) but usually you want to stick with lambda-calculus (referentially transparent computation) as far as you can take it. Mutable state is almost always necessary at some point, but you want to segregate it as much as you can.