Live data from Hacker News

Classes Considered Harmful [pdf]

web.cecs.pdx.edu

71–80 of 129 posts

Re: Classes Considered Harmful [pdf]

#71

I can buy a language without classes if it gives me these things: * Interfaces * Records * Abstract data types * All the above should use foo.bar() syntax Classes provide those things, which is good, but also provide implementation inheritance, which is bad.

>> but also provide implementation inheritance, which is bad.

Inheritance isn't bad.

Abusing inheritance is bad.

For the record, you can also abuse composition, polymorphism, operator overloading, free functions, interfaces, records, and abstract data types. I've abused them all and seen them abused as well.

Inheritance is just another tool. Like all tools, we are expected to use them wisely.

Re: Classes Considered Harmful [pdf]

#72
post #42

There seems to be a common fixation on objects "modeling the real world" with the counter argument for classes being that they don't model the real world. True, there isn't a physical chair class in real life(philosophically, there aren't even chairs), but there is a common abstract idea of what would describe the function and attributes of a chair. Granted, it differs a little from person to person, but objects them…

>There seems to be a common fixation on objects "modeling the real world" with the counter argument for classes being that they don't model the real world. I'm not sure why this is the case. Just the other day someone posted a link to a video about Abstract Algebra. In the video they defined "groups" that (to me) are synonymous with Classes in OOP.

My first exposure to abstract algebra is ultimately what made me become a programmer. I had dabbled with it before, and even done the first year papers, but none of it really stuck with me. After reading about algebraic structures it all started to make sense.

Re: Classes Considered Harmful [pdf]

#73
"I find OOP methodologically wrong. It starts with classes. It is as if mathematicians would start with axioms. You do not start with axioms - you start with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You end with axioms. The same thing is true in programming: you have to start with interesting algorithms. Only when you understand them well, can you come up with an interface that will let them work."

A. Stepanov (http://www.stlport.org/resources/StepanovUSA.html)

Re: Classes Considered Harmful [pdf]

#74

I can buy a language without classes if it gives me these things: * Interfaces * Records * Abstract data types * All the above should use foo.bar() syntax Classes provide those things, which is good, but also provide implementation inheritance, which is bad.

>> but also provide implementation inheritance, which is bad. Inheritance isn't bad. Abusing inheritance is bad. For the record, you can also abuse composition, polymorphism, operator overloading, free functions, interfaces, records, and abstract data types. I've abused them all and seen them abused as well. Inheritance is just another tool. Like all tools, we are expected to use them wisely.

[deleted]

Re: Classes Considered Harmful [pdf]

#75

I can buy a language without classes if it gives me these things: * Interfaces * Records * Abstract data types * All the above should use foo.bar() syntax Classes provide those things, which is good, but also provide implementation inheritance, which is bad.

>> but also provide implementation inheritance, which is bad. Inheritance isn't bad. Abusing inheritance is bad. For the record, you can also abuse composition, polymorphism, operator overloading, free functions, interfaces, records, and abstract data types. I've abused them all and seen them abused as well. Inheritance is just another tool. Like all tools, we are expected to use them wisely.

Do you know any situations where implementation inheritance is the best tool?

Re: Classes Considered Harmful [pdf]

#76

"I find OOP methodologically wrong. It starts with classes. It is as if mathematicians would start with axioms. You do not start with axioms - you start with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You end with axioms. The same thing is true in programming: you have to start with interesting algorithms. Only when you understand them well, can you come up with an interf…

As a side note, this is a very true statement about mathematics, which, unfortunately, doesn't hold for a plethora of texts (from elementary to the graduate level) which do, in fact, start with axioms.

To that end, even starting with definitions is often harmful when the definitions themselves are so often tailored to make a particular proof work, and make no sense without such context.

Re: Classes Considered Harmful [pdf]

#77
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.

With a few exceptions, functions and operators are allowed, in C++, to exist outside a class, which addresses your (valid) concern.

Re: Classes Considered Harmful [pdf]

#78
post #48

Earlier quoted context omitted.

There really isn't a direct analog in Rust. The best approximation in Rust is actually a combination of constructs in the language: structs and traits. Structs are almost exactly like the C-objects of the same name: they define layout in memory for a data structure with discrete types as fields, and there are various packing options available. However in addition to this a struct-specific implementation can exist whi…

You say traits are a rough analogue for C++ base classes with virtual methods. What are the fundamental differences?

I'm not familiar enough with the internals of how Rust handles v-tables and the like in light of its other features to answer that competently.

In practice, one defines an interface in C++ by having a (hopefully) stateless class with pure virtual method declarations, and then classes derived from this class must implement these methods (in order to instantiate them anyway). In Rust one defines a data structure (either a struct or enum) and then, separately, writes the "impl" for it for a trait.

Re: Classes Considered Harmful [pdf]

#79
I write most of my code in Python. I think that's give me a leg to stand on when it comes to discussing OOP.

I find that classes, if nothing else, are a fantastic way to namespace data and methods that operate on said data. For instance, if I have a chair object, I could use a class to describe the chair. I could store the materials it's made out of, the height of the seat, whether it is an office chair, a barstool, or something else. I could also write methods that can say, price the chair based on data about the chair.

In this particular case, I wouldn't subclass the chair at all. There's no point. Every time I need a chair, I would just create a new instance of Chair. That's why chair is an object. It's a self contained thing that I can pass around and pull data out of or mutate.

I don't really use deep inheritance at all in my code. I think the deepest I've ever subclassed something is once. I find inheritance to be useful if I need to be able to create multiple objects that deal with very different things, but perform similar operations on them. A good example of this is Django class based views(it's own controversial topic). I created my own resource class to use for handling API specific requests. The methods of pulling filters out of the query string and serializing database objects are exactly the same for every single request, but the objects that these classes operate on are vastly different. Therefore, I need to specify a serialization schema for each database object type, but I don't necessarily need to rewrite the function that does the serialization. I use my Resource class in order to more easily re-use these generic methods.

Just my two cents.

Re: Classes Considered Harmful [pdf]

#80

Having used C++, Java, C#, Python, Ruby, NewtonScript, Dylan, Scheme, and various other languages "in anger" I can say that in general I agree with the author's frustrations. My favorite (as in most flexible, least boilerplate) approaches to object-oriented programming have been: - NewtonScript's frames with prototype inheritance. Allows "objects" to consist of very small data structures that point to ROM objects and…

In addition to Dylan, Julia also allows multiple dispatch via generic functions.

I have found this approach to multiple dispatch leads to code that's much easier to reason about and maintain. My use case is a tensor library where the tensors have a single interface but various storage backend types, which 'interact' with each other. It doesn't make sense to define these interactions through methods belonging to only one class or the other.

Post reply on HN