Syntax highlighters are wrong (2014)
jameshfisher.com
jameshfisher.com
I find this whole article highly speculative. It's based on principles the author adapted from somebody who wrote a book about how to write code, but I doubt they really influence or apply to lots of people.
I don't value comments less or more because they are cursive or light grey. Code highlighting should help to easily distinguish between different parts of the code, but as unobtrusive as possible. This is not an easy task especially with the limited set of options you have for plain text.
I also don't perceive removals in diff view as "bad" because they are red but as "look here, could be important".
So this article is about the feelings of the author, not so much about facts.
Could you imagine the mess if you were writing an API that required JSdoc with the author's proposed highlighting scheme? Or Golang's enforcement of comments on public functions?
That's really the problem with using red for deletes in a diff. Red implies that the deleted lines are more important than the added lines, and that's wrong. You shouldn't (and can't really) focus exclusively on the deleted code. Breaking the 'red = important' connection and using a color scheme that gives equal weight to the removed and added lines may well work better once you get past how weird it is.
I've never gotten that impression. I and my coworkers & classmates usually highlight text using the standard yellow highlighter, occasionally using other snazzy highlighter colours - red sometimes included, but usually not.
The associations that leak in from other domains are green=good/go, red=bad/stop. It's very easy to mentally remap the colours to the intuitive green=new, red=old.
This is strongly and patently not true. Here in Japan the available taxi is in Red while the taken one is in Green, and there are many more similar examples. Admitedly it's mixed, because traffic lights are still red to stop and blue to cross. Wait blue? There was no distinction in Japan between green and blue back in the day and that still remains. Colors are very much culture-dependent, and saying otherwise is ingenuous at least and misleading at best.
That said I don't think it matters. The benefit of not making removing lines look like a bad thing is definitely outweighed by the cost of not making it obvious which colour means which. Was olive for lines added? I can never remember...
Your example of Japanese taxi signs using red to signal availability is an interesting but rare exception to the general rule across the industrialized world that red={warning, danger, no, stop, delete}, and green={safe, good, yes, go, maximize}. For this example, there are many counterexamples. After all, Japan uses red=stop, green-blue=go on all traffic lights, railways, shipping, and aviation, in accordance with international conventions.
Yes, colours are very much culture-dependent, but red=bad and green=good (or green-blue for cultures that don't distinguish these two colours) has been intentionally adopted across cultures for generations -- dating back to shipping and train signals, if not much earlier to verdant countrysides versus blood, fire, etc.
In China it is understood that red color is used to indicate STOP because it has the greatest wavelength and is therefore more visible in inclement weather.
In addition, color code for traffic light is fixed by international convention and it’s not like China can choose whatever it wants.
My point is, culture has nothing to do with the choice of stop light color in China
That's exactly my point as well. The article mentioned that the association between red and "bad" stuff (stop, error, etc.) is cross-cultural. Someone replied vehemently denying that, but the fact is that red={stop, error, bad, ...} is a long-standing international convention that does supersede regional cultural effects.
China can choose whatever they want, but don't.
Many countries have not signed the Vienna Convention on Road Signs and Signals. For example China, Japan and the United States have not signed the convention.
Japan is a good example: "It is a near universal constant when driving: red means stop, and green means go. So fundamental is this dynamic that it is codified in international law under the Vienna Convention on Road Signs and Signals, which has been ratified by 74 countries. Why, then, does Japan — not a signatory to the Convention —seem to buck the trend with its blue/green traffic signals?" https://www.atlasobscura.com/articles/japan-green-traffic-li...
In modern UX/UI, red is universally understood to mean delete, close, stop, error, and similar "bad" or destructive things. If you localize OS X to Japanese, does the "close window" button change from red to something else? If you localize GitHub to Japanese, does its irreversible repo deletion confirmation prompt appear in green? Is the "proceed to GitHub" button at https://github.co.jp/ green or red?
- documentation, that should be parsed by some external tool (javadoc, godoc, etc.)
- warnings in the code, like the "this call is very important because of..." example from the article
- commented out code.
I want warnings to sitck out, but I don't want doc to stick out.
Commented-out code should not exist, granted, but in the meantime I don't want it to stick out either. If I'm commenting-out my own code, that's because I'm trying to understand some issue, so please don't distract me. I'll remove it later, I promise.
This is where multiple rulesets could come in handy. Your editor which you're using for debugging should keep the commented-out code nice and subtle, but when you go to commit it should be bright purple with yellow spots.
The current "fashion" of making comments as unreadable as possible in most syntax highlighter themes is indeed my main gripe with them.
Comments don't have to particularly stick out, but it would at least help to make them as readable as the actual source code.
There are nice doc generators that allow natural language with markup only for special attributes.
If you need to read the source code to use a library, something is wrong with that library.
(On the other hands it is fine to document local functions, but it doesn't need to be a verbose documentation comment.)
I tend to classify libraries into two classes, something I can obviously make but I don't have to, and something I would have to try hard (or even be impossible) to make it. You can't quickly understand the second class of libraries by reading the code. If you need a concrete example, take miniSAT [1] as one.
[1] https://github.com/niklasso/minisat/blob/master/minisat/core...
https://github.com/nothings/stb/blob/master/stb_image.h
It's a complete image loader library in a single header. This header contains everything I need to use that library: the entire documentation, the license, public API declarations and finally the implementation source code.
I can read this file from top to bottom to know every little detail about this library, or I can stop somewhere inbetween (for instance when I only want to know how to use the library and what the API looks like, but I'm not interested in the implementation details).
Also note that stb_image is extremely stateless. The documentation is not a must for stateless APIs because it is relatively easier to infer from the signature (of course you need to learn the convention, for example, to see that `int x, int y` etc. are out parameters and should not be NULL though). But many other libraries need states, and a proper documentation is required.
And I think it could be really useful and commented-out code would still be highlighted, but in a washed out way or some other way that emphasises it's not currently active code.
But those who write Javadoc well, will put important stuff in there. And those who write non-Javadoc comments badly, will put useless stuff there.
Looks like an AI-complete problem to distinguish important from unimportant comments...
E. g.
// do two iterations of bubble sort before // falling back two qsort // because 99% of data comes almost sorted
This information cannot be obtained by reading function body, thus it should be added to comment.
In my opinion, you should never commit commented out code. Here's why:
1. Commented out code, just confuses and clutters people's interpretation of the project. It's meaning is not clear as it's not a functioning part of the system.
3. It can incorrectly inflate the apparent complexity of a project.
2. It reduces the impact of meaningful comments, so people end up ignoring all comments.
Commit a working-state of the system only. If you're working through a problem, save the functions without commenting them out and completely remove them when you don't need them... you can always go back and find it in the source-control later when you need it.
Also, one of the first things I do when coming onto a new project is (switch to a new branch) and remove all the dead code.
> In my opinion, you should never commit commented out code.
But the editor works on uncommitted code, too. GP doesn't want to be distracted by that.
It could mean people are literally looking over his shoulder at his screen and complaining. But to me, it implies that commented code is becoming part of the project source...
Don't want it running in production because it's a whole bunch of database queries that just tax the DB, and production volumes are bigger than test cases.
Dead code, but useful dead code.
(Whether excluded by switch or comment makes little odds; it has no value in being turned on dynamically, so it's statically verifiably dead.)
Red is a lucky/auspicious/'good' colour in China and parts of S/SE Asia. Red dresses, for instance, are traditional at some cultures' weddings.
I think the author is just conflating the commonality of traffic symbols as some deep-rooted, cross cultural phenomena.
*always "bad". Some bad examples, red is associated with the evil political party i.e. Republicans.
/* regular comments */
// regular comments
/** Javadoc comments */
/// Javadoc comments
This convention is understood by many syntax highlighters.However, regular comments are frequently used to "comment out" code snippets while working on code. I assume "graying out" follows the "commenting out" idiom.
The colouring preferences of visual diff's are a completely different topic. IMHO, red/green is just a widespread convention. Of course it mentally supports the bad/good association, which is seldomly correct (even in other contexts as code).
// comment that explains what the change was and why we are keeping the old code around.
// Date we made this change.
#if true
new code
#else
old code
#endif if false:
this(does not compile)
else:
that.might("be")I see this the opposite. I love commits with tons of red, and dislike commits with lots of green. Green means more reading and trying to figure out what this new code does, whereas lots of red with a little green means someone cut away all the old ugly code and replaced it with something better.
If the colours were to be reversed, I would get really confused. I guess I've sufficiently internalised the current usage, and I'm fine with it. I suppose I should ask a younger programmer how they view the red and green in commits.
Edit: His "neutral" addition/deletion colours don't really work for me. They don't have the clear association that red/green has. And the red = bad, green = good works fine if you want to remove bad code and add good code. It's never that simple, obviously, but the colour work very well for me.
The colours don't work so well for red/green colour blindness, obviously. That's a good reason to use something else. But maybe every highlighting tool needs to be configurable to use the colours that work best for you. Most are, but is github?
For the diff colouring, I've always read red lines as being "bad" in the sense of "these ones had to go" (not in the sense of "these changes are wrong") and green lines being "good" in the sense of "these are what they should have been."
This is exactly backwards in my experience. Comments can lie. Computers don't execute comments. When reading code for understanding, you need to treat the comments as at best a guide and read the code first. Sure, there are rare cases where an algorithm is so subtle it needs to be explained via other media, but even then a simple link to documentation or a textbook reference is IMHO a better guide. Comments are awful in practice. Stay away.
Whether that has anything to do with syntax highlighting is an open question.
Instead of comments just to make clearer the intent of the code, now we had highly repetitive comments in boilerplate format that would add nothing to already clear code.
As comments went from being one thing "make intent clear, shout why something happens" to multiple things "generate docs" + "pass commit linting"... the value of a comment diminished fast and so it isn't surprising to me that syntax highlighting downplays the comment.
What is surprising is that we haven't yet defined multiple types of comment: 1 for documentation, 1 for clarity. Or that we haven't determined the context of a comment and chosen to highlight depending on that context.
My syntax highlighter does do this, for every language I can think of - and so does essentially every code editor software I can remember using. The most popular editors even have the power to collapse doc comments, and I believe some do by default.
In my editor I switched off all my syntax highlighting except for two things: all comments are in bold (for I agree with the author that comments should be in your face) and comments marked with "TODO" are in pesky red, nudging me to fix the marked issue ASAP.
For example in Emacs https://www.emacswiki.org/emacs/AutoPairs
I feel like my ability to read code improved from disabling any other highlighting, but I have no real way of testing whether that's true.
In this case 0x1_0000 is the least-significant bit of the third byte.
If that's a bit flag encoded in a 32bit value, I also like 0x0001_0000 for clarity.
Here's the line in question: https://github.com/jwilm/alacritty/blob/master/alacritty_ter...
I agree 0x1_000 or 0x10_000 would make more sense, but it doesn't look like the actual value used is hugely significant.
[1] http://colorbrewer2.org/#type=qualitative&scheme=Paired&n=3
That said, if you think color accessibility is about picking the right categories of colors you have a long way to go.
The important thing is that the colors need to have sufficient contrast beyond mere hue. Light green and deep red is fine for me as a strong deutan (i.e. severely green blind) even though there are combinations of "red" and "green" that are practically indistinguishable for me.
Obviously accessibility is about more than picking colors, but if a complaint is 'this is a bad color pair' it is interesting to know what is a better pair.
As for contrast beyond hue, does that include saturation? Because keeping luminance constant is an important part of keeping an interface legible.
I'd say a red-blue default with full configurability might be the best approach.
I believe something like orangish red and bluish green are distinct enough to most people with a red-green deficiency (including myself). The difference in ”blue content” makes them distinguishable.
So, why use a white background? Because it may feel better, and it may have nothing to do with paper.
Also, an objective reason: with black background in your text editor, it can be painful to switch to windows with a white background. If you try to set everything black background, you run into bugs: broken webpages, mails that set a white background anyway (bonus if they don't set the text color, or nothing is set but some words are highlighted or colored), documents, PDFs. I know because I tried. With support for dark themes in CSS it will probably improve but we are not there yet and you pretty much are in an unsupported territory.
The question also work in the opposite way: why use a dark background? Same answer: preference. For ecological reasons? It depends on the screen, and it should be measured especially if you compensate by setting the screen brightness higher.
No value judgement intended, and who doesn't love to see a diff with a ton of red replaced with just a little green? Someone was cleaning house!
Another pet peeve I have is why tf do all color themes try to color variable vs. functions vs. fields differently: they never get this right in dynamic languages, and they couldn't anyway bc variables often hold functions in them.
Basically all modern / editor-default color themes are dead wrong and annoying!
(VSCode and Visual Studio ones may be on point occasionally, but they fail at dynamic languages too and only end up spewing meaningless distracting colors everywhere.)
Most colourschemes out there are absurdly low-contrast. Part of my inspiration in finally getting round to making this colourscheme, and going for a light background, was experience several years earlier of using Vim on a not-too-bright laptop on a bus with substantial sunlight: I always had to switch to a dark-on-light colourscheme to be able to read the code at all.
A few: side effects, ordering that's required for correctness but not checkable by the language, guarding performance tricks from getting deleted.
An unfamiliar person trying to find out where in the codebase a thing happens relies on comments. Comments are much more readable and much more searchable than functions, in the same way that blogposts and comments add semantic terms to web documents to improve search.
Also intent -- to guide us in situations where 'this function is doing undesirable things but I have no idea what it's supposed to do so I can't fix it'.
Yes they get stale if not updated; it's a living spec. But most teams don't update any specs so code & comments are still the best one we have.
As for syntax highlighting, I'm not really sure what we want out of it. I like it for typo protection; type "cnost" and it doesn't turn blue, fix it to "const" and hey! pretty! It must be right. But it doesn't go far enough. If you call "foo.bar()", Github turns "bar" blue, regardless of whether or not it's possible to call "bar()" on "foo". My editor doesn't highlight those, but it should; if the method is callable, make it pretty, if it's some made up thing that isn't going to compile, make it ugly.
I think, at best, what we use syntax highlighting for is to break up what seems like a monotonous large quantity of text. Kind of like how I started a new paragraph to emphasize this point; it does help the reader. But I think we could do way way better, it's just that people seem to have given up. I think the excuse is along the lines of "it's too slow to use a 1970s era computer to figure out what methods are available"; that's true, but we are not using 1970s computers anymore. Why are compilers or syntax analyzers so slow, anyway? We can play real-time multiplayer games that render 240 snapshots of a 3D virtual world every second... but compiling hello world takes on the order of 200ms. Seems strange. No wonder our tools are so bad. What we're doing must be very hard for the computer.
(Github, however, has no excuse. They have all the time in the world to produce good syntax annotations; it's not like you're typing new code into that window. And their syntax highlighting does significantly worse than any editor I've ever seen.)
Red/green for deletion/insertion is less or more standard across tooling. It's not a quirk of GitHub and if you're going to make an argument about it shaping our value judgements about code contributions I'd like to see an actual paper or study please.
As others have pointed out, comments can have a lot of different reasons to exist. Inline documentation can be useful for tooling but should fade in the background when looking at the code directly. Commented out code generally should just be deleted but there are some rare circumstances where you might want to leave it.
Since we're already deeply in "unfounded personal opinions presented as fact" territory, I really prefer when important comments are explicitly marked using a standard keyword like "NOTE", "WARNING", "FIXME" or "TODO" because editors can be configured to highlight these lines differently, completely avoiding this flaming mess of an argument.
To be fair though, three line comments for every member seems a bit excessive to me as well.
Yikes. That's probably not the best way to approach improving usability - hectoring users that their interpretation is wrong.
Or, the author is asserting that their approach is better without any real evidence other than their own line of reasoning. There's a great deal of arrogance in this post, like the assumption that red and green have universal connotations (just look at the chart emoji to see a counterexample). Do Japanese programmers have inherently better code because the existing red/green semantics in Japan match up with the author's argument? Probably not. It's an interesting couple of ideas but without meaningful data I wouldn't consider making these changes.
I don't think I've ever looked at some code and thought I wish there were less comments. I don't see why comments have such a lowly status. Of course you can tell when someone wrote a comment because they felt they HAD to...stuff like:
/* Adding one to X */
x = x + 1
Which I've actually seen...but anything even remotely intelligent is generally welcome, I think.I once read somewhere that comments should explain the reason rather than the action. For example:
/*
x is taken from a system counter but it needs
to be consumed by humans. So we add 1 because
humans don't count from zero
*/
x = x + 1
is a more useful comment.On the other hand, comments like, "Hey don't change this because if you do, it'll break things in subtle and hard to test way because x," are definitely useful.
> In that specific case, potentially - bare in mind it's imaginary code you're refactoring. There will be occasions when "ugly" code is required.
ie, I don't disagree with your point however it's not always possible to refactor code for readability. Plus you're still missing the point that writing better comments can give you additional information ("why does it do it") that you might not gleam even from even well written code ("what does it do").
[1] The following (amazing) article discusses code comments at length:
Important "must read me" comments can be accommodated in syntax coloring via some additional notation:
//
// this is faded toward the background
//
// ! this is loud
// ! read it!
//
And, you know, unless I'm focusing on that area of the code for a specific reason, I probably don't want to read that. Should I need to turn my attention to that area of the code, I will read it, even if it is tastefully dimmed out.One problem with a loudly colored comment is that there is no "marked read" button once you get the message, like in a message inbox. No matter how many times you read it, it stays lit up. (Maybe there should be such a feature. If a browser can remember what links you have visited, and show them in a darker color, a text editor could keep track of which loud comments you have already read.)
What's else, one could make a similar argument on the use of + (a good thing) and - (a bad thing) to say that removals should be marked with +, and additions with -. Maybe switch to sad and happy emoticons?
I am pushing it a bit just to make a point (because yes, + is even more commonly used to indicate addition, and - to indicate substraction or, well, removal). There is this whole science dedicated to making reading nicer, more efficient and more comfortable, and we want none of it.
I think there are better ways to make code more readable in our editors than changing a few colours.
Inspired by Knuth's literate programming renders of TeX and METAFONT source code, I've experimented with variable-width fonts for code (why are we programmers not allowed to read beautifully typeset text when everyone else is?), but editors today are not really comfortable with that.
I immediately prefer the faded comments because they are there if I want to read them. I'm not going to struggle to understand and reason about the comments.
I may well find myself needing to re-read and reason about the code that follows, so it's good that it's easier to follow the code without distraction.
I also think most syntax schemes (the highlighter is perfectly fine, let us be technically correct) are very wrong for my own purposes.
This thing is the wrong colour, this looks like a candy party, some colours should be used only for such and such thing, strings should always be this colour, etc.
So, I made my own syntax colour scheme with my own conventions, and it is great.
But I don't try to convince anyone to use my colour scheme. That would be preposterous and intrusive.
By focusing on what I completed you give a much more helpful vibe, and you can reserve red for destructive actions, like permanently deleting something.
That was five years ago. It felt weird for about ten seconds. These days I find code with syntax highlighting very noisy and almost impossible to deep read.
It's quite possible that it was always like this, but that I was numbed to the effects and lacked reference points.
Whatever the case, I don't miss it at all.
- Comments and string literals can look like code without being code, so they get their own separate colours to make it easier to quickly scan past to where they end.
- Types live in a different dimension from the rest of the code, and tell me a lot about what it does, so they get a different colour.
And, finally, the one I've been trying for a while but am still not convinced about:
- Structural keywords (for loops, branches, etc.) get a separate colour as well.
All these colours are of course relatively neutral. I'm especially confused by people who use bright red for anything. That's a colour I use extremely sparsely for actual errors and bugs.
One of the downsides is that when you show your code to other developers who don't have the necessary visual bandwidth, they get uncomfortable.
One of the few things syntax highlighting actually helps with is for catching typos in keywords. (To the trained eye, this could be replaced with just bolding or italicizing the keywords.)
Today I'm using "the colorful words" again (as non-technical associates have described syntax highlighted code), and still looking for a syntax highlighting theme which does not look like crap. (Gruvbox is the closest I've gotten yet.)
Giving each identifier its own color (e.g. https://medium.com/@evnbr/coding-in-color-3a6db2743a1e) is another interesting idea.
Well: if you regarded the removed code as better than what you're replacing it with, you wouldn't be committing these changes, right?
This makes me think it would be nice to have at least two functionally distinct comment types: remarks (to be visually hilighted), and temporarily disabled code (to be visually suppressed). I suppose comment lines (remarks) and comment blocks (code) could be used for this, but as the author points out this would need to be a widely adopted convention.
And to that I say, change it. Editors these days are designed to make color-scheme changing as easy as possible.(I can confirm this about vim, emacs, sublime, Atom)
In reality though, code isn't poetry. C, Pascal, Javascript, etc. aren't meant to convey the thoughts of the programmer, but to give a machine clear instructions on what to do.
Humans aren't good at reading code. If we were, natural languages would have evolved like that, but they haven't. The idea of writing code for a machine and adding meta-information for humans to better understand the context isn't a bad idea, it's efficient.
What's more, when your code really does perfectly convey the intentions of the programmer and makes comments unnecessary, then your program is encoding information that the computer doesn't need, in a format that makes it harder for the computer to distinguish between relevant and irrelevant information.
They might attempt to capture that knowledge in a git message, but that is destined to be burried in the sands of time.
For example, in a recent file that deals with the database:
// note: the ROW() constructor below is required in Postgres 10+ if we're updating a single column
(There was a syntax change from Postgres 9 that was caught by our tests) // these are the only meaningful values in Postgres:
// see https://www.postgresql.org/docs/11/sql-set-transaction.html
(Justifying why we don't support the whole SQL-standard range of transaction isolation types) // on trapping the following two rollback error codes, see:
// https://www.postgresql.org/message-id/1368066680.60649.YahooMailNeo@web162902.mail.bf1.yahoo.com
// this is also a good read:
// https://www.enterprisedb.com/blog/serializable-postgresql-11-and-beyond
(An audit trail for why we catch some error codes but not others that initially look similar)All this takes time.
Comments are just a way of speeding up the process of "contexting up". Typically it's done by saving a bit of that mental context as a comment.
Ideally you want to save a piece of context that's easy to express, but took a long time to understand in the first place.
It is true that people often take these ideals too blindly and their beautiful architecture brings little in comparison with simple obvious code. (I'm often a culprit there.)
Code overspecifies. As long as there are programmers, the clearest code in the world should still have comments indicating intent. And comments won't save unclear code.
Give it some more time. I know it's far fetched, but chances are once human-computer interaction (HCI) is accessible for all humans and they transform into cyborgs (hybrid human and machine), code would be a more efficient means of communication as it is the perfect compromise between binary code (that pure machines understand) and natural languages (that pure humans understand).
The other problem with comments is multiple maintenance. It's very common for a change to be made to the code but not the comment. Now you've got lies misleading the next poor developer who dives in there.
Documentation comments can be useful, but I cringe when I see lots of comments per block or statement.
> Humans aren't good at reading code.
That's something you can get better at, but it requires discipline and effort. Programmers who have no intention of getting better at it think the comments will save them.
Yeah. And people who make memory errors in C are just bad programmers. At some point people will have to accept that their preconceived notions are just that and not some mythical rule that others have to discover to find enlightenment.
Find me one programmer who's as fluent in C as in any natural language.
1. Commenting in the redundant way
2. Avoiding comments where possible
3. Using comments to convey intent in public APIs, and occasionally to explain how something works inline.
#3 came from working totally remote for a year.
(On my Visual Studio theme, comments are green on a dark grey background. What's the significance? Probably none.)
I think the same should be true of type annotations. They are important to the compiler and as documentation, but should be deemphasized relative to the actual logic.
"What" comments tell you what the code is doing. In a lot of cases, depending on the language, the need for these can be reduced by writing clear code. This is much easier in, say, Python than Assembly. Even in Python though, sometimes you can be doing something a bit subtle where a 2 line comment can clear things up. These comments aren't irreplaceable because with a bit of reading and work, you have all the information to work out what is happening.
"Why" comments are much more important - telling the reader WHY the code is doing whatever it is that it's doing. The 'trim()' comment referenced in the article is a great example of a Why comment - all the reading around the code wouldn't give you an explanation (although sometimes git blame will).
Many 'what' comments are superfluous, almost no 'why' comments are - they are the collective memory of design decisions that otherwise lives in people's heads.
Right, as long as the commit messages talk about the 'why' instead of the 'what'. (e.g. 'Make it look bigger on mobile', instead of 'Add META tag with viewport and scaling')
Also, I'm not sure if calling Javadoc interface documentation "redundant" and "obese" is warranted. If it feels useless, it is not verbose enough.
The code needed comments because it crossed layers – in general a sign of bad code. So the idea of "if your code needs comments, there's probably something wrong with it" definitely holds.
Sometimes you need not to do bad things to handle a bad situation (e.g. handling a third party embedded & unfixable system working against the spec), but that does not invalidate the fact that comments are an indicator of "somehow flawed code".
The vendor knows their protocol is buggy, because in their PC maintenance software they have arbitrarily chosen to start frame sequence numbering one higher than the escape character so that they can avoid triggering the bug in most cases. It still breaks down when the payload contains that prohibited escape code. Service techs are used to the whole thing randomly requiring retries and constantly joke about how repairing the $vendor's stuff has the best billable hours per work done ratio.
What about comments that describe our assumptions? I guess that's a special-case of 'what' comments. Such comments are often better expressed as assertions in code (i.e. adding more code to improve readability, rather than reworking the code). Assertions have the considerable advantage that they don't go stale as the code is changed.
> although sometimes git blame will
That's a great point. Detailed commit messages are a good way to record the 'why' points. Unlike in-code comments, they don't bloat and dilute the source. Of course, they're also less immediate. (I guess I'm not sold on comment-folding as an answer to this.)
It's also a point in favour of fine-grain commits. If you squash your commits, 'git blame' becomes less able to help you track a line of code back to its original motivations.
Knowing the assumptions gives the reader the context for why certain decisions were made, and why they may still be valid decisions or not.
I agree that requirements-level assumptions (We assume the Vogon server will allow us to make at most 4 connections at any time) are exactly the sort of thing that should be captured in comments. I'm not sure there's a bright line between assumptions and requirements here, really.
My other point there was that any non-trivial code-level assumption that can reasonably be captured with an assert, should be captured with an assert, in preference over a comment.
That way when I go to update this code in the future I know if and why I have to worry about if x could be negative now.
It's a great idea. One of the best. I think many people will agree with that.
But there needs to be an equally effective way to keep the comments up to date. Because in my short experience programming professionally, the insidious part about comments is that as soon as one is shown to be a lie, none of them are reliable anymore. And it's very easy for a comment to become a lie as code evolves.
This is why things like unit tests are more attractive as a form of documentation - if the tests lie, the build fails, forcing you to fix lies.
The solution in both cases is the same: when you find problems, fix them. Don't throw out the baby with the bathwater.
For example, I looked up the SQLite source (which often comes up here as an example of a well-written program), picked the first source file I saw (alter.c), and one of the first comments is this:
/* The principle used to locate the table name in the CREATE TRIGGER
** statement is that the table name is the first token that is immediatedly
** preceded by either TK_ON or TK_DOT and immediatedly followed by one
** of TK_WHEN, TK_BEGIN or TK_FOR.
Another great one: ** Note that ON cannot be a database, table or column name, so
** there is no need to worry about syntax like
** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
What unit test would you write to convey this information? Tests can be helpful but they serve a completely different purpose. You can of course write a test to cover the same ground as these comments (and knowing SQLite, they probably did), but a unit test to poke at corner cases is nowhere near as clear as simply stating what's going on.I think the article means to suggest that one possible way to nudge programmers to keep comments up-to-date is to have editors that don't make them appear "washed out".
Yeah this is called code review. If someone repeatedly submits code with comments that are out of date then their manager needs to have a one on one with them.
[1] : https://doc.rust-lang.org/rustdoc/documentation-tests.html