A Comfortable Couch

Tuesday, August 10, 2004

Why C++ sucks

I'm constantly reminded that C++ just plain sucks. Its archaic, error prone and a productivity drain. I should know, I've made my living writing in almost exclusively for 5 years now.

Today I was writing code that returns an vector of HMODULEs, which are Win32 DLLs loaded into memory. But if I'd like the caller to not have to deal with freeing the HMODULES when done, then I need to wrap them in a class, and those class objects are stored in the vector. But to do that my classes need copy constructors and assignment operators overloads, otherwise the lifespan of the contained HMODULE won't be managed correctly and it will crash. So the right way is to copy or clone the HMODULE during object copy and creation, so each class instance has it own copy. But in the case of HMODULEs, there is no built in function to make a copy, so you have to query it for its full path, then do a LoadLibrary using that path. Then I have to get the semantics right for the copy constructor and assignment operators, so that it can deal with self assign and what not. Blech. All this just to return some HMODULEs is a sane manner.

Or I could ref count the object instance and store smart pointers in the vector, but there is no built in ref counting in C++, so I have to roll my own or download someone else's implementation. But which one? Is it buggy, is it bloated? Will other engineers who maintain my code like the library, or will they curse the library and me? I just wanted to return some HMODULEs dammit!

C++ is death by a thousand cuts.


Anonymous said...

You are damn right!!!

2:56 AMlink  
Anonymous said...

Win32 is not C++. In properly written C++ code, you would not have those problems.

5:32 PMlink  
Anonymous said...

So the problem is having to deal with the backwards compatibility quirks inherent in the 20-year heritage of the Windows C-based API? What exactly does this have to do with C++?

HMODULEs were not designed so that clients do "not have to deal with freeing the HMODULES when done." Twenty years ago, it was expected that programmers assume responsibility for clean-up tasks. Wrapping an HMODULE in a C++ class does not change that.

C++ is a sharp tool and it makes sense to use its power when a situation warrants it. C++ also lets you cut yourself when you use it incorrectly. A screwdriver is a lousy tool for hammering nails, but a screwdriver hardly "sucks" because of it. It's all about knowing what tools to use when their called for. Anyone who dictates that so-and-so language "sucks" is someone who displays an unfortunate degree of naïvety.

By the way, look into shared_ptr in the Boost C++ library for solving the reference counting issue. shared_ptr is expected to be included in the next revision of the C++ standard.

11:43 PMlink  
Damien said...

I stand by my comments. C++ sucks. This was one instance of why it sucks, there are many more. The problem in this instance was not the W32 api C interface code, the problem was how hard it is to get copy constructor semantics right and how much work it is. Many other languages make this stuff much easier, even when interfacing with C code.

It's almost never the best language a project, its a niche language. When ranked with all the other modern languages, its suckage-factor(TM) is quite high. I'm no uninformed newbie to the language. I've written a fair amount of successful code in it. I understand the language and its nuances, I marveled at how brilliant RAII and exceptions make for elegant error handling and the incredible degree of control it gives. I can justify my position: I think it sucks, its time as a all-purpose language has passed, its now a niche language.

12:16 AMlink  
Anonymous said...

Your code reflects your ability..If you are stuck somewhere, you have to review your logic..

1:50 AMlink  
Steve said...

does that mean i have to start paying royalties to use the term suckage-factor????

12:21 AMlink  
antivoid said...

People that cannot code in C++ properly because "it doesn't clean up after me" means for certain you're just lazy.

C++ is designed for 100% absolute control and authority over the operating system. Free memory when done. One of the powerful features of C++ is that you can override the new and delete operators in a class so that you can determine the method of allocation and deallaction. This permits deferring deletes, for example by using a static allocation table array for a specific data type.

I have been programming C++ for a good while myself. I have programming experience related to garbage-collected languages.

