n2liquid's sandbox

Why I hate C++ (and all other imperative languages I’ve tried)

Posted on: June 24, 2011

Afternote: I think I could’ve been more specific when I targeted imperative languages; I think I should’ve said procedural languages instead. Not that it changes much, just to be pedantic…

I know, I know, there are lots of blog posts and web pages telling why their authors hate C++.

I know putting yet another one on the network won’t give me much visibility. I know most people will just ignore it thinking I’ll just reiterate over the same points again.

But I have to say, as a foreword, that I never entirely agreed to any of such rants, so this one might be just a little bit “new” compared to them.

I used to really love C++

… for its C-like closer-to-the-hardware (compared to most other languages) approach. For one, I felt like C++ would never push me towards dynamic allocation when that wasn’t needed. It’d never push me towards dynamic dispatching when that wasn’t needed. Wouldn’t push me towards pointers unless they were really called for.

I felt that I had total control over my tool set

When you drive a car you start feeling like it’s an extension to your body; you know your summed up dimensions, so you don’t crash when taking it out of the garage.

In the case of higher-level languages like Java, C#, or even Delphi or Visual Basic, when you do something, you always have to ask others what are the exact implications of what you’re doing.

Are you comparing strings without knowing? Are you using dynamic dispatch when you think you’re doing an extremely trivial task inside a tight-like-hell loop?

Documentation never makes those things clear.

For reasons I’m not really sure, ever since I started using C++, I always felt like I automagically knew the implications of all my steps. I knew I wasn’t buying bloat for a triviality.

As my knowledge of the language evolved and I discovered new language features, I was always surprised that even though that feature was there, I hardly ever used it even if indirectly. And they were still very useful. And I never took more than a few minutes to understand how that worked and exactly how it’d impact my code.

I can’t say I was never took by surprise. For example, I remember I was shocked when I discovered dynamic_cast would do an insane amount of string comparisons depending on the implementation. But, frankly, I don’t remember of any other event like this.

I then started using Java at the university

And was damn shocked that people just starting to code were using a very large chunk of the C++ tool set without even knowing it. Every breath a Java programmer take causes a handful of virtual dispatches (edit: I didn’t know the JIT’ter eliminated those back then, heh) and dynamic allocations to happen behind the scenes.

Everything is a pointer, so everything gets dynamically allocated (okay, maybe the VM actually puts stuff in the stack when it thinks it’s a good idea, but the point is that you don’t see this happening; the programmer is blind to those details), every variable is nullable, pass by reference and pass by value are mangled into the same syntax and people don’t get it why modifying an object argument modifies the original one, and why modifying an integer argument doesn’t. They have no idea how tough string concatenation really is for a computer and simply use it blindly all the time, among other things…

“Hahaha!! Java’s such an utter mess~! :3”, I thought. “Thank goodness I’m a C++ programmer :’DD”.

Even today I don’t know what magic spell is used so that the processor prefetches the right code path when a Java method is called, or if Java programmers just don’t care about those things. “Come on, that doesn’t impact code speed!”, right?

Well, I am a game programmer. I must be able to make things look good on a Nintendo DS. I’m sorry if my game doesn’t run in a data center cluster, ok? I’ve seen presentations showing how proper caching and prefetching impact PS3 games, and no over-engineered piece of OOP code compensates for one cache miss every two instructions.

Same thing goes for C#, and I didn’t even get started with dynamically typed languages.

So, seeing how every language used so many things C and C++ taught me were not to be taken lightly without even telling me, I felt C++ was my safe little castle. And it was very comfy while it lasted.

Then I started understanding better why C++’s features existed

I started understanding that OOP didn’t just mean creating objects the way I saw fit. I bumped upon the principles of object-oriented design, and I felt I groked a bunch of them.

I thought I finally understood what designing software was like and started foaming through the mouth like a mad dog to put everything into practice. I felt I understood what good software design really was about.

I noticed that even though I understood that a class should respect the open-closed principle (OCP), any but the simplest of the classes played well with it when putting it into practice using C++.

I noticed that soon enough my classes simply had to be modified to be extended, and the more I struggled to make them “OCP-compliant”, the more I had to resort to dynamic_cast‘s, hash maps, etc., slowly drifting towards C++’s higher-level, heavier constructs.

I might as well just switch to a dynamic language if I’m to use everything I have to use to make this shit work. And we’re just talking about one of the most basic principles of OOP here.

I then thought I should remain calm, though. C++ just had to have means to do things right without becoming Java-without-the-VM. So I started digging into templates and template metaprogramming, just to find out those features were largely meant for a different paradigm altogether.

C++ isn’t just OOP, it’s multi-paradigm, remember?

So I got in touch with this new paradigm, called generic programming (GP), and noticed it simply didn’t click with OOP, at least not in C++.

