The Beauty of C++

It’s pretty widely acknowledged that C++ is not a very nice language.

Some people hate that it still has pointers. Some people hate that it now has references. Some people hate that it now has pointers and references. It has just enough library support that some of the utilities are useful, yet professors can keep track of them all and specifically forbid you from using all the useful ones on programming assignments. It has nice features like templates and operator overloading, but makes them so painful to use that you almost wish it didn’t. (Did you know that the stream operators must be overloaded as stand-alone functions, not as member functions of the class? Otherwise you get bizarre, nonsensical compiler errors that can’t be fixed until you vaguely remember reading in your C++ textbook two years ago that the stream operators must be overloaded as stand-alone functions, and then dig out said book and look it up to confirm.)

In fact, it seems that there’s only one person in the world who didn’t get the memo about the uncool nature of C++, and that person happened to be sitting in front of me on the first day of my undergrad discrete math class.

At UC Davis, the first programming course for majors focuses on C, without worrying about object oriented anything. The second programming course for majors covers C++ and object oriented everything. I was also enrolled in the C++ class that term, and during the first session earlier that morning, the instructor (a hardassed security research guy) had finished his introduction to C++ by telling us that it was a horrible language.

See, he knows that C++ is uncool.

But the guy sitting in front of me in discrete math had a different opinion: he insisted to his friend in a loud voice that C++ was—well, if someone had a gun to your head and would shoot you unless you could say something positive about C++, what would you say?

Stroustrup tried his best. He came so close to incorporating object orientation into C.

He gave it the old college try.

It makes some things easier than C.

It’s better than Cobol.

It’s fast.

It’s fast.

It’s fast.

He didn’t say any of these things.

He said it was beautiful.

He said “Yeah, I’ve looked at some C++ code, and man, it’s beautiful.”

He didn’t say it sarcastically, like “It’s beautiful. Yeah, man. Just beautiful.

He said it passionately, like a hot-blooded young man swept off his feet by a beautiful and mysterious woman.

He said it as if he’d seen C++ at the masquerade, shared a flirtation, danced with C++, tangoed with C++, spun C++ around with a rose clenched in his teeth, and then when he was done he’d said “So, C++. Shall we meet again?” And C++ replied “At the next masquerade. Look for me at the rise of the full moon.”

Of course, some of us had a similar flirtation with C++ when we were young, hot-blooded, bucks who had just recently chosen the manliest major around and were feeling invincible. But a year or so of its prima donna antics convinced us to leave it. The drama C++ brings to a simple act like making sure the correct override of a derived class method gets called through a base class pointer, the hysterics around templates, the fights over accepting instances of the string class instead of pointers to char; sure, it adds spice at first, but eventually it just gets to be too much. Maybe you feel manly at first, like you’ve conquered the most desirable woman in the world, but eventually you just get tired of fighting with C++ over even the smallest things. Eventually you just want a safe, plodding, homely sort of language, like Java. Sure, it’s not too bright. You have to explain everything to it at untoward length. It lacks those spicy features like operator overloading, and it doesn’t always do things in the smartest or most efficient way. But it’s a hard worker and is biddable enough if you give it what it needs. It never does something so hotheaded as seg faulting.

At this point, I had already taken two semesters of C++ programming, one general introductory course and one data structures and algorithms course. For me, after a year, the romance with C++ was over, and I was starting to discover all the warts and moles and festering gumboils it keeps hidden under its flamenco gown. Probably the most festering of all the gumboils, at least as far as being the ugliest and most idiotic syntax I could possibly imagine to represent the concept, is the “virtual void belch() = 0” syntax for declaring pure virtual functions (“abstract methods” for Java people). Other genius syntax decisions were requiring a semicolon at the end of class and struct definitions and requiring a space between angle brackets in nested template declarations.

You also might expect a beautiful language to make it easy to write beautiful code—beautiful in the sense of shorter, simpler, easier to read, expressed at a higher level of abstraction. C++ does none of those things. If anything, it does the opposite; it’s frequently easier to write the ugliest, most godawful thing you can imagine than anything halfway decent, because the language contains no infrastructure to help you write the nicer solution. For example, I can’t even count the number of times I’ve dealt with text using some variation of reading a single character at a time, clumsily saving some number of them in an array, and examining the whole mess with a huge, ugly if-else if-else if-else if-else if-else if-else-if else-else statement. I could have taken the high road and written my own regular expression engine, but with C++, go too far and it could end up screaming at you for three hours and then storming off to stay with its lover, refusing to come back until your deadline has passed.

