## Inheritance is the Base Class of Evil

I found this talk by Sean Parent to be very informative (and it's 24 minutes long). What do you think?

## Comment viewing options

### Is the wisdom in this talk

Is the wisdom in this talk applicable outside of C++? I can't really tell...

### Yes.

I believe it is, but not necessarily for all programming languages. It's an interesting notion - Inheritance derived from Evil - outside of the specific object oriented language medium used to express the assertion (not all languages employ objects and polymorphism, right?).

Or is it that the C++ part of the equation elicits your skepticism? I'm not surprised that this is the case given the status of C++ in the academic programming language design community, but I question the base of your question. :)

### I mean, is the talk working

I mean, is the talk working around problems in C++ or in OO languages in general. How would the approach be applied to and would benefit C#?

C++ has many self-inflicted problems that do not exist outside of C++. This is why, academically, it is not a very interesting language to study: many of the results you would get from studying/improving C++ do not transfer outside of C++, meaning your community is quite limited. Of course, there are people who care about C++, and the results are still meaningful, and there are academic researchers who do a lot of work for C++ outside of the obvious bug checking work.

C++ isn't alone in this regard. Many of the problems solved in Haskell (a fairly academic language) are due to its purity and often do not exist when purity isn't desired (though of course, purity is a noble goal also, just like C++ efficiency is).

### Watch and listen

You can design that answer yourself after you watch and listen to the talk. You're much more capable than myself to answer this question. Why ask me?

Sometimes it's OK to spend 24 minutes evaluating something, compiling understanding. I know I've posted 70 minute talks in the past, but this is 24 minutes. Grab some popcorn and geek out, man.

### I've actually tried going

I've actually tried going through the video. The question is still valid.

### Indeed.

Indeed it is. That's why you asked it. It's also why I posted a link to a talk titled "Inheritance is the Base Class of Evil" on this forum. Is it true? Is it false? Is it both (true sometimes, false other times. What are those times?).

I don't think the video does the question justice. In fact, I think the speaker is confused on multiple points. Let's start with slide 6:

1. By using inheritance to capture polymorphic use, we shift the burden of polymorphic use to the type implementation, tightly coupling components.

Status: false in general. Besides confusing inheritance with subtyping, an abstract class defines an interface that a type must satisify in order to be used. There are several ways for a type to "implement" that interface; just in C++ you can use multiple inheritance or delegation (adapter pattern), and in Java you can use interfaces.

2. Inheritance implies variable size, which implies heap allocation.

Status: false. First of all, inheritance of _fields_ implies variable size objects--in C++. There are several things mixed up here, not the least of which is that advanced VMs for languages like Java and JavaScript do escape analysis so that capture objects aren't even allocated, on the heap or elsewhere. Second, even in C++ you can stack allocate variable size records and pass them down the stack.

3. Indirection, heap allocation, and virtualization impact performance.

Status: false. I assume by "virtualization" he means virtual dispatch. The speaker might want to look into such techniques as class hierarchy analysis, rapid type analysis, and dynamic optimization of virtual dispatch. In short, he's about 20 years behind the state of the art in his understanding (or C++ compilers are 15 years behind in devirtualization techniques). Second, indirection can often be removed through the big optimization--escape analysis--or by object inlining and colocation. Third, indirection can actually save performance when it is used carefully to move cold fields out of objects and share read-only parts of objects (sometimes actually saving memory!) The above general statement falls apart when one looks closely.

Status: disagree about reference counting. Modern GCs make all forms of reference counting obsolete. Second, what if object lifetime management does indeed lead to garbage collection? Is that bad in and of itself? Maybe for programmers who believe they have such important tasks as to do manual memory management--a common, mistaken belief among C++ programmers.

5. This encourages shared ownership and the proliferation of incidental data structures.

Status: so what? Shared ownership of immutable objects is no problem, it's safe, efficient, and can save memory overall. I'm not sure how inheritance "encourages" incidental data structures...maybe if the language lacks things like proper lexical closures, tuples, or lightweight classes. But that's not an argument against inheritance, it's an argument for those other features.

6. Shared ownership leads to synchronization issues, breaks local reasoning, and further impacts performance.

Status: False. Shared _mutable state_ leads to synchronization issues and breaks local reasoning. It orthogonal to inheritance and represents a deeper design problem. Blaming these problems on a language feature like inheritance results from profound confusion. The last part, that shared ownership further impacts performance is similarly unsupportable--as if CPU caches couldn't share data somehow. They in fact do extremely well when the data shared is not written, especially with shared L2 and L3 caches these days. The presenter is just plain blinkered here.

That's not to say that I think inheritance is the greatest feature to use to accomplish polymorphism. Actually I think parametric polymorphism and abstract data types are often better than inheritance. I have a whole host of other reasons why I blended lighter weight classes, first class functions, tuples, and type parameters into Virgil and why I think that combination works better--but the reasons presented here are basically all completely confused.

Even worse, in the very next slide, his example uses ad hoc polymorphism with function overloading! What the hell? And then he wraps the whole bastard in huge ugly C++ class with C++ fictions like copy and assignment operators, unique_ptr, etc. I seriously can't watch the rest with so many glaring mistakes in the first few minutes.

### You are interpreting each

You are interpreting each statement as a general statement, when they are clearly meant to be interpreted in the context of his example in C++. In that context all these statements are true.

I also feel like you missed the main point of the talk: doing ad hoc polymorphism that you only pay a performance cost for if you actually use it with more than 1 type.

### Disagree

Stumbled across this post - let me address.

1. I'm aware of the difference between inheritance and subtyping, in C++ there is a single mechanism for both called "inheritance". Whether you use multiple inheritance, delegation, or Java interfaces, you are still stating the dependency explicitly in the derived class, coupling your components and tying your object to how it used. To conform to another interface, even if your class already has the correct member functions, you must explicitly inherit from another interface. See some of Rob Pike's talks on the Go Programming Language to see why normative typing causes so much trouble with large systems.

2. Even if you don't inherit data members (fields), if the derived class has any specific data at all then your interface is to a variable sized type.

3. The state of compilers has come a long way but in every language that I've looked at (JavaScript, Java, C++, Objective-C, Lua) indirection, heap allocation, and virtualization have a serious impact on performance.

4. I have yet to use a system with a GC where the GC didn't have an unacceptable impact on performance or where the developer didn't spend more time managing the GC then he would have spent managing memory. But I'm not advocating manually managing memory - I'm advocated value semantics. There is no need for a memory manager because everything is scoped. I still read about GC systems where "with only 3x the memory" they achieve "nearly equivalent" performance. I work on systems where memory = performance. If you cut my memory to 1/3 my performance goes down nearly proportionately. And don't even get me started about the issues with systems that don't have precise object lifetimes. Managing resources is broader than managing memory.

5. I make the point that shared ownership of immutable objects have value semantics (modulo an indirect equality) further in the talk.

6. The mechanism of inheritance encourages designs with shared mutable state. Just look at any "object oriented" framework to see numerous examples. When you refer to every object through a pointer it is a challenge to know when or if it is shared.

As for the flaming about C++ at the end; yes C++ has issues. I can't argue that point (and I make the point in the talk). But constrained non-intrusive polymorphism (a much better term than ad-hoc) is exactly what is achieved (or if you prefer the terms it is structural, rather than nominal, typing). With Concepts (or Concepts Lite) we can certainly do better. Some of this comes directly from the language appendix I wrote with Stroustrup for Elements of Programming.

### invoking the ghosts of BitC

this all sounds like something the BitC folks have, are, and hopefully will continue to delve in to. if i had a talisman that would invoke them here, i'd apply it... :-)

### What I got from sampling Parent's video.

I greatly prefer text to video because I read (even when not skimming) several times faster than anyone can talk. I sampled the video by moving a time slider after listening to the (slow) opening parts. The middle is mostly two demos: one of C++ using STL and another showing UI for Photoshop with undo based on similar ideas. Preliminary slides include an odd definition of polymorphic that seems not worthwhile.

Sean Parent seems to advocate functional programming in C++ by always copying (in STL containers) instead of mutating, thus avoiding shared mutable state problems. He also finds inheritance unnecessary when you can just use STL to maintain collections of content he manipulates. If the video has another significant point, I missed it. A single page of text more than captures the gist, though not the demos.

The video doesn't address 1) what happens when you share common state with others, or 2) how someone extends code other than replacing it with new code. I wrote this up to justify lost time of watching the video, which I would not have watched had I seen a summary like this one.

### Fair enough.

Sorry for not providing a summary/conclusion up front. The idea was to get insights/commentary from this group of domain experts versus sharing something with a "though the linked video is not worth your time to watch, Sean Parent asserts that...".

### Videos are different beasts

Videos are different beasts from papers or blog posts simply because we can't skim them to "get the gist" and decide if a deeper commitment would useful or not. I'm utterly horrified that the blogosphere seems to be moving to videos, but my 20 year old cousin says he actually prefers to consume content in video form vs. reading something...generation gap?

I actually watched the video, Sean Parent kept talking about efficiency, avoiding allocations, memory management, copy constructors, swap constructors, and so on. Things that I just haven't seen before or since the 10 or so years since I've used C++. It seems like this is a great trick if you want polymorphism without indirection or heap allocation...if you care about performance a lot to squeeze out that last few percent without making your code absolutely ugly.

I wonder, however, if this related to Haskell's support for ad hoc polymorphism (which also eschews inheritance), which manages polymorphism statically.

### Maybe videos are a form of conspicuous consumption.

I have insufficient data to guess whether there's a generation gap. My sons don't read for pleasure, but neither did their mother, nor any of my four siblings when I was that age. Maybe nothing's changed.

I'm utterly horrified that the blogosphere seems to be moving to videos ...

I haven't noticed many videos. For the most part I ignore videos, assuming there's no real content, since otherwise a text version would exist. For me to bother requires raving fans of a video, or a speaker I really want to hear, say an icon from a past age. But never yet has a video been worth seeing. The only real extra content present in a video is how a speaker behaves, which doesn't make up for very sparse content at a slow pace, since I find irrelevant what folks think of themselves.

Using C++ STL doesn't avoid allocations, it just moves more management under its control. Since STL favors extrinsic links, it generally has poor performance for anything that must be reached from more than one indexing scheme, such as LRU list and hashmap in a cache. Intrinsic links are essential for speed, so that after finding a thing once, you already have what you need to finish in constant time.

(Edit: Before I respond to replies, let me clarify one sentence. To make points shorter, I put minimal provisos in sentences we can get from surrounding context. When i said But never yet has a video been worth seeing, I meant in the context of replying to Sean, and in the context of topics we discuss on LtU. So rather than all video is bad, my intended meaning is But never yet has a [programming tech] video been worth seeing [for example, about programming languages]. Maybe it was asking too much for folks to assume I meant video relevant to an LtU context.)

### Bret Victor has done well

Bret Victor has done well upping both live (video recorded) presentation levels as well as rich web documents with good writing along with interactive content. I'm working on the latter, I'm not sure I'm good enough for the former.

### Examples of good videos

For me to bother requires raving fans of a video, or a speaker I really want to hear, say an icon from a past age. But never yet has a video been worth seeing. The only real extra content present in a video is how a speaker behaves, which doesn't make up for very sparse content at a slow pace, since I find irrelevant what folks think of themselves.

Examples of good videos are hard to find but sometimes the presenter is an obvious expert in the field and manages to combine technical know-how with an ability to communicate to a lay audience. This is what I consider to be a good video. It's from the BBC which has gone a bit "personality cult" with its popular science/engineering documentaries lately but this one actually works: YouTube video on clockwork

BBC again but old school (early 1980s) --- no "personality cult". This one is quite good on information: YouTube video on helicopters

We need something like the above two done by somebody who knows how to turn data-structures into tv. It's not easy. Here's a BBC documentary on logic and CS which didn't work for me: YouTube video on logic

### Well, if we go outside tech videos...

I bet those are interesting videos, but I receive every Youtube link with the same enthusiasm I feel towards a video described, "This awesome time my aunt stepped on a banana peel." There's a program called (I think) How It's Made I watch irregularly on the science channel when eating or reading fiction, so an appeal to complex machinery is well-placed. Showing how things are assembled, with voice-over explaining detail about how and why, seems like a good use of video, especially when moving parts are shown in action. When subject matter needs to move to get a message across, video ought to excel, and this might apply to demos of dynamic user interfaces.

My sons force me to watch Youtube videos now and then, against my protests, with a low batting average in quality but a sprinkling of gems. Hands down, one of the funniest was the eHarmony cat lady, which resembles a brilliant job of acting after you see it a couple times. Some musicians made an auto-tuned version of it to songify her delivery, which is also brilliant. (I'm guessing "can't hug every cat" might refer to that song.) However, despite being terrific, this is off-topic for LtU.

I might have a suggestion to improve technical videos, inspired by what I see in console games my older son watches, recorded by gamers like Seananners. Build a video on something that must move to get a point across, then inset small frames showing a presenter so their expression contributes, so an audience gets everything at once. Then slow speechifying at least has something worth seeing in video. A video of slides doesn't seem like it uses video bandwidth well.

### nowt

I bet those are interesting videos, but I receive every Youtube link with the same enthusiasm I feel towards a video described, "This awesome time my aunt stepped on a banana peel."

I gather from your posts that you consume a fair amount of fiction. Do you feel the same way about books recommended to you? If not, then I would like to ask you "why not?" since there is plenty of garbage books out there. You're right to be wary and I understand where you're coming from, but are you not missing the nuggets of gold?

To me, that first video, the one on clockwork, was the best documentary I've seen in a long time, maybe ever. I could feel it was going to be good when he pronounced automata correctly. He also nailed the connection between the clockwork mechanic's cams and the coder's programs. I also thought the social history aspect actually added to the content; it wasn't just an attempt to broaden the appeal. Finding videos like this makes all the garbage I've endured worth while.

### Should probably wind up this sub-thread.

(Hmm, first time I've seen "nowt" in print. North English dialect for nothing. When I hear a word sounding like nah-wit in dialog, I never imagine it spelled that way. I'm way more familiar with naught.)

I'm not that interesting, and it's hard for me to reply while addressing topics like programming languages, whether inheritance is evil, the original video, making videos better, value-oriented coding in C++, or minor nuance in STL efficiency. I think posting links to good videos has value because wandering a bit in topics hurts little and encourages people to be more creative than dry remark posts alone.

I viewed the first five minutes of the hour-long clock video, and it looks promising to finish later, though I can tell I'd prefer a transcript. Narration might be sped up by a factor of three. (As fast as possible that's still clearly enunciated would be best. I wish a speaker would assume, "You are about to walk away, so I want to get you to stay by putting the most important points across now.")

I'd consume a lot more new fiction if a vibrant market in science fiction by talented writers still seemed to exist. More writers of Neal Stephenson's ability would be nice. Can I put in an order for a couple dozen? What if I agree to pay \$200 per hardback for novels as good as Cryptonomicon? Because I would. There's a missing market opportunity no one is trying to address, so maybe someone at Amazon or a startup can think about it. I'd be willing to write SF literary criticism if there was a market for that.

Usually if I like an author, I enjoy everything they write, and read their entire works. But a prolific writer often has a series (or sub-genre) I don't like, and I read none of it after sampling. Today there are few professional writers I enjoy. I don't want to insult specific writers, because I'm sure each is a nice person. The market place seems to drive writers in a commercial direction based on channels and appeal to least common denominator. One of my favorite writers no longer seems to write brilliant novels because (I'm guessing) less weird space opera that has a chance of being optioned for film is a better career move. And my favorite author of all time passed in this last year — if you could bring him back to write more novels I'd be ever so grateful.

