Commitment to Comments
Why do comments exist in every language?
From Assembly (1949) to Julia (2012), all programming languages have some type of syntactical representation of a human-programmer-readable, non-compilable string.
The ubiquity of comments becomes even more intriguing when we take into account that programming languages have evolved so much in terms of readability.
program hello
! This is a comment line; it is ignored by the compiler
print *, 'Hello, World!'
end program hello
hello world in FORTRAN (1954)
// This is a comment line; it is ignored by the compiler console.log('Hello, World!')
hello world in JavaScript (1995)
Why haven't we created (or stumbled upon) a programming language that has no need for human readable comments? Why isn't it possible to write self-documenting code?
My hypothesis is that human languages have an inherent structural flexibility that programming languages can not have.
ASTs vs NLPs
Language is a broad and deeply complex topic, but for the scope of this blog post, we will only be discussing the concept of “flexibility”.
If you have ever been in a high school English class, you'll remember the anxiety of searching through the web for synonyms to make your essays sound more "edu-professional".
You’d replace good with satisfactory, or sad with despondent in order to look like you possessed a tighter grasp on the English language than any seventeen year old has any right having.
For example:
The cat went to the corner store to buy some food.
is effectively the same as:
The feline went to the bodega to procure some provisions.
The difference between these two sentences is quantitative, not qualitative.
This means that languages can develop dialects and local terminologies and rapidly adapt to a changing environment.
Programming languages do not have the fortune of having this inherent flexibility.
I'm Stuck
One of the common complaints I hear from new programmers is the difficulty in grasping the rigid rules of programming languages. Most all programming include what are known as 'reserved keywords'. These words (for, let, if/else, etc.) are in essence imperative commands for the computer to undergo.
If there are any spelling errors (for vs fir, if vs ef) the compiler of most any language (shout out to JS) will throw a temper-tantrum of epic proportions.
In order to write code, one needs to be able to map a vague idea into repeatable steps, known as an algorithm. These algorithms are rigid by nature. This is because any computer running the code needs to create the exact same output.
Consider the following:
const fruits = ['apple', 'orange', 'mango']
const fruitsLen = fruits.length // 3
for (let i = 0; i < fruitsLen; i++) {
console.log(fruits[i])
}
// apple
// orange
// mango
If this code was written even slightly differently, the qualitative value changes. The only changes I could make without breaking the structure of the program are the variable names (fruits
, i
, fruitsLen
).
Everything else has to stay the same.
What Does This Mean
Comments are useful because they retain the flexibility of acting like "regular" human language, while finding themselves physically located next to rigid, deterministic code.
This allows people to explain their thought process to readers in the indeterminate future, or to take advantage of cultural colloquialisms that are not written into the rigid rules of a programming language.
Self-less Documenting Code
As far as I know, no level of self documenting code will truly be able to capture the complexity and freedom of natural language, while also respecting the determinism of an algorithm.
Despite having well named variables, and despite writing functions that only do one thing, self-documenting code cannot go far enough.
There needs to be a bridge between mind and machine, and comments do a pretty good job.
written by Bram Adams