On Highlighting Syntax vs. Semantics

Aesthetics aside, syntax highlighting in code performs an important role in improving the readability of code. Most syntax highlighting schemes including popular libraries are limited to using colors and formatting changes to distinguish between different syntax classes. This is what it would look like if we apply the same logic to English prose. Each word in this paragraph is highlighted using its respective syntactic purpose.

Not very readable, is it? The colors aren’t random. They highlights correspond to noun, adverb, gerund, preposition, adjective, and verb. This is basically how people use syntax highlighting.

Typographers have long known that punctuation alone can’t sufficiently convey emphasis and relative importance of prose. As [1] points out italics is “the print equivalent of underlining” (via [2]). A number of uses exist for this font variation including providing emphasis, stressing important points, or indicating foreign words and phrases. When conveying emphasis the syntactic role of a word does not change; the word just is more important to make the central point than others.

These key important words alone can’t convey a story. Grammar and completeness demand accompanying words. It is the role of formatting, whitespace, and punctuation to guide the reader towards what is important. Without formatting and whitespace, prose would become an intimidating “wall of words”; tedious and unwelcoming.

And so it goes for code; some code is more important than others. The central logic of a snippet of code lies somewhere in the middle surrounded by boilerplate, setup, and tear down logic. If syntax highlighting were to make the central logic stand out, then it has to go beyond mere syntactic purpose of the tokens comprising the code.

What would such a scheme look like?

bool ListValue::Set(size_t index, std::unique_ptr<Value> in_value) {
  if (!in_value)
    return false;
  if (index >= list().size())
    list().resize(index + 1);
  list()[index] = std::move(*in_value);
  return true;

Perhaps not the best example, but you should get the gist of where I’m going with this. The highlighting quickly directs your eyes to the parts that need attention. You’d still need to do the legwork for the details. But for someone trying to figure out what some undocumented function is doing, this kind of highlight makes quick work of going past the boilerplate.


L. Truss, “Eats, shoots & leaves: The zero tolerance approach to punctuation,” Penguin, 2004, p. 146.
Wikipedia, Italic typeWikipedia, the free encyclopedia.” http://en.wikipedia.org/w/index.php?title=Italic%20type&oldid=1049142359, 2021.