Skip to main content

Using Org Babel and Org Roam to Learn C++

After Clojure, it’s now time for brushing up my C++ skills, and since it's been quite a while since I've touched that time-honouredlanguage, that basically means I need to relearn it. Being throughly trained in the Humanites, I figured this was a good occassion to build yet another Zettelkasten. I ended up finding a setup that turned out to be highly effective and makes learning C++ fun. This setup includes the following tools, all of which run in Emacs:

  • Org Roam, the database-driven Roam replica for Emacs,
  • Org Babel, Emacs' built-in way of running code within an Org Mode buffer,
  • and of course a running installation of the C++ compiler.

The idea is as simple as it is effective: Everytime I come across a topic, a problem or a new interesting function, I create a new Org Roam file. I usually add a description, some interesting links and other references. Since I like to trace programming concepts back to their mathematical foundations, I also use nicely formatted LaTeX snippets to write down the maths stuff. Having the power of Org Mode at my hands, I can also mark notes as todo items or as requiring some review. So far, so nice, so conventional.

But Org Mode really starts to shine when I actually test the stuff I am just trying to understand. I found out that the (nicely named) Library of Babel, Org Mode's collection of languages that can be run from within Org itself, also contains a C/C++ mode. I've set it up, and now I can insert actually executable C++ code directly into the descriptions and explanations.

This creates a very pleasant workflow: I formulate the idea in a new file, and then I write some short code that illustrates it. This snippet, which is part of the note, can then be executed directly from Emacs. No need to switch to another file or mess with the shell for compilation. It's all as simple as hitting C-c C-c in the C++ code section, and I get direct feedback on what I am trying to understand. And of course, based an that feedback, I can further refine my notes and its associated code. In addition, the feedback is inserted below the snippet so that I can see the output immediately when I call up the note again.

Here's an example:

Example source code

That's a short code snippet illustrating the use of lambdas in a std::for_each loop. Nothing crazy in terms of programming, but a handy little thing to have when in doubt about some obscure syntax detail. And I am a visual type; I like to actually see the stuff I am talking about, to get a feeling of how it looks like.

Of course, Org Mode being what it is, all of this can be made much more intricate. I can pre-define input variables, chain the results of multiple source blocks, and even use so-called tangling to re-use some boilerplate code. That's a lot more than I need at the moment, but it's nice to know that it's available.

All this reminds me of the typical REPL workflow experience, which C++ as a compiled language unfortunately cannot offer. You write some code, inspect the results and rewrite the code accordingly. In fact, the new C++ experience even surpasses a REPL in one respect, as everything that is executed is also documented, including the result. It's actually more like a Jupyter notebook, creating something that tells a narrative, with the added benefit of tagging, hierarchies, and a decent editor. The code is naturally integrated into the notes, which makes for a smooth and good learning experience.

To replicate the setup, you don't need any special configuration settings. Install Org Roam and run (require 'ob-C) manually or in your init.el file, that's all. You could even test this workflow in one simple Org file, without bothering about a Zettelkasten tool at all. In that case, all you need is to enable the Org Babel C/C++ mode. Trust me, it is the perfect environment for learning a programming language.