Syntax Highlighting Is Backwards (2018)
benkuhn.net
benkuhn.net
In my case, I prefer having the keywords highlighted and comments less visible than the code. Why? Because code is not something I read linearly. Having the syntactic markers highlighted helps me visually navigate from blocks to blocks. And it makes it much easier to spot missing curly braces, parentheses, etc.
In the case of faint comments, I may need to slow down and actually read them if they’re a little less visible. I also know instantly that comments aren’t code and I don’t want all my code faint so just them being different from each other is enough for me.
Article makes good points but doesn’t seem like it’s for me.
[0] pycharm and vscode come to mind because I use them the most, but other that I don't recall also do it
edit: a couple of typos
I have BENFIXME and a git hook to prevent ever commiting it, handy for when I want to not forget to come back to something.
The contrast between hues has the enormous benefit of being both obvious and ignorable at the same time, depending on your frame of mind -- comments stand out, but it's also easy to think "brain: ignore anything green". I suspect this would work with any (primary?) color (though it will change what other colors are available for everything else)
I do have one environment (which I barely use) that has comments as a dull gray like the article has, and now that my attention has been drawn to it I realize I actually kind of hate it, and I'll probably end up changing the color scheme.
Exactly, and I second the personal preference point. It's not so much about highlighting specific things to call attention to them as it is about breaking up a homogeneous block of mixed text and whitespace into something structured and horizontally navigable.
Highlighting the noisiest and least uniform parts of code is probably not what I really want. I don't want to draw references from an entire book in title case, with tiny section and chapter titles for example.
Highlighting helps me identify code from a distance, it doesn't help me read it. It's about visual cues that eventually form into heuristics that save me time as I roam around a codebase. "Oh this is one of those" "that's not the kind of function that I'm looking for"
Semi-related: do folks actually use very low contrast themes like the one in the screenshot? My vision is better than 20 20 and reading light gray on white (or dark gray on black, for that matter) is a terrible strain on my eyes.
Edit: it appears the author has a dark mode theme on their blog which changes the colors of the screenshots
I think the default highlighting is fine because it's what's best for beginners, highlighting language keywords and such.
My vim is Solarized based so I can invert colors at night and get more contrast by collapsing the RGB values into just red using NegativeScreen.
My bash is more custom: to be even lower contrast, I don't use colors except for when commands return with an error code. Instead, I use font attributes a lot (bold, italics, faint, etc) and I timestamp my commands so when I check my records I can easily find which parts took me the longest to think and do. The command I am typing has the current directory in bold to quickly remind me as I usually have several terminals opened.
Here is what my bash (where I spend the most time in) and vi looks like: https://raw.githubusercontent.com/csdvrx/sixel-tmux/master/s...
My bash has changed to match vi: the # prompt and red error codes now use the background color with: \e[2;48;2;215;215;175m\]#[\e[0m\]
Negative Red=win+alt+F9 { -0.3, 0.0, 0.0, 0.0, 0.0 } { -0.6, 0.0, 0.0, 0.0, 0.0 } { -0.1, 0.0, 0.0, 0.0, 0.0 } { 0.0, 0.0, 0.0, 1.0, 0.0 } { 1.0, 0.0, 0.0, 0.0, 1.0 }
Example on: https://raw.githubusercontent.com/csdvrx/sixel-tmux/master/s...
For the last 6 years I use three colors for comments: https://i.imgur.com/vU783Xo_d.jpg?maxwidth=640&shape=thumb&f...
If you have a configurable editor you can find more useful things to highlight in the code as well: https://www.reddit.com/r/programming/comments/1w76um/comment...
This has slightly increased the number of comments overall and while I think quality has gone way up, I don’t want them more visible to the point of distraction.
Yep. Put me in the latter camp. If you made my comments more visable it would be highly distracting. Thinking about it like commentary is good because along with WHY comments over WHAT comments, I’ve been making an effort to describe INTENT for future readers (me).
- the 'supra-comments' are essentially used as "section headers", "block title / description", i.e. structural markers. It's thus logical to want them standing out, like titles in a text document or section borders in a spreadsheet.
- the 'infra-comments' are the "explicited subtext", "block/line anotation", i.e. substantive but secondary content (usually discussion about the code, which may also be externalized to a spec doc). These are naturally dimmed, because they sit beneath or outside the primary substance, the code (inline or aside, as in text documents and web pages, or even hidden behind a toggle).
In that regard, languages with two syntaxes for comments (eg. block "/* ... */" and end-of-line "// ...") may be formatted by editors respectively "standing out" and "dimmed", giving this two-level comment schema to the developer.
I personally think the 'supra-comment' / structural approach is detrimental to readability (longer, bloat, to me it's ASCII art however you want to spin it), and that problem should be solved by organizing code better (block order, files), choosing more self-explanatory names for everything, and having proper external documentation (however succint, it's not optional and can't be substituted by comments).
https://i.imgur.com/EKiVA0X.png
By the way @akkartik, it looks like your imgur link goes to the thumbnail rather than the image :)
I think semantic coloring makes way more sense than this or syntax highlighting because it helps you see the actual flow of the code, unlike syntax highlighting which helps you see the syntactic structure of the code (should be second nature to any expert) and unlike this which merely helps you see comments (which even in the example in the blogpost is secondary information to what the code actually does, which is usually the main thing I want to know when I read code).
I don't know what you mean about "when everything is highlighted, nothing is"—shouldn't they be highlighted different colors, often diametrically opposed colors?
With my coloring most of the time I don't even notice the semantic coloring except when I'm looking for it, especially when I type a long variable or method name and I'm thinking "did I spell this right?", if I see the same name somewhere else and if it has the same color, then I have high confidence I didn't make any typos.
Edit: Yep, dark mode mangled the code snippets.
Another thing: I think one thing he is missing is how important it is to see at a glance where function definitions are and where possible early returns are. The first example highlights def (function definitions), return (possible early return) and the return type (arguably not that important in most cases).
Try turning them off, it'll look much better.
It overrides the GTK theme used to render page content and still allows to use a dark theme (or whatever you want) for UI components.
Array.from(document.styleSheets).forEach(ss => Array.from(ss.rules).forEach((ssr,i) => {if (ssr.cssText && ssr.cssText.includes("dark")) {ssr.parentStyleSheet.deleteRule(i)}}))My favorite color scheme for a long time has been Made of Code, which I have slowly tweaked over the years. It has particularly great rendering of comments, where the line of the comment has a background color that takes up the entire row.
Example 1: https://s3.whalesalad.com/syntax/made-of-code-tweaked.png
Example 2: https://s3.whalesalad.com/syntax/made-of-code-tweaked-2.png
Theme file (Textmate/Sublime): https://s3.whalesalad.com/syntax/whalesalad-2.tmTheme
What matters most is to separate code from what's not code and which doesn't follow the same rules: comments and string-like literals.
In my vim color scheme (https://github.com/Canop/patine) I only have three colors: one for code, one for comments, one for strings, and I find reading the code easier and less tiring this way.
I'm currently (with new laptop) doing a thing where I try to install/customize as little as possible. This means not using that philosophy, but not because it didn't work.
https://github.com/mikelward/conf/blob/master/vim/colors/bas...
And a similar theme I wrote for vscode.
https://marketplace.visualstudio.com/items?itemName=mikelwar...
He said, "The hard part of coding in Python isn't getting the syntax right; the hard part is getting the logic right. Syntax highlighting makes me focus on the syntax when I should be focusing on the logic."
I didn't change my own setup, but it did get me to question my default assumptions on the practice.
That said, one specific example when syntax highlighting is very useful IMO is "rainbow parentheses". Especially when coding in Scheme, Racket, Lisp, Clojure, etc. But also for matching parens in fluent APIs and for matching braces in JSON.
Since then I've liked adding a little color.
I suppose that's exactly what's happening in your "rainbow parentheses" case when you have way better things to do than squinting to count those little bleeders.
I don't feel like I know how/if highlighting helps, so all I can really say is seems interesting.
I think some of the problems commenters are having with the color scheme are because the site has a dark mode and contrast might be worse when it's on. Maybe in that mode text colors meant for a dark background are still getting applied in the white-backgrounded code fragments.
https://en.wikipedia.org/wiki/Literate_programming
IME that gives all a starting point or base line for discussions around "what can more/good/better comments and support for documentation do for me".
Though maybe I installed a plugin to do this, I'm away from ny laptop at the moment (happy holidays).
I'm on the later camp, and I believe that's why I dislike the "movement" towards getting rid of puntctuation like (in JS land) semicolons, function braces and parens, and the like.
That helps me parse the code visually into its structure, while it must just appear like noise to somebody "reading" code linearly.
Now, for the kicker: Who do you think writes more blog posts about how much they like their languages and code highlighting, people in the "text thinking" camp, or the "visual thinking camp"?
#.h1 Auxiliary functions
#.h2 Date routines
function foo(x) {
#.summary {
// this foo does
// no bar, baz
}
#.fold {
assert(x != null)
}
}
code-style.css: .summary > comment {
font-size: 20px;
color: red;
}
.fold {
display: fold;
}
function-kw {...}
function-name {...}
.h1 {
margin-top: 80px;
}
comment {
margin-top: 5px;
margin-left: 10px;
border-left: 2px solid grey;
}
id[name=“assert”] {
color: green;
}
That’s what cascades stylesheets were done for. Another advantage is that we could create color themes with css, not with some cryptic barely documented config. And then toggle read/edit/toc like your wiki page. We could write entire technical documents in it.Sadly, that’s not possible as current mainstream stuck in raw text. We have all formatting for pointless blah-blah, but no formatting for a thing that manages everything.
This approach makes more sense to me though, as literate programming emphasises writing a book that incidentally contains source code, whereas the idea of valuable comments nowadays is closer to highlighting concerns or explaining functionality that isn't evident or easy to read in the code itself.
Or in other words; this sounds like a good idea! I wonder if it would help decrease the incidence of people ignoring and not updating comments though, as a developer's focus is often primarily on correcting the code and thus even with bolding the comments may end up falling into the same trap as banner blindness[1].
https://twitter.com/FPresencia/status/1133733608785473536
For copy/paste (in Atom -> Stylesheet...):
.syntax--comment,
.syntax--punctuation.syntax--definition.syntax--comment,
.syntax--comment .syntax--markup.syntax--link {
color: rgba(255, 51, 255, 0.8);
}
.syntax--comment.syntax--block,
.syntax--comment.syntax--line {
background: rgba(255, 51, 255, 0.1);
}
[1] https://jameshfisher.com/2014/05/11/your-syntax-highlighter-... /** Frobnicate the weevil with the given ID and return 0 on success.
*
* Locks the weevil during frobnication, which may take a while.
*/
int weevil_frobnicate(const char *id);
The most important part here is the function definition and its types and parameters. Usually, when I'm looking for a function, the name and type is enough to find what I'm looking for. So I want the function declaration to stand out and the docstring to be faded.The thing with important comments is that they are verbose, so they can span for example 5 lines (or 3 in this example). Because of this, they already have more attention.
I think most themes are pretty sane. There's a mixture of blue, darkblue and black for keywords and methods/vars. For contrast, strings and numbers are red and green. Comments are green, so they stand out enough for me.
In general, comments are not so informative and the code is self-documenting. ie., the code itself should be emphasized for comprehension.
The author misses the purpose of highlighting: to supply grammatical information where the language itself is deficient. Since we have plenty of experience with english grammar, we do not need help.
[1] https://www.sourceinsight.com/doc/v4/userguide/index.html#t=...
Ideally the editor would allow me to modify the graphics. Being vector, they would check into version control.
Also, I would like all function name definitions to be in bold, in a larger font.
I use Visual Studio to edit & compile, and Qt Creator. You've given me an idea: a VS plugin. Would a VS plugin be allowed to display the source code, I wonder? I'd pay money for that.
In my IDE, comments are bolded, italicized and red with a dashed underline and a mark on the scrollbar (thanks Rider). It's extreme, but it works perfectly with my code and commenting style. Doc comments are not treated this way, only inline comments, of which you may find one or two in an entire project of mine. I did away with non-doc comments a long while ago, and originally set this style in order to have any I'd missed stand out and "be ugly" so that I'd move the comments into a "remarks" section of the doc comments. The side-effect, though, is that when I'd run into a comment that was a "here be dragons", it'd stand out so strongly that you couldn't miss the warning.
So I left it that way for good.
MacPascal's bold keywords almost certainly descend from the Algol 60 report¹.
¹ http://www.softwarepreservation.org/projects/ALGOL/report/Al...
https://github.com/a-nikolaev/vim-boltzmann
- keywords are grey
- comments are red
- constant and literals are green and blue
- main text is off-white
Keywords often can be guessed from the overall structure of the program block and its indentation, especially in languages like OCaml. So making them bright only adds noise, distracting from what's important.Shrunken keywords might work. Not in conjunction with fixed width fonts, though.
The comments are the most important part of the document, yet they are barely visible due to colour and contrast chosen by syntax highlighter. Example: https://learnxinyminutes.com/docs/csharp/
I make them green, instead of gray. This makes my code ( IMHO ) much more readable. Also the comments ( when taken into consideration by the developer ) are one of the most important letters in the source code, so better be more visible than the keywords.
https://en.wikipedia.org/wiki/ColorForth
Basically in ColorForth, color is sort of part of the syntax. For instance when you want to define a new function, you would use the red color to type its name.
Perhaps syntax highlighting should detect comments that say WARNING, like some detect TODOs. But that is a separate issue.
If your codebase have good, sparse and accurate comments this could make sense.
But I don't see a lot of that. It's mostly low quality and outdated.
I want to see the code, because that's what's actually being executed.
if i recall correctly, pycharm knows about TODOs, WARNINGs, NOTEs, etc. not only it highlights these keywords but it also allows you to jump from one to the other throughout the code base. you can also customize how these are displayed.
so no, out of the box WARNINGs are not an issue for pycharm. speaking of that, i need to check if vscode has a plugin for that (since everything is plugin driven in vscode...)
WHY not WHAT really helped me out. WHY is this thing here instead of WHAT it is can give a line like i++; a vastly different comment and implication for the reader.
As a result, comments lie and can’t be trusted, and the only source of truth is actual code.
One fallacy I've seen is a reviewer only looking at the diff on Github, where a minor change in the middle of a function is made, and they don't expand the diff to notice that the new change completely invalidates a comment, params, call signature, return value..etc.