Criticism of C++

Started by
143 comments, last by Ryan_001 8 years, 3 months ago
Advertisement


think 'C++' has many flaws. One of them is the functions.

Well, it's an interesting criticism, but I think it's a little off base for this topic.

The most fundamental construct of the C programming language is its use of functions parameterized on pass-by-value arguments. Using pass-by-value instead of some of the more complex methods used by competing languages at the time (pass-by-descriptor in FORTRAN, pass-by-reference in Algol, functional programming in LISP) allowed a C language compiler to be very small, very fast, and very portable and made most of the programs written in it smaller, faster, and more portable too. Hence its long-term continuing success over its rivals over the past 40-odd years.

The rather verbose post above criticizes the pass-by-value machanism in favour of a pass-by-name architecture. Fair enough: there are a number of ways of passing parameters into subroutines, and the designers of the languages are usually aware of the tradeoffs involved when choose which mechanism(s) to specify. Perhaps in alternate universes we all use Modula-3 to write complex systems (except for MacOS, which uses Oberon). Fact is, pass-by-value is Good Enough and easier to get right when writing a compiler, even if sometimes it can cause performace problems when not used correctly.

Stephen M. Webb
Professional Free Software Developer


think 'C++' has many flaws. One of them is the functions.

Well, it's an interesting criticism, but I think it's a little off base for this topic.

The most fundamental construct of the C programming language is its use of functions parameterized on pass-by-value arguments. Using pass-by-value instead of some of the more complex methods used by competing languages at the time (pass-by-descriptor in FORTRAN, pass-by-reference in Algol, functional programming in LISP) allowed a C language compiler to be very small, very fast, and very portable and made most of the programs written in it smaller, faster, and more portable too. Hence its long-term continuing success over its rivals over the past 40-odd years.

The rather verbose post above criticizes the pass-by-value machanism in favour of a pass-by-name architecture. Fair enough: there are a number of ways of passing parameters into subroutines, and the designers of the languages are usually aware of the tradeoffs involved when choose which mechanism(s) to specify. Perhaps in alternate universes we all use Modula-3 to write complex systems (except for MacOS, which uses Oberon). Fact is, pass-by-value is Good Enough and easier to get right when writing a compiler, even if sometimes it can cause performace problems when not used correctly.

Here is not about that - in every case it's the same. I mean the function called expects the calle to store some variables on the stack which locations are well-known (just like any normal variables). I'm not proposing to change pass-by-value mechanism. Instead what I am proposing is the ability to modify function arguments and return value the same way as any normal variable. Not in the limited way we can only do now - by simply copy-initalizing. This will eventually allow the creation of faster, optimized code on some old compilers and also will give the programmer a better idea of what he is actually doing. Although 'C' (and so 'C++' partially) have some bigger issues to deal with.

I'm not proposing to change pass-by-value mechanism. Instead what I am proposing is the ability to modify function arguments and return value the same way as any normal variable.

You just gave the definition of pass-by-name. You're proposing to use pass-by-name instead of pass-by-value.

Algol used pass-by-name.

Turns out it can be really hard to write good code using pass-by-name. Consider this classic snippet of Algol code.
 begin integer i;
        integer procedure sum(i, j);
            integer i, j;
                comment parameters passed by name;
            begin integer sm; sm := 0;
                for i := 1 step 1 until 100 do sm := sm + j;
                sum := sm
            end;
        print(sum(i, i*10 ))
Because the parameter "j" is "i+10" every time i gets incremented, so does j.

Stephen M. Webb
Professional Free Software Developer

Pass by value is the meaning of life.

"Recursion is the first step towards madness." - "Skegg?ld, Skálm?ld, Skildir ro Klofnir!"
Direct3D 12 quick reference: https://github.com/alessiot89/D3D12QuickRef/

Just a bit off topic, but I'd like to make a criticism of the Jonathon Blow video that was posted a few pages back.

What I like

I like his point about exceptions. Despite their popularity in many programming languages, the way they work is fairly non-intuitive, and I think Go made the right decision with allowing functions to return multiple values as the primary means of error reporting; it makes error handling clean, while at the same time hard to ignore. However, I believe there's still a case for having "loud" errors than may be recovered from at some level, even if that "recovery" is just to halt with an error message generated from one point in the code base.

I also agree that being able to do low-level manipulations with a high degree of confidence is a very good thing, and that programmer morale is an important aspect of any code base.

I get what he's saying in how memory isn't quite a "resource" in the same sense as a the others listed (mutexes, texture maps, etc), however, I don't really see why it matters. Cleaning things up is still cleaning things up.

What I don't like

The biggest point in this video seems to be about how RAII is bad, but the only real point he's made against it is how it's "boring". The rubber has to hit the road somewhere as far as resource cleanup is concerned, and I'd rather it be done in a special cleanup method than anywhere else. Having that method called automatically is really nice, which is why you should make that method the destructor (hence, RAII). He also claims that RAII exists because of exceptions, but that's kind of a moot point seeing as many C++ programmers heavily make use of RAII while avoiding exceptions completely.

One rather bizarre portion of the video is how at one point he criticizes the syntax for std::unique_ptr, when he's actually using it incorrectly. It's pretty inconsequential for the purpose of the video, but a mistake as simple as that really draws into question how much experience he has with these C++ abstractions, and subsequently whether he has enough to properly criticize them (at least in my mind). However, I do agree that it would be nice if these smart pointers and ownership semantics were more strongly integrated into the language, though maybe not with the semantics he's suggesting.

Anyway, I've seen plenty of smart people give bad advice (I've certainly given my fair share of it, and I can't even claim to be that smart). He's given plenty of good advice elsewhere even in the same video, but I think you should take everything people tell you with at least a little bit of salt. It'll be interesting to see what happens with this language he's developing, though I'm not convinced it'll be the game industry replacement for C++ he seems to be aiming for.

This topic is closed to new replies.

Advertisement