r/ProgrammerHumor • • May 16 '21

StackOverflow in a nutshell.

Post image
14.8k Upvotes

675 comments sorted by

View all comments

Show parent comments

43

u/individual_throwaway May 16 '21

I am almost certain that in any given field, there is a subject that nobody really understands.

In physics, it's quantum mechanics. In programming, inheritance might be a hot contender, together with regular expressions.

20

u/superluminary May 16 '21

It’s a funny joke. But there are some very real difficulties with classical inheritance, the main one being “what is a class”? If a class if an object, then it must necessarily have a class of it’s own, so what’s the class of Class? Logically, the class of Class is Class, so you have a circular inheritance loop at the top of your hierarchy.

Alternately you can say that a class is not an object, it is syntax. This creates a whole other set of problems. Where do you put your static methods? How do you manipulate classes? You need a whole introspection API.

JavaScript sidesteps all these issues with prototypical inheritance, which is way simpler.

1

u/gilbes May 17 '21

A class is not an object.

Classes are used to generate types. In a reflective environment, there may be an API to get information about the class used to generate types, but that API will give you a type that represents the class, not the class itself.

Where do you put your static methods?

Maybe you don't understand the point of classes. It is a way of organizing code within a language. A class is a unit of code organization. Your static methods are put within that class unit to organize it.

How do you manipulate classes?

By editing the code.

1

u/superluminary May 17 '21

A class is not an object

Depends on your language. In Java, a class is syntax. It’s a logical construct for creating objects. We use reflection to look inside it. It’s fine, given you’re willing to live inside those restrictions.

In Ruby, a class is very much an object. All objects have a class, so the class of a Class is Class. This means you don’t need reflection, you can modify classes programmatically with standard code. All functions are methods defined in a class, so we have the question, how to implement static methods that belong to the class itself? We can’t put them on the Class class. Ruby gets around this with Eigenclasses, little secret classes specifically for holding static methods that sit above the class on the hierarchy.

In JavaScript a class is simply a constructor function. You call it and it gives you an object. Inheritance is directly from object to object. The type system is a separate construct, typically provided by TypeScript. This gives us several advantages. Classes are just objects, so you can do what you like with them.

Maybe you don’t understand the point of classes.

This is funny. I’m not going to pull a “do you know who I am,” but trust me, I understand the point of classes.

A class typically does three things. It groups functions with data; it acts as an object factory; and in some languages it is your type system. We talk about concrete vs. abstract. The implementation of classes varies dramatically between languages though.

→ More replies (1)