10/09/2021

QM: Do you think C++ is beautiful?

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.

QM: The bare minimum

I have an old computer having having intel pentium processor with 2.00 GHZ speed, 1GB RAM and only 150 GB hard drive. I have a passion to learn programming. Can I learn with this device?

This, my friend, is the Commodore 64.

It was launched in the year 1982 at a price point of $595 ($1,500 in today's money). Its 6510 processor ran at roughly 1 MHz, it had 64 kB of RAM, could be fitted with a casette tape player or (if you had the cash) a floppy disk-drive. It had no true operating system, but booted you directly into a BASIC interpreter. As for hard drives, Internet, sample-quality sound or graphics with more than 16 colors, those were pure fantasies at the time, about as attainable and practical as ordering a unicorn by mail. And you know what?

It probably created more programmers than any other computer.

Yes, it was limited. That's actually a point in its favor, because we learn and grow as programmers by facing and overcoming limitations placed in our way. And to varying degrees, as many as ten million C-64 users did precisely that, because they had ideas, because they were challenged by the scarcity of resources, and because the barrier to entry was so low.

So. The computer you have is thousands of times faster, with over ten thousand times the memory, and nigh-infinite permanent storage that does in nanoseconds what would take minutes to load or save onto precious tape on the '64. Its Internet connection lets you download, for free, all the development tools and examples and tutorials you could ever need. With the proper OS, you can have it multitasking like a dream, and you'll never have to deal with not having memory protection, virtual memory, or any of the thousand things that didn't exist during the eighties.

Right, the conclusion. Well, the question was, "can I learn with this device?" And that is of course entirely up to you. But the idea that the computer itself would limit you is just... no. Compared to what most programmers throughout history used to learn their craft, the one you have is amazingly, mind-blowingly, ridiculously overpowered.


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.

QM: Do good programmers use Else?

Coming from a C/C++ legacy codebase written by self-taught engineers, I understand why someone would embrace this notion. You can only look at so many gigantic piles of nested if statements before you go "you know what? I'll just collapse the whole pile by exiting early using return statements." Before long, you'll clear away all these little methods that only branch when their various calls result in a null pointer, and you'll pat yourself on the back for having eliminated all the else statements. Tempting, then, to wonder when else is even necessary.

True, those else statements were redundant. That means very little. All it proves is that like any other tool, else can be misused. That doesn't make it a bad tool, it just means it was used in the wrong place. For short, well-considered routines, if-else is perfectly legitimate.


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.