Do you think C++ is beautiful?
God no. It works. It’s powerful. But beautiful? No.
C++ is all power and sharp edges. It’s pretty much a Brutalist fortress composed from wildly disparate architectural viewpoints. Syntactically, it’s rather cluttered, with no real unifying principle. It’s a bric-à-brac if you’re generous, and a crash site if you’re not.
However, C++ is much better than it used to be. Original C++ was very messy. Expressing yourself succinctly in it was a difficult task. C++17, by contrast, is straightforward, and allows you much terser and more orthogonal constructions, safer memory management, less clutter, etc. Consider iterating over a vector in C++98:
for(std::vector::const_iterator p= pairs.begin() ; p!=pairs.end() ; ++p) { std::cout<< p->name<< ": " << p->val<< '\n'; }
Now the same thing in C++11:
for (const auto& p : pairs) { std::cout << p->name << ": " << p->val << '\n'; }
And in C++20:
ranges::for_each (pairs, [this] (auto& p) { std::cout << std::format("{}: {}\n", p->name, p->val); });
Now adding some usings and new capabilities, we get this:
for_each (pairs, [&] (auto& p) { print("{name}: {val}\n", "name"_a=p->name, "val"_a=p->value); });
Dismissing that as mere syntactic sugar misses the point, which compared to older C++ versions and C is this:
- Modern C++ lets you express yourself with less noise.
- Modern C++ lets you write code that is more generic.
- Modern C++ avoids entire classes of error, particularly when following common patterns and conventions.
C++ is heading somewhere. It’s converging on a leaner, tighter subset. Despite the requirement to be mostly backward compatible with C++, we’ve already seen careful excision of things like diacritics and the virtual deprecation of #define, bitwise arithmetic, spurious operator overloading et cetera.
But C++ wasn’t written to be beautiful, and it shows. Even the idealized C++ that Stroustrup is clearly striving toward is an unlovely, brutish thing. But sometimes, for some jobs, you need a combine harvester rather than a Camaro.
Lisp, Smalltalk, Prolog, Haskell, Python — these could be said to be beautiful. In my opinion, beauty follows criteria that neither C nor C++ satisfies:
- Conceptual elegance. In Lisp, everything is a list. In Prolog, everything is either a world fact, an implication, or a query. In Smalltalk, everything is objects and messages. Haskell wants everything to be a mathematical, stateless function chain, where Python just wants everything to be as simple as it possibly could. C++ does have a philosophy, but it’s murky and syntactically impenetrable; C has “I’m portable machine code!” which really is more of a mission statement than a coherent concept.
- Terse, expressive syntax. Smalltalk can famously be described on the back of a postcard. Lisp and Forth can be derived from almost nothing. C is pretty good here in the terseness metric, although I think its legibility is overstated. Prolog, similarly, does much with very little. By contrast, while it’s possible, you have to really know your C++ to consistently produce terse and idiomatic code.
- High level of abstraction. A beautiful language is one in which you merely have to model the problem in the domain of the language to have the solution suggest itself. Again, not a C++ hallmark unless you’ve learned how to do this as an orthogonal discipline, and C provides virtually no abstraction other than that of memory and flow constructs.
- Legibility. If not for this metric, Forth (and possibly Perl) would probably make the list. A truly elegant language is not cryptic but can be read and understood in a straightforward way. Code written in C and C++, by contrast, can be famously illegible.
There are other metrics, but these are the ones I value. In closing, however, I have to say that while elegance is always a desideratum, it is not a requirement. Sometimes, tackling a big, sprawling language can be a fun challenge, while a more streamlined version of the same thing would just feel empty and impersonal.
Notes on the Quora Migration: this piece originally appeared on Quora. Since Quora is no longer what it was, I'm migrating my content here.