C++ is rubbish: some notes

Again, I’ve been forced to use C++. As in all my previous experiences with the language, it’s a major pain in the ass. I came to the conclusion that C++ is a programming language which no one can ever master; no matter how long you use it, after six months off of it you forget everything you knew, because it’s just too big and completely counterintuitive.

Someday I might take the time to explain more profoundly why I hate (I’m using the word hate here) C++. For now I’ll just make some notes.

I’ve been forced many times to use it, and no one could ever give me a good reason why. I believe such reason does not exist. If you want to do low level, system level or whatever you want to call it programming, C will do. While it lacks the facilities of more recent languages (and lisp), making it a doubtful choice for general purpose programming when efficiency is not the primary concern, I found C to be a really simple and clean language. It’s not at all hard to keep the primary feature set and libraries in your head.

C++ on the other hand is huge. You need to google constantly to do the most simple things, and what’s worse, you should do it, because there are a lot of things that just don’t work in the obvious ways. This is specially true when working with files.

All the advantages of object-orientedness quickly fade away when you see yourself dealing over and over with obscure syntax errors on classes that are a hundred lines long way before they do any real work. The real advantage of object oriented programming is the way it helps to think and structure your programs, and you don’t need classes or inheritance to accomplish that.

C++ is so bad that makes Java look good. I don’t like Java. I think it’s too verbose and lacks several features I expect from a high level language, most of its advanced techniques (design patterns, reflection, xml frameworks) being just patches for those absences. But all in all, Java is a language that makes sense. C++ is not.

It’s backwards compatible with C. Seriously Stroustrup? What on earth were you thinking?

The compiler never helps. The error messages never make sense, and in most cases ignoring them is the healthier choice.

C++ Strings suck. They suck so hard that they’re not a built-in type, they’re not even used in the libraries, so you have to use stuff like c_str().

Streams suck. The syntax is weird, they’re just too many of them, I seem to recall there was some diamond inheritance weirdness going on over here… I’d rather stick to C files.

STL sucks. The iterators are error-prone and tedious to use. You have to write a lot to use most of the methods of the containers, even for basic stuff, ending up with code like:

    for (map<string, string>::iterator it = myMap.begin(); it
            != myMap.end(); it++) {
        string key = it->first;
        string value = it->second;
        //...
    }

Don’t even get me started on templates.

Whether you use them or not, namespaces are a pain in the ass. If you do use them, the code gets confusing and annoying to write (and it’s easier to get compiler errors when you forget to specify the namespace). If you don’t use them in your code you still need to add the using namespace std bit everywhere or you get syntax errors that as usual don’t point the problem.

Why are there references? One can’t consistently use either references or pointers, you are forced to mix them in certain contexts [1].

The language is not consistent. An example: you instantiate classes in the stack like:

    MyClass myObject(arg1, arg2);

But what if your class has a constructor that takes no arguments? You might expect it to work like this:

    MyClass myObject();

But no, the correct way is omitting the parenthesis:

    MyClass myObject;

You might find yourself wondering for half an hour why your program won’t compile for things like this.

Methods are not virtual by default. Premature optimization is the root of all evil.

It’s copy-paste oriented. Code duplication is trouble. In C++, the standard way to code a class is to copy all the method prototypes from the .h to the .cpp file, adding the MyClass:: bit. Even with just a couple of getters and setters, theres a whole lot of typing and copying. This is not only tedious, but one of the places where more errors occur. And remember, the compiler speaks its own language.

You can’t refactor. Unit testing frameworks are awkward to use and setup. Creating a class and making it compile is so hard that most of the time you stick to add an if.

[1] http://www.paploo.net/programming/rant_01.shtml

Advertisements

4 thoughts on “C++ is rubbish: some notes

  1. whatever they do for c++ it will keep the same crap !! I 100% agree with the article idea I hate it …

    all languages gave me a real productive spirit .. but when using c++ I feel so sad and frustrated … everything is wrong … not string standards and encodings … very ugly file management … I spent 2 hours trying to find out whether getline is a member function … or a standard one … where different tutorials are talking about 2 concepts … finally I discovered it is both??

    it has no standard GUI library neither WCF nor anything … when need a GC you have to skip the original language syntax and start using SMARTPTR defintion instead why??

    it fits only some guy who cannot differentiate the taste of ketchup from mustard !!

  2. c++0x or c++11 is something like a little boy who’s torn his mobit and his grand ma sewed it with black thick thread … to get it just like chucky !!

  3. It depends. I like C because I think of most problems in terms of function(data) and for that C is just fine and simple. POD types and switch statements are easy to read and easy to reason about. Union structs and arrays are like legos. I can make anything out of bricks. But if I start thinking in terms of a network, or in terms of what things do instead of what they are, then C++ is easier to write and read. But C++ is a slippery slope. Instead of just using virtual functions and simple templates I’ll go off the deep end trying to generalize everything, because it’s possible, and that’s a killer addiction. It helps to remember that the STL was written by many folks smarter than me and it took over a decade to work the bugs out. And C++ is still evolving. I can’t count on having implicit move and other recent features across all compilers. As clever as I try to be, with very few exceptions, I’m doing nothing but writing a class of syntax sugar. I’m here because I’m currently working with C and have come to a subproblem that I think of as a network of similar nodes. I’m trying to resist the urge to reach for C++. The grass is always greener.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s