Use your programming language

In today's post, you will learn about what that language part of programming language means.

I have to start by admitting that I had a different view on this in the past. However, even back then, I saw a programming language as a language. A language that I can speak with the compiler and, of course, my fellow developers. Language is the key here.

One person who noticed this early, maybe the earliest one, was the famous computer scientist Grace Hopper. This is a quote from Grace Hopper which I like very much:

Very few people are really symbol manipulators. If they are they become professional mathematicians, not data processors. It’s much easier for most people to write an English statement than it is to use symbols. 1

I think the idea of writing code that others and I can read like an English sentence is great. We should think more about our code in that direction because we humans are the ones who have to maintain the code. Compilers don't fix bugs, humans do.

I think that it's interesting that in the beginning, people did not believe what Grace Hooper told them, that computers could understand English:

When I started with the first compiler, nobody really believed it. 1

Use words instead of tokens

At this point, I believe I have to apologize to Titus Winters. In a Twitter discussion some years ago, I did neglect the power of C++'s alternative tokens (lex.digraph).

Okay, at this point, some code would be good, right? I'm not a philosopher, after all. Here you go. One of the changes I adopted early is to avoid statements like this:


Think about it, how quickly did you spot the negation? The problem I have over time is that also, depending on the font, this negation is easy to miss. What I use nowadays is the following form:

if(not ptr)

As you can see, not even the written version of not is easier to spot, it is now a (more or less) token-free sentence: if not ptr.

What I adopted later

The part where I wasn't convinced back when the Twitter discussion took place was something like this:

if(!ptr && (haveValue || skip))

At the time, I used not but kept the tokens for and and or.

Admittingly, that line contains quite a few tokens where we have to know the meaning to understand the code, plus we have to translate the tokens back to a sentence that makes sense. Luckily, in many programming languages, the meaning of these tokens is identical, but nonetheless, they are tokens.

As a sentence, the code reads like if not ptr and haveValue or skip. Better, isn't it? So let's use English words:

if(not ptr and (haveValue or skip))

What do you think? That's C++! At least in C++, you can choose to write by using the alternative tokens. Interestingly other languages like Python only provide the language option, no tokens.

What's on my wishlist

Inspired by Python, I have a hard time writing range-based for loops. I tend to really write for variable in container. Unfortunately, there is no in keyword in C++. Instead, you and I have to write a colon.

Staying at the colon, when writing a derived class, I sometimes feel it would be nicer to write class Derived extends Base instead of class Derived : Base. But then I get an even bigger desire for an interface keyword. I looked into what we can do with C++20 without changing the language in my talk C++20 Concepts - We have to rethink what we did in the past at ACCU 2022.

Back to the in keyword, I also would love to write if(key in map) like I can do it in Python, as opposed to if(map.contains(key)). That one might be the weakest as if map contains key does already read pretty good.

Take away

Use words instead of tokens for readability. Give the alternative tokens in C++ a chance. See whether you also conclude that code becomes more readable. The wisdom from Grace Hopper is still true. Humans are better with words than symbols. Well, at least some of us :-)