Edit: here are examples of things I like more than videos:

The Secret Life of Walter Mitty by James Thurber
Slow Tuesday Night by R. A. Lafferty
Harrison Bergeron by Kurt Vonnegut, Jr.
The Moon Moth by Jack Vance

### Video is a virtual experiential medium

It's fine to prefer text (reading) over video (watching, listening). That's simply a personal preference. There is no arguing that text is easier to parse, skimming or not. For example, when exploring new APIs, it makes little sense to watch a video about it, when you can read a sample, copy some code, experiment, learn in your favorite editor...

Video representation of information is more than information: it's an experience. It's entertainment and education. You can read all of Feynman's ideas in print (his papers, his lectures transcribed, his books...), for example, but watching and listening to him is worthwhile, too - it's an entertaining educational experience and you get to know him, his quirks, see how his mind works, and see/hear his passion in action.

There are countless examples of lectures or conversations or panels in video form (even when you just listen versus watch and listen) that are well worth your time. Sometimes, the person behind an idea or a technology or a philosophy or a programming language :-) is just as interesting as the topic unfolding in the video that captures the person, too. Close to home (I filmed many of these myself...), watching Erik Meijer and Lars Bak geek out at a whiteboard or Anders Hejlsberg and Gilad Bracha debating type systems or...., is so worth the time - it's education and entertainment. Now, I'm not saying reading is not entertaining... I'm saying that it's fine to watch a human share what he or she thinks/does/wants in "person", that is, as if you were there, watching and listening in real time.