“Wtf?”, I asked myself. “It feels as though I’m programming the same thing in two different, incompatible programming languages!”, I thought. I could write part of my code using GP, part of it using OOP and more or less get both parts to “talk” one to another, but sure like hell both paradigms couldn’t coexist inside one same logical part of my code.

This confused me a lot.

At first sight, C++’s templates and OOP looked like they’d beautifully complement each other, but that couldn’t be further from the truth.

Getting them to complement each other requires hacking through the most obscure quirks of template metaprogramming and discovering just how ugly the C++ standard really is.

I discovered that, overall, the GP guys don’t like OOP, and the OOP guys don’t like GP, and that templates were introduced into C++ so that it would support GP just for the sake of those who liked that paradigm. No effort seems to have been put to make both work well together.

Some guys from the Research and Development Laboratory at EPITA, a french computer engineering graduate school (whose website seems to be offline for years; does anybody know why?), who, like me, were not fully satisfied with either GP or OOP actually got to mix both together using pure C++.

They named this technique SCOOP (Static C++ Object-Oriented Programming) and released a library to ease its use, and although I really respect those guys for what they did, I feel like SCOOP may all-too-easily become a maintenance nightmare, not to mention nobody would understand your code if you started using SCOOP in your everyday code.

This discovery was the last straw it took me to accept my idealization of C++ as simply one massively giant fallacy. Not only programming the way I thought was right would produce hard-to-maintain, hard-to-understand code, it would also use too much templates, which basically means the memory footprint would skyrocket because the compiler would vomit infinite amounts of slightly different versions of the same machine code into the executable file, which is generally cool on the PC, where memory is abundant, but is just suicide in embedded devices like video-games.

Ever since then I feel like C++ just can’t convey my voice anymore, be it like a computer or a video-game developer.

C++’s expressiveness dried out for me

When I write C++ code now I feel like I’m wearing a goddamn straitjacket.

My mind goes blank and I can’t help myself to write anything. All attempts to equalize my code with the best practices in my mind feel like I’m bumping into walls.

What bothers me the most, though, is how everybody is just happily getting stuff done in the world, be it with C, C++, Java, C#, Python, Ruby, JavaScript, or whatnot, while I’m completely stuck watching them breaking each and every principle of OOP and saying they love OOP.

I think now’s a good time to ask you what the hell do you mean when you say you feel comfortable with the OOP expressiveness of your language?

Sure, you can more of less map the agents of your problem-universe and it kinda feels like your design is “living”. But you must be fucking kidding me if you tell me you can write good OOP with Java or C#, or even C++.

With the currently available statically-typed languages, I don’t see how the best OOP practices can be put into use. Static typing simply fucks it big time.

If you’re creating classes in Java, C#, or C++ and thinking you’re actually doing mankind a favor you’re just fooling yourself. You are not. You’re just awkwardly getting the work done. Your OOP skills are a joke. You should reeducate yourself. You’re sacrificing your most brilliant ideas by writing them the wrong way, making everybody have to rewrite them later if they want to reuse them.

Languages like JavaScript, on the other hand…

… from what I have seen, really do empower you as a programmer.

But JavaScript is a gigantic hash map, and what did I say about taking strings, the computer cache, and memory footprint lightly?

Really…

I want a language that empowers me, but not by defecating on the hardware’s head. What do I do?

Conclusion

I’ve written a lot in this post, but I passed far from explaining why I think this way. Hopefully you’ll forgive me for not citing any references. I’m just too depressed to get things straight. This post isn’t as much for you as it is for me, the n2 whose brain is bursting to flames and wanted to write down the reasons why.

I still feel the urge to code, but I’m afraid it will pass with every failed attempt to feel good programming again.

I might try one of those purely functional programming languages out there, but I don’t see how a game could be naturally described with them.

And, really… I hate C++ now. No other language seems to solve my problems, but at least they didn’t deceive me for so long. I wish I could believe somebody would point me out to something in it that would enlighten me and let me restart my programming journey, but I’m afraid I’ve seen enough of C++ to know this isn’t going to happen. I need something else.

And I need something new.

Advertisements

3 Responses to "Why I hate C++ (and all other imperative languages I’ve tried)"

Lua when you can, C when you need. They work together quite nicely.

It sucks that it turned to be this way, but based what you said why develop software anyway?

Well… I don’t know what hooks me so much about programming and software development. But I like it, and I wish I could be more proud of it. And I think I have pretty good ideas on how to achieve that, and that’s why I started writing about the Chii language, trying out some stuff, and noting down ideas.

The thing is… software development is to me conceptually really almost addictive; it really gets me going. But in practice I’ve found what I described in this old post. Then again, I think I know what to do about it, so I’ll do it slowly when I have the time and feel like it.

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

un.ma.i!

Twitter (technical)

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Twitter (personal)

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Get messaged when I post something new!
Just enter your e-mail and hit Follow:

Join 171 other followers

%d bloggers like this: