2
$\begingroup$

Linus Torvalds has famously attacked the object-oriented language C++, but he didn't offer many specifics about why, besides saying C++ uses "inefficient abstracted programming models". What exactly are the strongest criticisms of object-oriented languages? Are they "inefficient"?

$\endgroup$
5
  • 1
    $\begingroup$ This sounds like more of a question for Software Engineering to me. I don't understand what the word "exactly" adds to your question. Would the question be the same without that word? Which criticisms are strongest might be a matter of opinion. $\endgroup$ Commented Jan 19 at 6:57
  • 2
    $\begingroup$ I am confused what this has to do with object-orientation. The linked thread talks about the quality of C++ compilers, the cost of C++'s abstractions, the fact that Boost and STL claim to be portable and stable but aren't, and many other, mostly implementation-related concerns. There is nothing in there about OO, except a small section at the very end, where it is mentioned that the Linux kernel does actually object-orientation. $\endgroup$ Commented Jan 19 at 8:33
  • $\begingroup$ The page you linked says nothing about OOP. It's purely an attack on the kind of people typically attracted by C++ and its popular libraries. $\endgroup$ Commented Jan 19 at 8:34
  • $\begingroup$ Since this question is concerned with PL design, it might be a better fit for the programming language SE. $\endgroup$ Commented Jan 19 at 14:16
  • $\begingroup$ Cross-posted: cs.stackexchange.com/q/171056/755, langdev.stackexchange.com/q/4276/917. Please do not post the same question on multiple sites. $\endgroup$ Commented Jan 20 at 18:04

2 Answers 2

6
$\begingroup$

I think Torvalds attacks C++ on two grounds.

One is that the language is over-complicated and that the subset of C++ which is appropriate for Torvalds' use (as a programmer of computer operating systems), is roughly that part which is common with the C language.

The other is that the C++ language attracts OOP fanatics, who in Torvalds' view are unsuited to programming computer operating systems.

These two prongs do not necessarily overlap.

C++ is not exclusively an OO language, but incorporates a large number of different features which have arisen over a long period of time from no single reigning philosophy, and the integration of which has always occurred within legacy constraints (never settled from a clean slate).

Many of these features are abstractions which have not been designed with OS programmers in mind, whose exact workings can be difficult to grasp by paper-and-pencil methods, and which can become leaky or inefficient under circumstances which are difficult to anticipate.

The overall complexity has become such that the whole C++ language is now widely regarded as fathomless to any one developer. This doesn't do when software designs require a high degree of control - that is, where a design does not decompose particularly well into independent parts or layers but requires extensive integration and all-round harmony (typically down to the hardware level for OSes), and for every developer to be able to engage with the whole design.

So Torvalds argues that he doesn't get much bang for his buck from using C++, because everything extra it has over C are all the parts which are likely to end up excluded from use.

By contrast, C was designed coherently by one man Dennis Ritchie, it was designed specifically for OS programming and to be non-abstract, and has been extended more conservatively than C++ in the years since it first emerged. Indeed, C has changed more conservatively perhaps because C++ has acted as a lightning rod for clamour for additional language complexity.

The other thing is the culture and philosophy of the people that Torvalds would have to deal with, if C++ were in use and these people were attracted to try and work with Torvalds or work on Linux development.

Torvalds is explicit that he sees the use of C as a natural deterrent to the involvement of these people - presumably because, again, C++ acts as a lightning rod for them, and the preclusion of any use of C++ means they see little or no hope of indulging in their preferred manner of design and programming which Torvalds opposes.

It can be difficult to fully characterise the people in question but they are associated with a certain style of doing object-oriented programming, and they are often proficient with no other style or with any non-OO language.

I don't think Torvalds is necessarily criticising OO languages in general and for all possible purposes, or criticising C++ merely because it has classes and OO features, as much as he is criticising certain practitioners who follow the style to which I've referred (and presumably, the extent to which they will seek to become involved and impose that style of development).