Video has it's place, just as text does. They are just different mediums with pros and cons (you can't read when focusing on something else (like the road, while driving) but you can listen). You can play a video in the background while writing code on another monitor or, actually, while reading.

Sometimes it makes sense to read. Sometimes watching video is the best way to experience learning something new. Obviously, you're not arguing that this isn't the case. I'm just stating a position on why video matters and will continue to matter, as it is no more or less than an experiential medium with a tendency to entertain and educate simultaneously in the almost-real (humans talking, coding, presenting, moving, debating, engaging).

OR is tyranny. AND is happiness. (This is a way of saying it doesn't *have* to be either or. And is just fine. Maybe Or is the base class of evil (in some contexts, just like inheritance) :-)

### video works best when coupled with other media

Video is definitely appealing for the YouTube generation, but I like to consume my talking heads in person. In Sean's case, I wish the message had companion notes in the style of what Bret Victor does with his talks, but this might be appropriate only for strong messages.

BTW, Splash is in Portland again for 2014, and there is a good reason you should go :)

### Splash, Channel 9, Me, Video and Text coupling

Sean, I'm no longer on the Channel 9 team, but if I am still at Microsoft when Splash happens, I'd gladly come down with my trusty camera and some conversation kindling.

I've asked Sean if he plans to write about the topic of Inheritance and its relationship to Evil.