C++ has its uses. It’s fast. It’s better than Cobol. It supports object orientation somewhat better than C. But one thing it is not is beautiful.

Thus I fulfill my rite of passage. I have rejected C++. I pass the test, and remain a programming blogger.

Advertisements

4 thoughts on “The Beauty of C++

  1. What I overhear about C++ is that people can’t agree which 10% of C++ features are safe to use.

    I have a friend who codes PHP and another friend who codes C++. The C++ coder was building a 3D engine and the PHP coder said “Good luck with fixing segfaults”. He has two bug-fixing stories: The first one involves a segfault even though the code should be working (according to a StackOverflow answer): It was a video driver problem on Linux. Then, he fixed a frustrating 3D model loading problem by observing memory usage.

    The PHP coder and me just took a course in software construction: It’s about OOP and design patterns in Java. He said Java is very verbose. I agree based on my experience with the less-verbose languages like C# and F#. C# has getters, setters, indexers and operating overloading, and F#’s type system allows me to create a 20-line-equivalent Java class in 3 lines (due to auto-generated constructor, equality and comparison in F#).

    Personally, I like F# because it’s the best of both worlds: C#’s imperative and OOP and Haskell’s functional programming and algebraic data types (Haskell IO is mind screw). It’s very concise, using abstract functions rather than loops, and type definitions in F# are much shorter than classes in C# and Java: F# abstracts away classes and constructors. Even better: Compatibility with C# code and even making new classes in F#. F# uses indentation like Python, but unlike Python, where opening the parens suspends the indentation rule until the expression ends, in F#, indentation is important even inside expressions. The downside is that I keep forgetting the syntax for virtual methods, getters and setters in F# OOP. Finally, F# computation expression, based on Haskell monads, is the best abstraction ever: It puts things like logging, yielding and conditional chains behind the scenes. the seq and async workflows are so useful they are in C#.

    Given my current projects, I’m using C#, F#, JavaScript and Angular.js. Some scripting languages in code golfs, but never C, C++ and PHP.

    Speaking of PHP, my friend said “English is the PHP of human languages” and “German is the C of human languages” (long compound words = structs) and “Chinese is the Haskell of human languages”.

    • The “can’t agree which 10% of features” problem was pretty much what prompted me to write that post. Before that, I thought C++ was okay; it was less verbose than Java, and had nice features like operator overloading. What I didn’t realize was that I’d been living in a bubble, where I had picked a 10% of the C++ features that I liked, learned how to use them well and write decently readable code, and ignored anything outside that subset.

      When I wrote the post, I’d just been bitten by this. I had just struggled through a compiler project where the code I had to interface with used a different 10% of C++ features, and mixed pointers, typedefs, and templates in what I found to be quite horrible ways.

      I’ve never tried F#, but I’ve heard good things. C# too. The problem is, I don’t like being tied to Windows, and I’m a little wary of Mono. Because of that, and because I already knew Java, I went with Java/Clojure instead of C#/F#, even though C# does sound much better than Java. Clojure doesn’t let you get quite as OOP/imperative as some functional languages (such as OCaml) do, although it’s definitely “impure” if Haskell is the yardstick of purity. Recently I’ve been thinking of checking out Scala, too.

  2. It’s just a language. Why complain about how virtual methods define an abstract class when that’s just the syntax. One you know, you know. That’s it. What thing of beauty do you expect to find at this level? When you are fluent in a language the syntax no longer matters. It’s automatic. What’s beautiful then about the language are the complex structures you can build from it, just as you can build poetry from the spoken word.

    • The beauty that I’m talking about in this post comes from consistency, orthogonality, and perfection of abstraction. That’s the beauty I find lacking in C++. It’s not just about syntax; it’s about how the syntax suggests its ideas. Inconsistency, lack of orthogonality, and poor abstraction make it harder to build things in a language. I dislike comparing programming languages to spoken languages; the former was designed, built for a purpose, while the latter evolved naturally, and comparisons to spoken language have been used to justify too many terrible programming languages. But if we take your poetry metaphor, then a language like C++ that makes it so hard to use basic features like templates and overloaded operators correctly, that provides so many different and contradictory levels of abstraction to work at, that has so many conflicting and non-orthogonal features, is like trying to write poetry by carving it into wood with a butter knife.

      Having said that, this post is from a few years ago, and nowadays I probably wouldn’t have bothered to write it. It’s not really that funny, nor is it particularly insightful.

Comments are closed.