Indeed, I can gloat about C++ as much as I want, but the point is, choose the right tool for the task at hand. For sure, it would be foolhardy to try implement a C++ system for a CGI-type script, due to language entities that do not lend well. For web design, PHP is the best tool for the job, and C++ a lame and improbable solution.

But for 95% of programming tasks, I will choose C++, because I am man enough to take control of all the complicated semantics and understand them. I know how the operating system works properly, so I don't need java training wheels.

Cross platform binaries NEED C++.
Web design needs PHP/Apache/MySQL.
People that can't code use Java.

8:44 AMlink  
Damien said...

Antivoid, if your post weren't so amusing, I'd delete it.

11:44 AMlink  
Andrew said...

Wow, somebody claiming "manhood" because he can code in C++. I feel like kicking his ass on principle.

9:57 AMlink  
Mika Heinonen said...

>>Web design needs PHP/Apache/MySQL.
>>People that can't code use Java.

Web design needs Domino, nothing else is needed.
PHP/Apache/IIS/MySQL/Java/Oracle/DB2/ASP/JSP/SMF/etc.. are just workarounds for people who haven't seen Domino yet (and learned it) :)

4:37 PMlink  
Chris said...

I agree. C++ is my "primary language" re job experience, but I find it more of a headache these days that not. If I could choose, I would always pick Python first, as it makes life so easy. I have just learned some Java and also like what I see there. This whole notion of "being a badass" because you can code in C++ is absurd. Most applications do not need to be in control of the OS and memory these days. It's just a whip that gets in the way of solving your problem. A programmer can move so much faster in an interpreted language (no build step) that it's painful to crawl back into C++ mode once you've experienced the Joy of Python.

Hell, I would even choose Perl over C++ in most cases (that's hard for me to say).


12:34 PMlink  
Anonymous said...

Sometimes in these situations, you should ask yourself if the copy constructor and assignment overloads are really necessary for the way your code is actually being used.

Many times, people tend to try to implement these operators because they believe they have to when all it does is add needless complication.

Why not simply make the copy constructor and assignment operator trigger a debug assertion failure in case they are called by mistake and simply require the user code to pass pointers around rather than actual copies.

This would make memory management far simpler.

2:48 AMlink  
Hardcore Pascal Programmer said...

I agree with most of the people that you should clean up what you mess, but I also agree with you that C++ sucks, in many ways than one. C++ has unneccessary complications to certain things, and the programmers who wrote HMODULEs weren't exactly the best of their class.

6:01 PMlink  
Anonymous said...

You are so fucking right!!

What would take me 2 hours in C#, java, pascal, takes me 10 hours in C++. What a crappy and language with the most gay compiler errors and ugliest syntax ever.
C++ is shit coded by geeks for geeks

6:55 PMlink  
bklare said...

This post has been removed by the author.

10:34 AMlink  
bklare said...

If there is one thing worse then C++, it is Managed C++ on the .Net platform...someone kill me now.

10:35 AMlink  
Not a programmer said...

C++ is definitely more complicated than Python or Java.
I install/support/update/optimieze.. 2 systems written in C++ and 10+ other systems written in Java.
Please, if you don't need complicated things in C++, don't use them. Java systems plain/full/clearly suck to death. C++ ones are rarely visited : they just work.
C++ programs need not be written by "men", just don't use things you don't need/can't understand.
Java on the other hand does have a solid advantage : it allows for mid-stupid half-understandig incompetent people to have jobs. I'm serious : Java creates thousands of jobs.
Most intelligent IT people in the bank where I work (R&D departement) produce C++ programs that just work. Need 0 or so configuration, even they do the most difficult/benefit generating job in our systems. Java programs reflet the quality of their concepters : Tibco/WebLogic/Beans/Jrockit/Tomcat/... mess needing thousands of variables / GBytes of RAM / CPU cards / hands that can all just fall to the ground if you forget a point at the end of line 1105 of conf file 789. And keep OutOfMemoring. But thousands of jobs are created.

7:24 AMlink  

Post a Comment

<< Home