Live data from Hacker News

Classes Considered Harmful [pdf]

web.cecs.pdx.edu

51–60 of 129 posts

Re: Classes Considered Harmful [pdf]

#51
At least in the cases of C# and Java, classes and interfaces recapitulate a weak form of ML-style functional programming rather an impoverished version of Smalltalk-style object-oriented programming: “Inheritance” from a class corresponds to logical disjunction (and hence is equivalent to a sum type) and “implementation” of an interfaces corresponds to logical conjunction (and hence corresponds to product types). The visitor pattern is a verbose form of pattern matching.

You can recover the strong form of ML-style FP (albeit without the nice type inference) if you limit inheritance to abstract classes (thus, all classes are sealed/final or abstract), mark all fields as readonly/final, and use interfaces for destructuring (which addresses problems 1,2 and 3 under the “Alternatives” section).

As an aside, one of the big issues with “classical” OOP is that it enjoins the programmer to create phenomena using logic (i.e. wizardry) rather than of using math to model a system (i.e. science and engineering). Among other problems, this leads to designs that break symmetry (e.g. if modeling a bucket brigade, the perspective of many humans passing by a single bucket should be just as valid and available as the “natural” perspective of many buckets passing by a single human). Of course, appeals to symmetry is question begging, and I don’t have time to litigate that here, but it sure seems like symmetry is key to building scalable systems (where scalability means not just number of users, but also things like number of edits to a codebase).

In summary: Classical OOP seems to require that you either have a prior (well-designed and debugged) model against which you can program, or really, really good taste.

Re: Classes Considered Harmful [pdf]

#52

Computer science gets a lot of papers with arguments for a certain way of doing things. Very few with controlled experiments to find out if a way of doing things is better than another. Every time I mention this, people say that it is hard to do such experiments. Yes, it is hard to actually know things. while it is very easy to make plausible arguments, for almost anything.

This applies to you, too. What controlled experiments have you performed to test productivity differences of different methods of programming?

Re: Classes Considered Harmful [pdf]

#53
post #41

The practical usefulness of classes in C++ comes not from an "object-orientedness" that they supposedly give the language. It comes from the fact that they are merely a mechanism that can be used to create abstract data types, thereby providing a better encapsulation, and do many other things, rather than a policy saying that every object must be an instance of a class. So, one has to be careful when talking about cl…

The fact that C++ offers classes for building abstractions, does not imply that classes are the best (or even a good) way to do that.

Also, it does not mean that the particular C++ implementation of classes is any good.

Re: Classes Considered Harmful [pdf]

#54
post #50

IMO language should be designed to be more effective, simpler or powerful, not to avoid programers to miss use them. All the "rules" about not using goto for example are legitimate but people forget that sometimes using goto could be a good practice in some languages. A semantic is just a tool you can miss used it`s programer issue not a language/semantic issue. OOP is a way to program among other we can argue that i…

An adage I use is you should know all the rules so you know when and which to break.

Re: Classes Considered Harmful [pdf]

#55
post #41

The practical usefulness of classes in C++ comes not from an "object-orientedness" that they supposedly give the language. It comes from the fact that they are merely a mechanism that can be used to create abstract data types, thereby providing a better encapsulation, and do many other things, rather than a policy saying that every object must be an instance of a class. So, one has to be careful when talking about cl…

Classes tightly couple functions on types with the type declaration itself. It's often not even clear in which class certain methods belong.

Better to separate data types from functions on data types the way it's done in functional languages, and if you still want object-oriented programming, you can support it like Ada does, or as syntactic sugar over the ordinary call syntax like some functional languages do.

Re: Classes Considered Harmful [pdf]

#56
post #32

Earlier quoted context omitted.

What do you use to implement interfaces then? Generic interfaces are the one single feature that made OO popular for GUI programming; what by its turn is the one application that made OO popular. And it's still the most powerful of the OO concepts (what is shame really).

Are you referring to the fact that in Java interfaces are basically pure virtual classes? That always felt like an implementation detail to me. For example, both Go and Rust have interfaces without inheritance.

I'm referring to a pattern on OO languages where you specify an interface using a superclass¹, with hooks that you can specialize on subclasses, reusing most of the code on the superclass and keeping the code around it generic.

Go interfaces have the part about keeping the code around it generic, but I don't think it gets the easy reuse of code by just writing the hooks. (But I can easily be wrong here - I'm not a proficient Go programmer.) In that, it looks very like Java's interfaces (the literal Interface thing).

1 - In Java you would not use a literal Interface for that, since you can't provide default code for the subclasses to extend.

Re: Classes Considered Harmful [pdf]

#57
post #32

Earlier quoted context omitted.

Are you referring to the fact that in Java interfaces are basically pure virtual classes? That always felt like an implementation detail to me. For example, both Go and Rust have interfaces without inheritance.

I'm referring to a pattern on OO languages where you specify an interface using a superclass¹, with hooks that you can specialize on subclasses, reusing most of the code on the superclass and keeping the code around it generic. Go interfaces have the part about keeping the code around it generic, but I don't think it gets the easy reuse of code by just writing the hooks. (But I can easily be wrong here - I'm not a pr…

With just interfaces you can produce a variation of the pattern you describe by using an interface that specifies the hooks, having the concrete types implement the hook interface, and using a proxy that implements the shared behavior by consuming the hook interface.

Re: Classes Considered Harmful [pdf]

#58
post #32

Earlier quoted context omitted.

Are you referring to the fact that in Java interfaces are basically pure virtual classes? That always felt like an implementation detail to me. For example, both Go and Rust have interfaces without inheritance.

I'm referring to a pattern on OO languages where you specify an interface using a superclass¹, with hooks that you can specialize on subclasses, reusing most of the code on the superclass and keeping the code around it generic. Go interfaces have the part about keeping the code around it generic, but I don't think it gets the easy reuse of code by just writing the hooks. (But I can easily be wrong here - I'm not a pr…

Sometimes (hard to say without a specific example) the superclass can be represented as its own class and included in each subclass as composition. This has several advantages, including less code per class and therefore smaller interface surface area, more explicit interface / information hiding, and easier testability.

Re: Classes Considered Harmful [pdf]

#60

Earlier quoted context omitted.

I'm referring to a pattern on OO languages where you specify an interface using a superclass¹, with hooks that you can specialize on subclasses, reusing most of the code on the superclass and keeping the code around it generic. Go interfaces have the part about keeping the code around it generic, but I don't think it gets the easy reuse of code by just writing the hooks. (But I can easily be wrong here - I'm not a pr…

Sometimes (hard to say without a specific example) the superclass can be represented as its own class and included in each subclass as composition. This has several advantages, including less code per class and therefore smaller interface surface area, more explicit interface / information hiding, and easier testability.

The most obvious examples would be the widget classes all GUI frameworks export. Java has some interesting classes for networking, like its AbstractHTTPServer, and Applet. Most OO languages have some variant of Thread or Runnable that is built this way too.
Post reply on HN