C++ sucks less than you think it does

C++ seems to be the quick­est lan­guage to get a knee-jerk “it sucks!” re­ac­tion out of peo­ple. The com­mon rea­sons they list:

C++ is a very pow­er­ful, very com­plex lan­guage. Being a multi-par­a­digm (pro­ce­dural-, func­tional-, ob­ject-ori­ented–, and meta-pro­gram­ming) lan­guage that im­plores the coder to al­ways use the best tool for the job, C++ forces you to think dif­fer­ently than other pop­u­lar lan­guages and will take the av­er­age coder years of work­ing with it be­fore they start to get truly good at it.

C++ does have its flaws. Some are fix­able, some aren’t. Most of what is fix­able is being ad­dressed in a new stan­dard due some time next year (2009).

Its biggest prob­lem is that new­com­ers tend to only see its com­plex­ity and syn­tax, not its power. The pri­mary rea­son for this is ed­u­ca­tion. In in­tro­duc­tory and ad­vanced courses, stu­dents are taught an un­der­whelm­ing amount of tem­plates with­out any of the use­ful prac­tices that can make them so epi­cally pow­er­ful. Use of the in­ter­est­ing parts of the stan­dard li­brary, such as it­er­a­tors and func­tional pro­gram­ming, are put aside in favor of ob­ject-ori­ented de­sign and basic data struc­tures which, while use­ful, is only a small part of what C++ is about. RAII, an easy zero-cost de­sign pat­tern that makes re­source leaks al­most im­pos­si­ble, is vir­tu­ally un­taught.

C++ does tend to pro­duce larger ex­e­cuta­bles. This is a trade off – do you want smaller ex­e­cuta­bles or faster code? Whereas in C you might use call­backs for some­thing (as shown by the qsort and bsearch func­tions in the stan­dard li­brary) and pro­duce less code, in C++ every­thing is spe­cial­ized with a tem­plate that gives im­proved per­for­mance. This fol­lows C++’s “don’t pay for what you don’t use” phi­los­o­phy.

Don’t get me wrong, C++ is not the right tool for all jobs. But among all the lan­guages I have used, C++ stands out from the crowd as one that is al­most never a bad choice, and a lot of times is the best choice. It might take longer to get good at than most other lan­guages, but once you’ve got it down its power is hard to match.

Posted on January 10, 2008 in C++, Coding, RAII

Related Posts