News for hackers. Stuff that matters.
Re:How Class-based Programming Sucks (2011) by barrkel on Friday December 13, 2013 @08:13AM (#6900304)
Class-based programming works great for GUI toolkits. In most other contexts, the suitability is variable. OO is a horrible match for compilers, for example.

The article is chasing down the wrong tree when he tries to build an Option type in C++, though. The normal OO way to handle the same class of functionality as ADT sum types is to use separate subclasses. What he's not acknowledging is that OO and functional+ADTs both only handle half the expression problem[1], in different ways. There's no absolute superiority for either model. It depends on the domain.

Less mutability is good almost everywhere though. The lack of persistent data types is a blight on almost all non-functional container libraries.

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

Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @09:59AM (#6900773)
PL designer here with extensive experience writing compilers in C#, Java, and Scala. Pattern matching is nice but OO will get you most of the way and filling the matching gap is simple.

The expression problem is much safer solved using virtual classes (using traits and type members) in scala than with case matching. There are so many OO solutions to this problem it's not funny; I think I had a small argument with Wadler over this back in 2001 or 2.

As for persistent collection types, observable collections with undoable operations are just as good, if not better because they can still support mutability while persistent collections cannot.

Re:How Class-based Programming Sucks (2011) by barrkel on Friday December 13, 2013 @10:22AM (#6900919)
I've written compilers in C, C++, C#, Delphi, Java, ML and Lisp; targeting x86, x64, JVM and MSIL. I was maintainer of the Delphi front end at Borland / Embarcadero. Pattern matching isn't just nice; you end up with substantially better typechecking, and doesn't need ugly patterns like visitors with their inverted callback control flow, if you want to decouple e.g. type checking or code generation from your AST classes. Visitors are implicitly closed to extension unless you create very abstract visitors (and I've gone down that path, it's not pleasant) - you lose a big chunk of the extensibility objects give you over ADTs. (I'd prefer to use multimethods, but they are rarely available.)

I think persistent collections, along with nice syntax for updating immutable objects (i.e. cloning with a subset of changed attributes) are more practical than having undo available. Immutable data types remove the burden of worrying about who's going to modify the data type from under you, so it lets you share subgraphs more freely. You don't need to worry as much about coupling, because the things you give your state to can't modify it. A possible alternative is mutable collections with snapshots or freezing, but I think persistent collections are a better approach.

Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @07:55PM (#6903936)
I've never had to use visitors in my compiler implementations; abstract methods + overrides is good enough; I also use partial classes in C# heavily even those these don't support separate compilation. I also dislike callbacks also, but control flow in general is abstracted away by a flow analysis framework of some sort anyways (technically you don't need this for parsing, but it works well for the systems I build). On the other hand, scalac is just a giant pattern match in Typers and Namers, a style I dislike but to each his own.

I've built a live programming environment where reactive mutable collections are much more appropriate than immutable persistent collections, you can check it out here:

http://research.microsoft.com/en-us/people/smcdirm/liveprogr...

The problem with immutable collections in general is that they completely cannot track change deltas, which is necessary when building an incremental systems. Undo is also essential for these kinds of systems. I think persistent collections are a dead end, but its an argument I'll have to work on over the next few years.

Re:How Class-based Programming Sucks (2011) by nilkn on Friday December 13, 2013 @03:04PM (#6902595)
The most complex code I've ever personally worked with involved a proliferation of abstract visitors in Java. I really don't think most people working on the code could understand how it all tied together (myself included).
Re:How Class-based Programming Sucks (2011) by haberman on Friday December 13, 2013 @05:40PM (#6903306)
> Visitors are implicitly closed to extension unless you create very abstract visitors (and I've gone down that path, it's not pleasant)

I'd love to hear more detail about your experience on this point.

Re:How Class-based Programming Sucks (2011) by xerophtye on Friday December 13, 2013 @11:44AM (#6901426)
Wow. Don't mind us mere mortals please continue your discussion, it seems really fascinating even though i hardly understand a word of it.
Re:How Class-based Programming Sucks (2011) by Nekorosu on Friday December 13, 2013 @05:18PM (#6903228)
I consider myself a mere mortal too but I can understand most of the discussion.
Re:How Class-based Programming Sucks (2011) by smosher on Friday December 13, 2013 @01:19PM (#6901984)
I couldn't help but notice this phrase:

[X] is nice but [Y] will get you most of the way and [dealing with the difference] is simple.

reads a lot like the Blub conceit. I'm not accusing you of that, but I do disagree that OO can give you what pattern matching typically does.

Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @08:02PM (#6903981)
I live ok without pattern matching and I build compilers/runtimes for a living; is that blub conceit?

I also have no problem with the expression problem. C# has partial classes anyways, which work even if type checking is non-modular. Of course, I would like it if C# support some form of pattern matching, but not enough to switch over to F# (whose pattern matching isn't as rich as Scala's, anyways).

Re:How Class-based Programming Sucks (2011) by smosher on Friday December 13, 2013 @08:44PM (#6904141)
> I live ok without pattern matching and I build compilers/runtimes for a living; is that blub conceit?

No, I would have quoted it the first time if it was. But if you want to repeat that again you will certainly sound conceited.

Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @09:40PM (#6904324)
OK, I have no idea what point you are trying to make then. Please forgive my ignorance on your social conventions.
Re:How Class-based Programming Sucks (2011) by smosher on Friday December 13, 2013 @09:56PM (#6904374)
The point was simply that what you wrote is how a very bad argument starts. I took care to point out I wasn't accusing you of that but you have reacted defensively anyway.

It's my fault for trying to engage an HN-er in a form of discourse other than debate.

Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @11:34PM (#6904629)
If its a meta comment, just say so directly. It is a bit difficult to decode these comments sometimes, especially when defending OOP.
Re:How Class-based Programming Sucks (2011) by coldtea on Friday December 13, 2013 @08:12PM (#6904023)
>reads a lot like the Blub conceit

Perhaps. But then again the "Blub conceit" is nothing that exists in real life as a proven fact of computer science.

It's just an argument expressed in an essay. Not some kind of formal logical error.

Re:How Class-based Programming Sucks (2011) by smosher on Friday December 13, 2013 @08:45PM (#6904144)
What's your point?
Re:How Class-based Programming Sucks (2011) by coldtea on Saturday December 14, 2013 @02:53PM (#6906851)
My point, which is pretty obvious, is that it "reading like the Blub conceit" doesn't mean anything at all with regards to its correctness.

Might as well say "your argument reads a lot as something that an unbeliever in the flying spaghetti monster would say".

Re:How Class-based Programming Sucks (2011) by colinkahn on Friday December 13, 2013 @06:45PM (#6903571)
What is the blub conceit? Google searched it and got nothing.
Re:How Class-based Programming Sucks (2011) by smosher on Friday December 13, 2013 @06:56PM (#6903626)
The important bit is:

    As long as our hypothetical Blub programmer is looking down the power 
    continuum, he knows he's looking down. Languages less powerful than Blub 
    are obviously less powerful, because they're missing some feature he's used 
    to. But when our hypothetical Blub programmer looks in the other direction, 
    up the power continuum, he doesn't realize he's looking up. What he sees are 
    merely weird languages. He probably considers them about equivalent in power 
    to Blub, but with all this other hairy stuff thrown in as well. Blub is good 
    enough for him, because he thinks in Blub.
From PG's Beating the Averages (http://www.paulgraham.com/avg.html)
Re:How Class-based Programming Sucks (2011) by coldtea on Friday December 13, 2013 @08:14PM (#6904036)
A "my language is more expressive than yours" version of the Sapir–Whorf hypothesis.
Re:How Class-based Programming Sucks (2011) by runT1ME on Friday December 13, 2013 @04:42PM (#6903075)
>The expression problem is much safer solved using virtual classes (using traits and type members) in scala than with case matching.

Why do you think this? I prefer having an open trait that's only inherited by ADTs.

I can get non-exhaustive match warnings and I still 'solve' the expression problem by matching on a superset of my partial functions (that do the matching).

Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @07:59PM (#6903957)
Virtual classes with family polymorphism attacks the problem directly: you can add new variants as well as enhance the super class with new abstract methods: your system is abstract until all variants have implemented the abstract methods of the super class. You can then do cool things like factor all your functionality for X in one layer and Y in another layer, replicating variants in X and Y layers to specify their implementations separately.

When I did this in Scala (using traits and type parameters in a virtual class pattern), I got a lot of pushback from the FP people, who thought this problem couldn't, or more correctly SHOULDN'T, be solved using object-oriented constructs; it was one of the main things they liked to boast about in how FP was better than OOP, and I literally took that away from them. A 10,000 line pattern match was somehow preferable to a file with 20 traits that described how the new operation was performed per variant. Martin even made my pattern illegal via a new type check in the Scala compiler after I left EPFL :)

Re:How Class-based Programming Sucks (2011) by fhd2 on Friday December 13, 2013 @08:23AM (#6900339)
> Class-based programming works great for GUI toolkits.

That and games (and I'd wager simulations in general). The only two domains I'm familiar with which I cannot imagine without OO.

From what I've seen, even UIs and games written in languages without support for OO still use something very close to OO. GTK for instance takes it quite far with GObject.

Re:How Class-based Programming Sucks (2011) by ocharles on Friday December 13, 2013 @09:37AM (#6900647)
I spent some time writing Asteroids in netwire - a functional reactive programming library in Haskell - in my free time over a few evenings. I blogged about this at http://ocharles.org.uk/blog/posts/2013-08-18-asteroids-in-ne... - and I don't think it pretends to be OO. It was a radically different style, and I left feeling fairly convinced that FRP is a fantastic model for realtime interactions
Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @10:03AM (#6900786)
FRP is great until you need to be interactive or switch over collections, then it becomes quite ugly. It can work for small games, like the ones in Courtney's dissertation. But over that? Not until a complete physics engine can be joined with a FRP library.
Re:How Class-based Programming Sucks (2011) by davexunit on Friday December 13, 2013 @01:57PM (#6902187)
FRP can exist happily alongside imperative methods of updating state. This approach is explored in FrTime for Racket. http://cs.brown.edu/~sk/Publications/Papers/Published/ck-frt...
Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @08:05PM (#6903994)
FrTime is not really pure FRP as envisioned by Elliot or Hudak. But then I prefer these impure FRP systems and have designed/implemented one myself called SuperGlue [1] as part of my dissertation.

[1] http://research.microsoft.com/apps/pubs/default.aspx?id=1793...

Re:How Class-based Programming Sucks (2011) by davexunit on Friday December 13, 2013 @10:07PM (#6904397)
I guess I prefer the impure systems then, too. :)
Re:How Class-based Programming Sucks (2011) by MartinCron on Friday December 13, 2013 @12:14PM (#6901621)
Thank you for sharing. I really enjoyed your netwire Asteroids post. I used Asteroids for teaching myself OO concepts many years ago. It's good to know that it's still a valuable teaching tool.
Re:How Class-based Programming Sucks (2011) by bunderbunder on Friday December 13, 2013 @04:00PM (#6902885)
That and enterprise software. As productive as I am with functional programming, I still find it works better to expose object-oriented interfaces to the world at large.

Some of that's admittedly just down to the window dressing. Object-oriented abstraction mechanisms such as interface tend to have a measure of self-documentation built in, to the extent that all the members are named, right down to method parameters. There's no reason you couldn't do something similar with FP, it's just that as far as I can tell nobody's ever made a point of doing so.

Re:How Class-based Programming Sucks (2011) by pjmlp on Friday December 13, 2013 @08:35AM (#6900373)
> OO is a horrible match for compilers, for example.

Actually it is a very good way to create ASTs if you need them to be extendable.

Which is exactly the use case where algebraic data types fail short.

Re:How Class-based Programming Sucks (2011) by emn13 on Friday December 13, 2013 @10:30AM (#6900965)
You want some way of specifying an interface - that doesn't necessarily imply classes. Also, class-based inheritance tends to be a poor fit for interfaces; note for example that C#/Java have their own special-purpose interface mechanics, and even C++ which allows multiple inheritance tends to use implicit template-based interface implementation instead.

Discriminated unions on the other hand are - the name says it - intended for cases where you want to discriminate between multiple possible values. They're by design not intended to hide differences behind an interface; they don't address the same problem at all.

All in all, I think that neither classes nor discriminated unions solve the interface problem (nor were they ever really designed to do so) - that's a different problem.

Re:How Class-based Programming Sucks (2011) by pjmlp on Saturday December 14, 2013 @03:02AM (#6905093)
You can have OO without classes.

I just meant OO in general, not classes specifically.

Re:How Class-based Programming Sucks (2011) by emn13 on Tuesday December 17, 2013 @06:04PM (#6924623)
You can have Objects without classes, certainly - even C had structs, and all functional languages I know of have records or something similar.

It's a question of terminology whether you call that object-oriented. It's certainly part of OO, in any case.

Re:How Class-based Programming Sucks (2011) by qznc on Friday December 13, 2013 @08:40AM (#6900394)
What is so bad about OO for compilers? An AST can naturally be modeled as a class hierarchy. Visitor pattern for AST transformations.
Re:How Class-based Programming Sucks (2011) by rayiner on Friday December 13, 2013 @09:18AM (#6900547)
> What is so bad about OO for compilers?

See my comment about the expression problem in another thread: https://news.ycombinator.com/item?id=6783075, as well as this response: https://news.ycombinator.com/item?id=6784057.

A compiler is a program that consists of lots of different algorithms (operations) that operates on a pretty well-defined set of data (an AST or an IR). Changes to the data representation are rare compared to changes to the operations on that data.

What OO does in a compiler is totally obscure the control flow of each algorithm by spreading the logic out over dozens of classes. You can look at an AST node and see at a glance how it participates in constant folding or code generation, but if you're trying to optimize the constant folding algorithm or the code generator, you've got to follow control flow across dozens of different classes.

Using visitor pattern mitigates this somewhat, because you can have ConstFolder::visitBinaryOperation, ConstFolder::visitFunctionCall, etc, all in one file, but note that visitor pattern is just a really verbose, roundabout way of writing a switch statement! If you add a new type of AST node, you have to add a visitNewAstNode call to the visitor interface, and then go update every class that implements the visitor interface. This is no easier, and a lot more verbose, than simply adding a new variant to an AST ADT then fixing up all the places where the compiler complains that your pattern match is no longer exhaustive.

For a good example, look at how LLVM does instruction simplification: http://llvm.org/docs/doxygen/html/InstructionSimplify_8cpp_s.... A good old switch statement, no "simplify" method in the "Instruction" class nor visitor pattern.

Re:How Class-based Programming Sucks (2011) by seanmcdirmid on Friday December 13, 2013 @10:07AM (#6900815)
Scala, use traits and type variables, the problem is really easy to solve through what I call an open class pattern. See "new age components for old fashioned java" OOPSLA 2001, note I'm using Flatt's term "extensibility problem" which is the same as Wadler's "expression problem" term.

But if you want your control flow to be in place, then a 10,000 line case match ala scalac should be right up your alley. Frankly, I'd rather divide and conquer, which is what OOP is good at.

Re:How Class-based Programming Sucks (2011) by protomyth on Friday December 13, 2013 @11:47AM (#6901449)
"Class-based programming works great for GUI toolkits."

I've programmed with 3 approaches to UI: every things a subclass, instance of class, and prototype-based objects. I enjoyed programming UI on the Newton because of the prototype-based programming. It felt natural. NeXT / Apple's instance of a class works very well too.

Every time I see "every things a subclass", I wince and know its going to be a pain in the butt.

Re:How Class-based Programming Sucks (2011) by wsc981 on Friday December 13, 2013 @05:25PM (#6903255)
That reminds me of a little quote by Aaron Hillegass in his book "Cocoa Programming for Mac OS X" (p. 73):

> "Once upon a time, there was a company Taligent. Taligent was created by IBM and Apple to develop a set of tools and libraries like Cocoa. About the time Taligent reached about the peak of it's mindshare, I met one of its engineers at a trade show. I asked him to create a simple application for me: A window would appear with a button, and when the button was clicked, the words 'Hello World!' would appear in a text field. The engineer created a project and started subclassing madly: subclassing the window and the button and the event handler. The he started generating code: dozens of lines to get the button and text field on to the window. After 45 minutes, I had to leave. The app still did not work. That day, I knew that the company was doomed. A couple of years later, Taligent quietly closed its doors forever. "

Re:How Class-based Programming Sucks (2011) by lmm on Friday December 13, 2013 @03:38PM (#6902767)
Ever use Wicket? It's an utter joy to work with, and the only framework I've found that really shows the good side of Java.
Re:How Class-based Programming Sucks (2011) by protomyth on Friday December 13, 2013 @03:39PM (#6902775)
Cannot say I have. I left Java at the 1.1 version and never made it back.
Re:How Class-based Programming Sucks (2011) by codygman on Friday December 13, 2013 @08:27AM (#6900351)
I believe someone has solved the expression problem in Haskell and other languages.