$\endgroup$
5
  • 1
    $\begingroup$ Some people have competitions “how many patterns can I stuff into my design”. You don’t want them near OS code. Actually not near any code. But that has nothing to do with OO programming. $\endgroup$ Commented Jan 20 at 8:03
  • $\begingroup$ @gnasher729, well it seems OOP forms fertile ground for them, so that is part of the alleged association. And there is often a question, too, of how near you want any OO and OS code. I think the main thing is that OOP intrinsically consists of long and complex patterns of code (on standard hardware, anyway), and that it tends to facilitate certain conceptualisations amongst its programmers that are not generally regarded as well-suited to having direct contact and working effectively with computer hardware (as opposed to working within the Matrix-like walled garden of the "language"). $\endgroup$ Commented Jan 20 at 12:05
  • $\begingroup$ @Steve: I'm not sure I buy that. In fact, one of the arguments the proponents of using C++ for the Linux kernel make is that OO would make the kernel "better", and Linus Torvalds' response is not what you would expect at first glance: he argues that the Linux kernel already is heavily object-oriented, and that this works just fine using C, no C++ needed. If I remember correctly, the object-oriented rewrite of the filesystem subsystems as well as the introduction of the core kobject subsystem where some of the big refactors in Linux during the 2000s and 2010s. $\endgroup$ Commented Jan 20 at 18:06
  • $\begingroup$ IOW, Linus Torvalds' stance on OO in the kernel is not that it's a bad idea but rather that it's such a good idea that they're already doing it without needing C++. $\endgroup$ Commented Jan 20 at 18:08
  • $\begingroup$ @JörgWMittag, it's the same with Microsoft Windows (at least in the old days) - plenty of parts with arguably OO concepts, no use of any OO "language" or compiler (instead assembly and C). This is why I don't go any further than referring to practitioners with a "certain style" of practice, because it can be very difficult to demarcate. $\endgroup$ Commented Jan 20 at 22:20
2
$\begingroup$

One of the fundamental principles of Object-Oriented Data Abstraction is what William R. Cook calls Autognosis (self-knowledge), meaning that objects only know their own internals, not any other object's.

This is different from Abstract Data Types, where objects know their own internals and the internals of every other instance of the ADT.

Since an object only knows its own internals, it is impossible to implement what Cook calls Complex Operations, i.e., operations that require knowledge of the internals of more than one object. A classic example is concatenating two doubly-linked lists in constant time: if you know the internals of both lists, you can just point the next pointer of the tail of list A to the head of list B and the prev pointer of the head of list B to the tail of list A.

However, this is not possible in OO, since the operation must be performed by some object, and that object is either list A, in which case it does not have access to the prev pointer of list B, or it is list B, which does not have access to the next pointer of list A, or it is some other object which does not have to access to either pointer.

So, this is definitely a limitation of OO: complex operations (in the meaning defined by Cook) cannot be performed using objects.

On the other hand, because instances of ADTs know the internals of other instances, there can only ever be a single implementation of an ADT in a system at any given time. Whereas with objects, there can be many implementations of the same object type in the same system at the same time.

This is something that cannot be done with ADTs.

Daniel H.H. Ingalls proposed a test for whether a system is object-oriented or not, based on this, which others have named the Ingalls Test: can you create your own implementation of the Integer protocol and pass those objects to the system's builtin graphics driver to draw a rectangle on screen, while at the same time, all the other code in the system still uses the standard Integers to do the same?

$\endgroup$
3
  • 2
    $\begingroup$ I feel as though this answer exemplifies the kind of practitioner Torvalds wants to avoid. The possession of "knowledge" is attributed to programming concepts rather than to the developers which use them. There is an immediate intention to discuss "abstractions" rather than be anchored to realities. Integers in this world can be "reimplemented", when everybody knows that reimplementation of a native integer means changing the CPU - and just to even have this conversation, I have to shift from talking about integers, to making a distinction between abstract integers and native integers. (1/2) $\endgroup$ Commented Jan 21 at 0:55
  • 1
    $\begingroup$ It's like the problem in law with the "freeman on the land" litigant. A judge can be stopped in his tracks because he can't even work out what hymn sheet the guy is on, and the guy's talking English and referring ostensibly to law, but nothing is quite as we know it. What Torvalds seems to suspect is that these kinds of bamboozling encounters (which require skill and experience to handle deftly, if the encounter occurs) are less likely to occur at all when the use of C is mandated. (2/2) $\endgroup$ Commented Jan 21 at 0:58
  • $\begingroup$ William R. Cook's definition is one thing... But in practice, many mainstream languages that are commonly considered OO have objects that can access the internals of other instances of the same class. Also, I doubt Torvalds was thinking about any of this. $\endgroup$ Commented Jan 21 at 16:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.