First of all, let me clarify that I think C is a great language and excellent for the development of low-level system software. I think C++, specifically, adds an extra unnecessary layer of complexity which makes it less suitable for low-level work but in some cases it’s the only option. I would readily choose C over C++ for low-level software projects (e.g. drivers).
When dealing with C code, I would aim to write as little code in C as possible and provide an interface for writing the remainder of my code in another “safer” language, such as C# or Java. By “safe” here I mean something that is detached from the OS, so that it has a lower risk of corrupting the system’s memory or file system upon failure. Again, C# and Java run in virtual machines which provide an extra layer of safety should things go horribly horribly wrong. The layer of safety may not be perfect, but it’s better than having no layer of safety at all.
Also, when I said C++ sucks (sort of), I meant that C++ sucks for most types of applications (e.g. GUI or web applications). There are some cases where C++ is a decent choice, but those cases are rare and limited in scope. For the most part, C++ is just plain awful, because it consumes more time and produces more lines of code than either C# or Java. It also induces programmers to produce substandard code and has the inherent property of creating very difficult-to-debug code.
There are certainly cases where using C++ is unavoidable. When such a case
arises, it does not usually mean that C++ as a language is in some
way superior. It just means that C++ is not completely useless. I still
write C++ code occasionally, because I have to.
The Debate Goes On
Recently I came across an article that attempts to refute my claim that, in most cases, C++ sucks.
I have a couple of remarks related to that article:
“What do you do when writing cross-platform code in C and you need to make heavy use of the filesystem?”
I assume by “heavy use” here you mean low-level operations. In that case, you need to make a file system driver in C (or whatever language the OS supports for drivers) and expose APIs that can easily be called from a higher-level Java or C# client application. Again, minimize the integration between high-level application code and low-level driver code.
“On that note, nobody, and I mean nobody should be using C++ without Boost”
If that is the case, why is Boost not a feature of C++? Maybe it will make its way into C++0x? The very fact that Boost exists indicates that C++ is a deficient language. Boost is merely a patch over those deficiencies. And when I talk about C++, I’m also referring to the standard library as well as the language itself.
“I guess the author has never worked on embedded software or real time systems. Or games for that matter. Or I guess anything other than canned business software.”
Actually I have built games using DirectX in the past. I can confidently say that C++ was not needed for any feature. Given the extra time that C++ adds to the development of any app, being able to use any other language (and minimize the use of C++) is quite important.
Here is a simple 3D demo I built with OpenGL and Java in 4 intensive days of coding: http://www.dacris.com/article.aspx?name=demos. I probably would not have been able to perform such a feat with C++.
“C++ has a keyword called sizeof() that lets you determine how big a pointer is.”
Again, that’s nice, but I’d rather not use pointers and not write sizeof() statements all over the place. The time I save by not thinking about pointers (and by not reading code that contains pointers) is enormous.
“Perhaps an even better answer is in order though. I won’t use any Gui toolkit, because I won’t develop GUI code in C++. Why would I use the language for something which it doesn’t excel at?”
Exactly. If you’re content with building console applications, use C++. No problem. But for any kind of UI, C++ just sucks.
“C++ has a very strong, flexible type system.”
Flexible, yes. But the problem is it’s also lacking some very useful features compared to C# or Java. For example, how do you enumerate a class’s fields or properties in C++? How do you detect that class A inherits class B at runtime, or get an object’s type at runtime? In C# this is called “reflection.” There is nothing like it in C++. For this reason, it’s very difficult to do generalized serialization in C++. In C# and Java you can literally serialize any type.
I can’t think of any reason why you would ever want this. Macros and templates are a symptom of bad design. Instead of using code generation to deal with repetitive code, why not build a reusable component? Generated code is hard to maintain, hard to understand, hard to verify, and hard to debug.
Write as Little Code as Possible
Let me end by saying, I’m not a “substandard programmer.” I’m merely a lazy programmer. I like to write as little code as possible. I keep track of the lines of code in all my projects, to make sure that number stays low. I guess if I was paid by KLOC I’d be pretty broke.