Live data from Hacker News

Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

mail-archive.com

21–30 of 177 posts

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#21
I actually like to ask people in interviews to model a car and car factory because I want to see right away if they go deep into the nonsense of extending everything, or if they can use composition, or get away with something focused on maintainable code that meets requirements.

I've seen it all. Prius extends Car extends vehicle extends motor extends ...

Right within the first five minutes of the interview.

But I also think these old "a bike extends a " tutorials were the most frustrating thing in the world and made no sense to me when I was trying to learn OOP in the first place. From my memory, I recall all the examples I could find were absolutely nonsense but blogs were full of posts with lots of non-real-world about connecting web requests things to a DB code and lots of "yeah it's just like a person is-a mammal which is-an animal which...".

It wasn't until I wrote some c++ and looked at how it worked in the heap and stack before I started to understand it better. Then later I learnt about SOLID and discovered that you almost never really want to use extension in practice (at least in the problems I worked on), except for things like interfaces in places you expect (and plan to have, and even better if you already do have) a reason for variations in sub-types that have the same contract, or need to cross system boundaries.

I almost always want to use composition. I will use extensions but this should be really specifically be selected for a few key points.

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#22
The book "Design Patterns: Elements of Reusable Object-Oriented Software" has an initial chapter that provides one of the most concise introductions to OO I've read. Its main focus is on polymorphism and it provides some easy to understand examples.

In fact, if you think of Go as an OO language (which I guess many won't), it has all the things you actually care about from OO. It doesn't have inheritance and you don't explicitly declare that a type implements some interface (). Since there is no inheritance of implementation you are forced to use composition, which is what people tend to end up recommending after a few years of fighting with OO code that has been written by people in love with a Linnean hierarchies and complex approaches to DRY.

() There are tricks to accomplish this, for instance creating a New() function that returns an interface type, which will make the compiler ensure you implement the interface).

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#23
post #17

Earlier quoted context omitted.

The user you responded to uses C#, you can't override arbitrary methods there. Only methods explicity declared as virtual can be overriden, so this is different than many other OO languages. I think this is also the better approach, making inheritance something you have to explicitly design for instead of a way to monkeypatch almost everything.

> Only methods explicity declared as virtual can be overriden You can with "new", no? public class Foo { public void Say() { Console.WriteLine("Foo!"); } public void Think() { Console.WriteLine("I;m thinkin about thos beans"); } public class Bar : Foo { new public void Say() { Console.WriteLine("Actually, Bar!"); } } So with Bar we have inherited Think(), but have overridden Say(). Ok technically the MSDN docs will s…

Using new is still very different from using override, there are real differences in behaviour there. Though I never used "new", it's usually described as a really bad idea unless you know what you're doing, I never saw the need to use this.

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#24
Perhaps this would work: Build a program that...

* That has a hardcoded list of strings baked into it

* Accepts as inputs a filename (but can be empty) and a substring to search for

* At runtime, either (a) (if filename empty) iterates over hardcoded list of strings, or (b) opens file and iterates over lines in it

* For each string / line, if contains search string then output to stdout (and maybe also increment a counter).

It's very artificial but does allow the basic idea of OO to be illustrated and it's far less code than a GUI example. You have a base class and two derived classes that really do have a different mechanism, and even have different state (file handle or integer index). The program logic is also quite decoupled (the class interface is not tied to the fact you're doing string search).

You could solve the problem without classes by reading the whole file into the same list structure as the hardcoded strings, but that aspect is actually quite nice because you can present that first and then ask what to do if you want to avoid that unnecessary memory usage. It's also nice that you're illustrating a really common OO pattern (iterator).

The only slight downside is that some languages make this easy to do without (explicit) classes e.g. Python generator functions. But you can add a footnote that they exist and this is just to illustrate classes - it still beats the abstract examples.

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#25
post #9

Entity-Component-System is so much nicer paradigm than any hierarchy. I wonder why it's not used much outside of gamedev.

That's just structured procedural programming - subroutines mutating separately-defined data. This used to be all the rage before OOP took over!

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#26
post #14
post #10

Earlier quoted context omitted.

DRY is also not the goal.

It's not a million miles from it though. If you start from DRY as an approach to OO design you'll produce something vaguely reasonable and be close enough to where you want to be that you can learn the differences.

I should have been a bit more precise. There are essentialy two ways to approach DRY in OO. One is to inherit implementation, which isn't the best way to go about things since it leads to more brittle code. The other way is to make sensible use of polymorphism and composition.

There are aspects of DRY that are worth pursuing (like reducing the number of places you need to get things right) and some that aren't worth pursuing (thinking you reduce the amount of work by re-using code heavily). For code to be reusable you have to be sure that developers only have to care about its interface. Implementation inheritance often doesn't do that. I've worked on projects with people who were obsessed with DRY and made heavy use of implementation inheritance, and the projects inevitably needed extra work to untangle a brittle mess with lots of surprising behaviors.

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#27
When I first learned OOP from the dry examples like Shape is the base class of Circle and Triangle, and so on … I just didn’t appreciate why that was much better than the flat imperative style of plain old C and retreated back to the comforts of C. Then I started writing larger C programs with structures that had common function pointers called “draw” and “rotate” and realized these are just obj ct methods … and the structures were different implementations of a common base class … OK, so I guess OOP is useful after all.

So it helps to compare the old ways to the new ways side by side, with a real world example, to understand why the new ways are better.

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#28

After many years of OOP, I'm not sure if we gained anything from OOP, and if it wasn't a solution in search for a problem. At first I was enthusiastic. Then I've realized that there might be better ways to solve problems than trying to fit everything in a "everything is an object" mentality. OOP can lead to overly complex code, uneeeded abstractions and design patterns thrown on top of each other for no good reason.…

OOP still rules the GUI. Even if you spread some functional sauce all over it.

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#29
post #17

Earlier quoted context omitted.

The user you responded to uses C#, you can't override arbitrary methods there. Only methods explicity declared as virtual can be overriden, so this is different than many other OO languages. I think this is also the better approach, making inheritance something you have to explicitly design for instead of a way to monkeypatch almost everything.

> Only methods explicity declared as virtual can be overriden You can with "new", no? public class Foo { public void Say() { Console.WriteLine("Foo!"); } public void Think() { Console.WriteLine("I;m thinkin about thos beans"); } public class Bar : Foo { new public void Say() { Console.WriteLine("Actually, Bar!"); } } So with Bar we have inherited Think(), but have overridden Say(). Ok technically the MSDN docs will s…

Isn't that shadowing instead of inheritance ?

Re: Goodbye, shitty Car extends Vehicle object-orientation tutorial (2011)

#30
post #21

I actually like to ask people in interviews to model a car and car factory because I want to see right away if they go deep into the nonsense of extending everything, or if they can use composition, or get away with something focused on maintainable code that meets requirements. I've seen it all. Prius extends Car extends vehicle extends motor extends ... Right within the first five minutes of the interview. But I al…

The problem with that is you might feel pressured to talk about Prius extends Car extends Vehicle etc. even if you wouldn't do that in real code because it seems the question is meant to probe the concept of inheritence so it's natural to answer that way.

It takes a brave interview candidate to say "the best solution depends on the exact nature of the problem - and your problem doesn't make any sense so there's no valid solution".

Post reply on HN