News for hackers. Stuff that matters.
Re:OOP Isn't a Fundamental Particle of Computing by michaelochurch on Wednesday November 14, 2012 @07:27AM (#4783056)
One of the reasons discussions of OOP leave me feeling dissatisfied is that OOP has a "blind men and the elephant" problem. One feels the tail and says, "this Creature implements Rope". Another feels a leg and says, "this Creature implements Tree". A third feels the side and says, "this Creature implements Wall". We end up discussing dramatically different things.

My big issue with OOP is that it injects unnecessary complexity when used as a catch-all. Alan Kay advocated OOP as something to do when complexity became unmanageable: encapsulate it behind simpler interfaces. That's a good idea! Unfortunately, the OOP fad dovetailed with the 1990s-ongoing attempt to commoditize programming talent and we ended up with a generation of mediocre programmers who took OOP to mean "Go out and build massive, complex, over-featured objects", not "Here are tools to reduce complexity when needed". Alan Kay's original message was hijacked, producing the monstrosity of corporate OOP.

When I advocate FP, I usually put it like this. Instead of having 23 poorly-understood and often badly implemented design patterns, functional programming has two design patterns: Noun and Verb. Nouns are immutable data: from integers to record types to OCaml's unions to Scala's tree-based Map and Set. (Occasionally Nouns have to be mutable, which means they have attached Verbs. I'm glossing over that for now.) Verbs are functions-- when possible, referentially transparent. We can also use the latter as nouns, which gives us cool "combinators" like map, reduce, and flatMap/mapcat that help us to compose functions.

There are two annoyances we hit with "programming in the large" when we're restricted to nouns and verbs. One is namespace collision, and the object-oriented solution (see: Scala) is to have locally interpreted functions, or methods. That has advantages and disadvantages, but is sometimes the right solution. A related issue is the Adjective Problem-- how to handle similarities, such as objects with a "close" method or collections with "foreach"-- which Haskell solves with type classes, Ocaml with functors, and Java with inheritance. The issue I have there is that it's hard to solve the Adjective problem using "modern" OOP without injecting non-locality into code comprehension, and we learned a lesson about extreme non-locality with Goto. I am not the world's biggest fan of inheritance.

The core idea of object-oriented programming-- that you should hide complexity behind simple interfaces and expect the application programmer only to know the latter-- is solid gold. That's why you don't have to learn a whole new language whenever you move to a different SQL database (or a later version of the same one): the implementation changed, the interface (or, at least, most of the parts you care about) didn't. The problem is that it's really hard to get interfaces right, and average corporate programmers don't have the ability.

Professional programming, by the way, is all backward. The way to become a decent programmer for realz is to start on very simple, self-contained projects like scripts for data analysis, and move upward to more complex programs once you get the non-trivial architectural problems associated with simple ones down. The Unix philosophy (small components, large software solutions being respected as systems rather than thrown together as one giant single program) is superior in general, but especially when people are learning how to write software. You don't learn software architecture except by doing it, and starting small gives you the quick feedback cycle that helps you learn. Corporate programmers in OOP-land often never get the architectural experience of writing systems (starting with small ones) from scratch. Instead, they spend their 40 hours maintaining and tweaking large monoliths other people wrote, and this is a big part of why they never improve.

Re:OOP Isn't a Fundamental Particle of Computing by stonemetal on Wednesday November 14, 2012 @08:48AM (#4783253)
I usually put it like this. Instead of having 23 poorly-understood and often badly implemented design patterns, functional programming has two design patterns: Noun and Verb. Nouns are immutable data: from integers to record types to OCaml's unions to Scala's tree-based Map and Set. (Occasionally Nouns have to be mutable, which means they have attached Verbs. I'm glossing over that for now.)

Then I would have to ask how is that different than OO other than the straw man at the beginning. It has Nouns and Verbs, sure data is default mutable instead of immutable but immutable is certainly an option. OO typically calls their verbs methods instead of functions but referential transparency is a good thing on the OO side of the fence too. We tend to call map for_each, etc.

Frankly I am beginning to realize that FP vs OO is the biggest bike shed I have ever seen in the real world.

Re:OOP Isn't a Fundamental Particle of Computing by Robin_Message on Wednesday November 14, 2012 @09:14AM (#4783362)
http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent

The real differences are in solving the expression problem (verbs are easy to add in FP, nouns are easy to add in OO), and as you say, OO's default of mutability vs FP's aim at referential transparency.

Re:OOP Isn't a Fundamental Particle of Computing by michaelochurch on Wednesday November 14, 2012 @09:19AM (#4783380)
The problem with the mutability debate is that most examples show two programs, one mutable and one immutable, on the scale of about 20 lines of code. At the small scale, it's a toss-up which is better. Often the mutable solution's more intuitive for most programmers, and sometimes just flat-out superior: more clear, easier to understand. Mutable state isn't evil. It's necessary in the real world. It's just that stateful actions don't compose well, especially when you involve concurrency or Big Code problems. Good programmers learn that they need to manage (not eliminate) mutable state. That's what FP is about.

So the aesthetic dominance that FP advocates hope to establish with their 20-line A/B depictions doesn't come through, because the truth is that the problems with mutable state very rarely show up (except in contrived, over-complex examples) at 20 LoC. At 20 LoC, the snippet you'll like better is going to be the one you're most familiar with. The real differences show up at 2000 LoC, which can't be put in a PowerPoint.

Immutable programming is somewhat less prone (but not immune) to complexity creep. For example, you see 500-line for-loops in corporate software all the time. The conceptual integrity is gone because so many people (who never learned what the others were doing) have added tweaks to it.

The difference between mutable and immutable programming is that making that sort of change to an immutable program also changes the API, unless it's purely a performance tweak (e.g. plus(2, 2) still returns 4, but does it faster). If you add logging to plus in a purely functional world, you change its signature from (Int, Int) => Int to something like (Int, Int) => (Int, String). As you might guess, that's a double-edged sword. Sometimes you want people to be able to add "purely stateful" (i.e. no API changes) effects without changing a signature... but very rarely.

So I think the major upshot of immutable programming is that it makes it impossible to add many varieties of complexity that corporate engineers tend to add silently (in pursuit of a short-term hack) without changing an API and breaking the build. This slows down complexity creep, and that's a good thing.

It's the reduction of that externalized-cost/complexity-creep dynamic that makes FP superior, in my opinion. A 60-line referentially transparent function really isn't less evil than a 60-line method of an object. They're both fucking incomprehensible, in most cases. You're just less likely to see the 60-liner in a mature FP codebase. Also, because functions compose better than stateful actions, it's usually a lot easier to break large functions up long before they get anywhere near 60 lines. (In my opinion, double-digits are "warning" territory and 25+ lines means it should almost always be split, at least into inner functions.)

Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @09:31AM (#4783434)
When you have state...I mean real state, it sure is nice to encapsulate that state in an object rather than in what is basically an unencapsulated monad. OO supports state encapsulation, pure FP basically does not, that is a big deal. Immutable programming sort of side steps the issue, that state is needed at all, that an interactive program can somehow be stateless is ridiculous, even many batch programs require some form of state (even if it is unencapsulated in a monad).

Your 60-line function decomposed into nice small parts using beautiful composable abstractions is an FP pipe dream. Yes, if the problem is well understood, someone has thought about it for a long time and has come up with some beautiful abstraction that works for a narrow set of related problems. Now, as soon as you venture outside of a well-understood/nice abstraction domain, your code is just as bad in FP, if you can figure out how to implement at all.

Re:OOP Isn't a Fundamental Particle of Computing by chousuke on Wednesday November 14, 2012 @11:19AM (#4783958)
Could you explain why a monad is unencapsulated? As far as I can tell, using monads is a far superior way to encapsulate state, since it's impossible to use the state without also operating within the type universe that you've defined for your stateful calculations. Therefore, you can trivially tell if a piece of code is relevant to the state of your program, and it's enforced at compile-time.

Further, since the actual state values are immutable, you can store references to them indefinitely. In most OOP languages, the whole concept of a "state at this point of execution" is completely unsupported, and that state is inaccessible to the programmer.

Edit: I wrote this assuming we're talking about a sufficiently advanced static type system here, such as Haskell's. I've implemented monads in Clojure and while they can be useful, of course in a dynamic language they provide less safety since it's trivial to escape the monad.

Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @12:11PM (#4784256)
The problem with monads is that they DO leak into the type system. What effect the object has must be exposed for type checking reasons, and it can't be encapsulated, hidden, changed transparently, and so on. Try iterative algorithms, UI programming with views and models, an interactive code editor, etc...you get trapped quickly by the type system. The point is, you often want to be oblivious about what that object is doing and when it is doing it.
Re:OOP Isn't a Fundamental Particle of Computing by chousuke on Wednesday November 14, 2012 @12:46PM (#4784439)
I'm sorry, I don't see the problem. You can encapsulate away the state behind an opaque type so that it can be only accessed by functions that you have defined. Whether this is a good choice is up to the programmer, but it's common practice in haskell to use domain-specific types wherever it makes sense.

For UIs and such, in addition to monads there are more powerful abstractions, but at no point is it necessary to leak information to the client. The common UI toolkits tend to have some impedance mismatch with FP because they've been designed with OOP in mind, but this is not the fault of FP.

Of course, in FP it often doesn't even make sense to encapsulate everything. Why hide useful data when it's guaranteed that any user will not be able to misuse it?

Haskell provides tools for abstraction that are IMO vastly superior to anything I've seen in an OOP language. If anything, you're more likely to have leaky abstractions and failed encapsulation in Java or C++ compared to Haskell, simply because of mutable state, closed classes, and limited expressiveness of the type system.

Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @01:27PM (#4784662)
Ah, they just didn't design their UI libraries right? I hate this argument, because its easily proven true (someone just has to design a "right" library) and impossible to prove false (the "right" library could exist, it just hasn't been built yet).

The problem with Haskell, which isn't a problem in an impure FP like Clojure, is that you can't define objects at all (in the sense that objects completely encapsulate state). Yes, you can do a lot of nice combinator tricks, but these only work well for well understood problems, and so FP practitioners spend most of their time trying to understand problems very well so that they are amenable to their elegant abstractions. Whereas if I just use an OOP language (or a pragmatic FP language), I just solve the problem in a hacky way without needing to completely understand its essence (which, for me at least, only comes after I've solved the problem N times in N different ways!).

Closed classes suck, but not all OOP languages restrict you to closed classes. Scala does very well here, and traits are wonderfully expressive.

Re:OOP Isn't a Fundamental Particle of Computing by se85 on Wednesday November 14, 2012 @11:18AM (#4783953)
Oh really? A pipedream you say? Oh ok then...
Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @01:29PM (#4784672)
Let me reply your one liner with my own: nice elegant composable abstractions are great when they work, and very horrible when they don't.
Re:OOP Isn't a Fundamental Particle of Computing by se85 on Wednesday November 14, 2012 @10:19PM (#4786905)
I don't see how this is any different for "OOP" though (or anything in life for that matter)?

Things are always great till they don't work, and usually are absolutely horrible when they don't.

I see your pipe dream on a daily basis as do many other competent functional programmers so I have to disagree with your entire argument which seems to more related to human error than programming paradigms getting in the way.

Re:OOP Isn't a Fundamental Particle of Computing by eternalban on Wednesday November 14, 2012 @08:10AM (#4783136)
> ... functional programming has two design patterns: Noun and Verb...

You have a fundamental misunderstanding about pattern languages. It would be correct to say "functional programming is a pattern (language)" and this pattern (language) has the "elements Noun and Verb".

p.s.: http://patternsinfp.wordpress.com/

Re:OOP Isn't a Fundamental Particle of Computing by michaelochurch on Wednesday November 14, 2012 @08:20AM (#4783163)
Pattern languages are new to me but I'd guess that I know more about programming languages in practice than 97% of programmers, and 100.0% of the people (by definition) who think VisitorSingletonFactory classes are a good idea. So if I'm getting fundamentals wrong in that part of the discussion, and I could well be, then I think there are precious few people getting this stuff right.
Re:OOP Isn't a Fundamental Particle of Computing by eternalban on Wednesday November 14, 2012 @08:31AM (#4783191)
Your competence as a programmer was not questioned, Michael, and was not even remotely a consideration. Have a beautiful day!
Re:OOP Isn't a Fundamental Particle of Computing by michaelochurch on Wednesday November 14, 2012 @08:38AM (#4783206)
Sorry if I seemed aggressive and snarky. OOP gets my blood boiling. The disgraceful state of most software architecture is a hot topic for me, having seen a couple of companies fail on account of code quality issues.
Re:OOP Isn't a Fundamental Particle of Computing by sageikosa on Wednesday November 14, 2012 @09:16AM (#4783368)
That's not OOP, its the designers and coders. The "problem" with being the mainstream programming paradigm is that the bell curve of talent is fully stocked from high to low ends.

The marketplace of managers looking for: a) cheap labor b) well-understood technology c) ability to fallback to expert knowledge

grew up with object-oriented paradigms.

Since OOP frameworks removed many low-level coding tasks, the cost of entrance into the labor market allowed the labor pool to be flooded with developers, it helped that demand was so high and investors so hungry that proof-of-concepts often became products.

What came out of all this is that OOP matured, but OOA and OOD didn't keep pace as market demanded disciplines. What you see today is many OO systems that were not analyzed or designed by architects who were good analysts and designers, but who understood OOP and had access to a pattern-book.

But, taking a page from category theory, we could abstract OO from OOP, OOA and OOD, and should be able to easily see that the same could eventually apply to FP, FA and FD should the bell-curve of talent fill out due to market demand.

Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @09:38AM (#4783476)
The nice thing about a language like Haskell, then, is that you have to be really smart to use it, reducing the market for low end programmers. Actually, many companies put Haskell (or even Scala) down as a requirement just to filter out the low end programmers, they have no strong intention of actually using those languages.

OO is accessible because it is much more naturalistic than FP, being based on natural metaphors (objects) and natural language concepts (subject verb) that make many computer scientists cringe (was it Dijkstra who was against anthropomorphic metaphors?). FP is much more based on math and logic, conveying certain benefits but requiring a change from the way we are normally wired; I've been in a room full of the best of the FP guys before and I could have sworn I was on a different planet.

FP is a reliable tool in moderation. But when I'm designing a text editor, object thinking is definitely preferable.

Re:OOP Isn't a Fundamental Particle of Computing by papsosouid on Wednesday November 14, 2012 @10:34AM (#4783732)
>The nice thing about a language like Haskell, then, is that you have to be really smart to use it

I'm living proof that you are mistaken.

>Actually, many companies put Haskell (or even Scala) down as a requirement just to filter out the low end programmers

If by "many" you mean "a couple", then it depends what you mean by "low end programmers". Asking for haskell is decent at weeding out people who have no interest in learning new things and improving themselves. It isn't particularly useful for filtering out people who aren't "really smart".

>OO is accessible because it is much more naturalistic than FP

It is interesting that you pre-suppose that is the case. My experience with teaching people programming with no prior experience is that OOP is not accessible at all, and FP comes quite naturally.

Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @11:14AM (#4783937)
Asking for Haskell is actually good for weeding out most American programmers, but whatever. The point is that you'll get a better programmer if they know lots of programming language paradigms. But I wouldn't for the life of me take on as an employee someone who knows Haskell but not an OOPL (like C#, Java, or at least C++), that would indicate severe ideological bias on their part.

I'm using naturalistic here as a real term related to natural language and humanistic fuzzy thinking that we seem to be born with as opposed to the logical mathematical thinking that we must learn at school. The idea that we think (or at least most of us) in terms of objects rather than formulas biases us towards object thinking when designing systems.

Its sort of preverse, but I agree with you: its easier to teach FP to people with no programming experience, since you can focus on simple stateless computations. Without objects, you can't even broach the harder topics that teachers carelessly throw into 101 courses. But even if you are using the SICP, you'll see object thinking introduced in Chapter 3.

But now we seem to be arguing about languages, of course you can have objects in an FPL and you can have lambdas on an OOPL. The best programmers will be able to apply both of course when applicable, and won't be ideologically biased. The not so great programmers are probably better off with OOP since that's what they'll need for their jobs, though a dose of FP can help them become better programmers.

Re:OOP Isn't a Fundamental Particle of Computing by papsosouid on Wednesday November 14, 2012 @11:22AM (#4783979)
>But I wouldn't for the life of me take on as an employee someone who knows Haskell but not an OOPL (like C#, Java, or at least C++), that would indicate severe ideological bias on their part.

Ironically, it actually indicates a severe ideological bias on your part, not theirs.

The rest of your post seems like it is entirely platitudes intended to say nothing. No, people don't think in terms of objects, that is why the leaky metaphor used to teach OOP traditionally causes so much confusion, and makes OOP harder to learn than FP. Thinking in terms of FP is not thinking in terms of "formulas", and it appears you are basing all the rest of your assumptions on top of that one faulty assumption. People intuitively understand "I make a thing that transforms other things" much easier than they understand "I make my things know how to transform themselves, or maybe other things, or maybe both, but sometimes neither and I make different things to do the transforming instead, but those things are wrapped inside an "object" that doesn't represent anything for no discernable reason".

Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @11:59AM (#4784174)
I'm not sure I understand, do you mean, that someone only knows an FPL and not an OOPL means they are more well rounded than someone who has experience with both. And I didn't even mean they have to do most of their programming in an OOPL. For what I do, I cannot afford to work with a PhD student who does not know more than one programming paradigm!

I definitely meant to say something, I think the platitude remark is uncalled for. People don't talk in terms of transformations, they talk in terms of instructions, recipes, responsibilities, parts and pieces, and lots of encapsulated processes. Again, check out chapter 3 of the SCIP, which you already probably know about if you are teaching programming from an FP perspective.

Re:OOP Isn't a Fundamental Particle of Computing by papsosouid on Wednesday November 14, 2012 @12:19PM (#4784308)
>do you mean, that someone only knows an FPL and not an OOPL means they are more well rounded than someone who has experience with both.

No? How would you get to that interpretation when I didn't even say anything about this theoretical person, but rather about you?

>For what I do, I cannot afford to work with a PhD student who does not know more than one programming paradigm!

Unless you are doing programming language research, yes you can. Almost everyone already does, it is just that the only paradigm in question is imperative, rather than functional. That's not a significant barrier for the vast majority of roles, especially not PhD students.

>People don't talk in terms of transformations, they talk in terms of instructions,

That's the same thing. People think "beat the egg". Beating is a transformation, egg is a thing. People do not think "send the egg a message requesting it to change itself to the beaten state", which is why the OOP metaphor fails so badly. Functional programming directly maps to the normal way people think, as does traditional imperative programming. OOP does not.

Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @01:22PM (#4784632)
> Unless you are doing programming language research, yes you can.

Yes, I do, but no, its still a problem. With my colleagues, they are even more strict about OOP experience. I'm sort of unusual in that I appreciate FP experience, or I actually demand it, as part of finding well rounded candidates (very difficult in China, to be sure).

You don't "transform" the egg chemically from an unbeaten to a beaten state. Instead, you beat it, and that well-known transformation happens as a side effect. Also, after you've beaten the egg, you don't have an unbeaten egg to reuse elsewhere in another operation. Finally, someone has to beat the egg, either yourself or your assistant, or a machine designed specifically for beating eggs. In this way, FP does not map to the way normal people think.

Re:OOP Isn't a Fundamental Particle of Computing by papsosouid on Wednesday November 14, 2012 @02:27PM (#4785020)
I'm now unable to tell if your post is serious or satire. The strawmen are so glaring and absurd that I have a hard time believing you are seriously attempting to use them to make an argument. Chemistry, seriously?
Re:OOP Isn't a Fundamental Particle of Computing by seanmcdirmid on Wednesday November 14, 2012 @03:05PM (#4785230)
Totally serious. You picked beating, which is THE classic side effect operation, and you had no idea what was going on? If so, just refresh your memory on what beating the egg actually does (spoiler: people don't beat eggs because they are angry at them):

http://en.wikipedia.org/wiki/Mixture

Your argument doesn't become strong just because you accuse me of logical fallacies, and actually, that's all you are really doing here. Good day.

Re:OOP Isn't a Fundamental Particle of Computing by papsosouid on Wednesday November 14, 2012 @03:20PM (#4785314)
Well that is genuinely impressive then. Please explain to me how the fact that no chemical reaction occurs when beating an egg is in any way relevant, or even related to the discussion, or anything I said. I didn't say beating an egg caused a chemical reaction, and at no point would anyone who has even the loosest grip on reality consider that to be relevant at all.

Had you just suggested that beating eggs is a side effect, and thus not purely functional you would have gotten a response. But when you go so far off the rails that the only reasonable explanations are that you are trolling or insane you shouldn't expect people to take you seriously enough to bother responding.

Yes, beating an egg changes the state of the egg from unbeaten to beaten. Yes, this is a difference, however it is not a difficult to understand difference. I explain it all the time, that's why I used it as an example. "With functional programming, instead of the egg_beater function changing the egg, it takes in the unbeaten egg, and returns to you a beaten egg. If you gave the unbeaten egg a name, you can still use it.". That is easy to understand, which is why people respond with "oh, ok" instead of confusion. This concept has to be understood when teaching OOP too, as generally methods that just change state return "this" so you can chain method calls: egg->beat()->fry(). So proposing that it is some unfathomable difficulty with learning FP is ridiculous.