Live data from Hacker News

Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

lists.canonical.org

91–100 of 134 posts

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#91
post #42
post #7

Heh, I agree somewhat that the Duck>Animal is a bit annoying.. but this: Here’s an example that I think would be better to use instead: the `Visible` hierarchy in [Pygmusic][], which is a kind of software drum machine. A `Timer` is a horizontal strip on the screen with a stripe racing across it. A `NumericHalo` is a spreading ripple on the screen that fades. A `Sound` is a thing on the screen that makes a sound when…

Probably if you have a trivial piece of code, you don't need inheritance to help you structure it. But you're right that we need a smaller example than the entire class hierarchy of Pygmusic. What do you think would be the smallest sensible example? If your objective is only to explain what inheritance is rather than why someone would use it, you could probably write two classes and three methods: class A: def z(self…

Device drivers. You have a base class, device, which supports at the minimum, read() and write(). Then you subclass to block and character devices, and so from there. It's a real world example (Unix does it, although in C) and it's simple enough to explain the concepts.

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#92
post #63

Earlier quoted context omitted.

A lot of OO was, in fact, influenced by the task of writing programs for simulations. It's conceivable someone might actually want to develop animal/bird/duck classes (e.g., SimAnimals). http://en.wikipedia.org/wiki/Simula

Yes, I know. That's where the idea came from. Nevertheless, it's a terrible introduction to OO. It promotes very wrong conceptualizations and horrible code. And odds are, even if that is the type of code you're writing, you still shouldn't be trying to have a one-to-one mapping between physical things and classes and/or instances. It just isn't a very good way to work. Just because it was the first thing that was don…

Simula67, the version most identify as Simula proper, actually evolved concurrently with Smalltalk.

Rick DeNatale's memoir has a great article debunking some early OO myths -- http://talklikeaduck.denhaven2.com/2006/07/29/about-me

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#93
post #42
post #7

Heh, I agree somewhat that the Duck>Animal is a bit annoying.. but this: Here’s an example that I think would be better to use instead: the `Visible` hierarchy in [Pygmusic][], which is a kind of software drum machine. A `Timer` is a horizontal strip on the screen with a stripe racing across it. A `NumericHalo` is a spreading ripple on the screen that fades. A `Sound` is a thing on the screen that makes a sound when…

Probably if you have a trivial piece of code, you don't need inheritance to help you structure it. But you're right that we need a smaller example than the entire class hierarchy of Pygmusic. What do you think would be the smallest sensible example? If your objective is only to explain what inheritance is rather than why someone would use it, you could probably write two classes and three methods: class A: def z(self…

irc bots. Or chat bots in general since kids are not familiar with irc nowadays.

It's an amazingly useful teaching tool, since it's real enough to understand it and fake enough that you don't care about taking the bits apart and replacing them. Also, it has a lot of points where you can go and improve the design (extract interfaces, get away with inheritance and use delegation, then get away with objects, then end up with a lisp interpreter etc etc).

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#94
I think the best introduction to object oriented design is shown in the first chapter of Martin Fowler's book "Refactoring". He gives the example of a DVD rental shop that has to calculate the rental fee based on the type of DVD: - A New Release is $3 per day - A Children's DVD is $1.50 for three days, then $1.50 per day after that - A Standard DVD is $2 for two days, then $1.50 per day after that

http://www.amazon.com/Refactoring-Improving-Design-Existing-...

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#96
I've been working on writing a programming book for quite some time now, and I can tell you that coming up with good examples is really hard.

For me, a good example meets as many as possible of the following demands (in no particular order).

An example should

* demonstrate the feature or concept under discussion

* be short - shouldn't be more than half a page to a page

* solve a problem that the reader can easily understand

* solve an interesting problem

* not use unrelated features of the language or library that haven't been introduced yet

* should avoid complexity unrelated to the feature or concept under discussion

* should be as close as possible to what one would use in "serious" (ie non-teaching) programming

I found that i spent about as much time searching for good examples as actually writing text or code for the book. I'm pretty happy with some of the examples (for example the chapter on grammars parses JSON, which is rather real-world, but not too complicated), some of them still fail most of the demands given above.

(If you're curious, grab the latest PDF from https://github.com/perl6/book/downloads -- it's still very much a work in progress).

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#97
post #43
post #38

Earlier quoted context omitted.

I think the point is more that the analogy is so misleading, that it hurts the student's understanding. A similar bad example would be using familiar round objects to teach equivalence: there are 10 apples, and 10 oranges, so apples == oranges. Car's don't extend vehicles, and duck's don't extend birds. Just because we can organize these physical things into conceptual hierarchies based on their functionality, doesn'…

There is a direct mapping between our natural construction of ontologies (we are pattern-detecting creatures, we naturally see commonalities across the birds) and why object orientation works. Cars are a subset of vehicles in most peoples' minds; and Buttons are a subset of Controls in most UI programmers' minds. And the topologies of these ontologies are similar enough, by design , that you can indeed teach valuable…

Object-orientation does not work because cars are a subset of vehicles. Please do not teach anyone that.

Object-orientation works because in some programs, cars can be treated as if they were a vehicle, without knowing what kind of vehicle it is. This is the principle of substitutability, and it's the actual reason that inheritance hierarchies work. But in other programs, cars and other vehicles may not have much in common and should not be part of an inheritance hierarchy!

The real-world ontology is not a reason in and of itself to create an inheritance relationship.

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#98
post #8

Earlier quoted context omitted.

If it teaches people to think of object-oriented programming as world-modeling, it is not a great lesson. Polymorphism is an abstraction technique whose goal is substitutability . "Car extends Vehicle" is useful if other kinds of Vehicles can be substituted for cars. If you teach someone that there is value in making Car extend Vehicle just because that expresses a real-world relationship, you are teaching exactly th…

Yes, you're right, it's only a great lesson if it's taught properly. I mean, really? Doesn't that seem like hair-splitting to you? In my opinion, dickering about this is the equivalent of, "Yeah, those two hortizontal parallel lines are a great symbol for equality as far as it goes, but really students need to understand the difference between equivalence and implication!" It's an equals sign. Same deal here. It's an…

The thing is, people are constantly misunderstanding this point (and attempting to mislead others about it). For example: http://news.ycombinator.com/item?id=2914868

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#99
post #6

> You can’t add code to ducks. You do if you are creating a piece of software to track ducks. I don't get the impression that the person who wrote this ever had to seriously teach software development to software developers. Simplistic metaphors are used so frequently for this kind of thing because it prevents the learner from having to ascend more than one conceptual hurdle at a time. The alternative that he's propo…

You can explain is-a using examples that actually make sense to model using inheritance, like a set of game objects that have an interface like: class GameObject { public: // Called every frame to update the object's logical state. void Update(GameState* state); // Called to draw the object. Draw(GraphicsContext* context); } class EnemySpaceship : public GameObject { /* ... */ }; class SpaceDebris : public GameObject…

This is really not a good example as games have (read should have) entity systems. See e.g. these two links for an introduction. http://t-machine.org/index.php/2007/09/03/entity-systems-are... http://www.purplepwny.com/blog/?p=215

There should be no Draw() in your game object (read entity) base class. Not every entity is rendered. You should have instead a component Renderable. And if it happens to be so that your special snowflake has a Renderable component then you can render it on screen. E.g. AI pathing nodes would not usually have one unless you are debugging them you can add one.

In your example you end up with Movable extending GameObject, Camera extending GameObject and then you have no way of combining these two behaviours.

Re: Goodbye, shitty "Car extends Vehicle" object-orientation tutorial

#100
post #82

One of the first examples of OOP I encountered was the old 'shape' hierarchy demonstration: 'shape' is a base class. 'rectangle' is a new class that inherits from shape. 'square' inherits from 'rectangle', etc. It demonstrates the ".. is a .." relationship that inheritance describes perfectly. This is a pretty common way to introduce OOP as it draws on stuff we all know intuitively. The example proposed with a bunch…

> It demonstrates the ".. is a .." relationship that inheritance describes perfectly.

It's rather imperfect, see the http://en.wikipedia.org/wiki/Circle-ellipse_problem for caveats.

Post reply on HN