PS: The coupling of video and text is certainly an effective way to ensure that the core information you want to convey is expressed in a maximally reachable way (Readers and Watchers are equally satisfied and educated). My assertion was that video is a powerful medium for education and enlightenment, just like text, but with an arguably higher potential for unexpected entertainment (who would of thought that Feynman had a such deep Brooklyn accent when reading his words?). Sorry for being pedantic. Remember, I'm on the lower side of the IQ range of this excellent community...

### More detailed coverage of Sean's points

Sean has more detailed (and longer) video treatment of the topic "Value Semantics and Concept-based Polymorphism" here. This includes a PDF (slides) and source code. Of course, if you're not interested in C++, then you should not spend time watching/reading. If, on the other hand, modern C++ concepts and implementation details are interesting to you, then it's probably worth your time. Sean's abstract for his full presentation on the topic: This talk explains why (and how) to implement polymorphism without inheritance in C++. The talk contains many C++ tips and techniques, including many new features from C++11. During the course of that talk a key feature from Photoshop will be demonstrated and implemented.

### Tag soup

Would you mind replacing </a> with </i> in this comment? I'm using italics against my will here, help!

### Not any more you don't!

Not any more you don't!

### I don't follow

I italicized the abstract text and linked "here". What did I do wrong?

### You probably forgot to close

You probably forgot to close the i tag, and Drupal fixed it for you. Only not correctly...

### Ah...

I see. Thanks for fixing it, Ehud! (I assume you did since it's closed properly now.)