Code Comment Style
github.com
github.com
It was shockingly humble and transparent, and yet it seemed to flow from confidence, someone who had lots of experience in systems of all shapes and sizes. It was not signed, but I'm pretty sure it was by one of three coworkers, each of whom had years and years of experience running servers and networks. Its tone was a refreshing contrast to slick smokescreen recited by your average drone, in meetings and emails in the surrounding corporation.
And then there were no more comments. Yet the code was more comfortable to read, after that long intro, than most code I run into. I think it was two things:
(1) The author stated the intent. What was the file's purpose in life? Every line is easier to take in after you know that.
(2) The code was written by a human being, just like you and me. Usually code feels a little stiff, no doubt because the main audience member is a computer. A mild injection of warmth and humanity helps me keep my chin up when I face that daunting first read.
Would it have been better if it was more concise, spelled perfectly, and had a few line-by-line comments? Maybe. But it was such a treat I still remember it a decade later.
The Doxygen trend of just dumping APIs and comments at the start of functions and calling it documentation is horrendous.
https://blog.cleancoder.com/uncle-bob/2017/02/23/NecessaryCo...
I tend to prefer no comments, but more descriptive methods. There may be occasional exceptions.
I have also had situations where we decided "we are releasing in a month, cut corners and we'll deal with it later", and putting in TODOs greatly help deal with it later
I feel like comments belong in wrappers for unintuitive interfaces when a judgement call has been made to not replace those interfaces in-house due to project constraints. That's about it.
Part of why Worse Is Better is that relying on self-discipline and a healthy environment is a risky bet.
When code comments are presented as a failure to self-document, they are simply not written, irrespective of the actual clarity of the code. Add some pressure or toxic environment and everyone who's not able to grok your codebase won't complain about it — it is self-documenting! If you don't get it, the problem is on your end.
TODO: get the link for the study showing it’s about 10% faster to read code with module level documentation
// This code uses FOO because when it was originally written, we expected to process millions of items, so fast random access time was important. This is no longer the case -- feel free to refactor to use BAR instead.
// Do not refactor this code to use BAR! While this will make the function shorter, the result will have O(n^3) complexity, and this function may get millions of input items in some cases.
That is, whenever changes to the program are made, you are now required to also update the comment but a lot of times that last step is skipped. One may ask what's worse: the absence of comments or _outdated_ comments that no longer match the code.
For hairy code, I'd rather have an out of date comment than none at all. I can recall a lot more times wishing code was commented than having encountered a stale comment. And especially today, when code is usually in a VCS where you can blame the lines, it's not that hard to double-check if a comment seems off.
So, I think the absence of comments is far worse.
Also, from the git annotate man page[3]: The only difference between this command and git-blame is that they use slightly different output formats, and this command exists only for backward compatibility to support existing scripts, and provide a more familiar command name for people coming from other SCM systems.
[1] https://github.com/git/git/commit/cbfb73d73f272f194bafa70d5b...
[2] https://github.com/git/git/commit/c65e898754ef68a5520b279189...
That said, I wonder why ie. JavaDoc doesn't raise at least an informational message when a code block has changed, but its comment has not.
Are there IDE features that support this?
> Make sure the comment is up-to-date
Also, the most important than you can document is the why because given enough time, a good enough programmer will be able to figure out the what, regardless of how complex it is. But if you made an unusual decision based on something going on in your head at the time, maybe based on some data or testing you had done, no one will know that unless you write it down. This goes part and parcel with a good commit message, which is another place to write down the why.
If you haven't had the experience at looking at your own code a year down the line and scratching your head at some piece of it, well you just haven't been coding long enough. Good comments and commit messages are like gold at those times.
I've seen the following... These are all sins in my eyes.
// This code is the property of Blamo inc.
// Returns the result
return result;
// The name of the entity
public string Name;
// author: @author
// Date last changed: DD/MM/YYYY
/*
/ public property Name
/ The name of the Entity
*/
public string Name { get; set; }
I always try to tell people. DRY also relates to comments.Also if you are going to supply me with badly written inconsistent style guide that was cooked up internally with no references or reasoning. I'm just going to download a popular one from github, and have all the tooling sorted out-of-the-box. I don't negotiate with terrorists or religious nutcases.
You need a pretty damn good excuse to deviate from industry standards. Most of the time it is personal preferences and a need to be in control.
AKA. I care about standards, I just don't care about your standards.
I've found the following works really well: when doing something non-obvious then comment it immediately. Otherwise wait to see what arises from the Amigo Review. If the reviewer asks any questions, answer them with comments. Once the reviewer stops asking questions then the code is sufficiently commented.
“We don’t need to synchronize here because <some invariant>” and whatnot.
Basically, whenever a casual observer might think there’s a bug, either because you’re doing something unexpected or not doing something expected.
The reason given was frequently "it distracts the brain when reading the code. It makes reading the code take longer."
I'm sure whatever effect the very occasional one line comment has on reading speed is negligible.
When someone cherry picks something someone said once at a conference and forces it on everyone with dogma, you're not going to get good results.
what I dont understand is after all this time we dont have a richer framework for code metadata. all of those design, commit, review and issue discussions are basically lost once they are closed.
they should be indexed together. there's no reason why we cant use tooling to make this problem not just go away, but be substantially better for everyone.
I believe SourceGraph (sourcegraph.org) is working on exactly this
I'm a big proponent of intelligent tools with development.
I've encountered developers who look down their noses at people who use IDEs and their features, seeing it as somehow proving their lack of ability.
My argument is we're in the business of writing software. We should believe in the ability of software to make things better, including software development.
Also, I've often had a feeling that we could be doing more with our tools, different ways of viewing code to assist in understanding it and editing it.
I think developer experience is almost as important as user experience at the end of the day. And that UXers who understand coding should be hard at work making our tools incredible to use.
So best of luck with your mission!
- Java has package-info.java
- Scala has package.scala
- JavaScript/TypeScript has the @file annotation in the doc comment at the top of the file
- OCaml has the doc comment at the top of the file
When you want to document an entire sub-system, it makes sense to put the documentation in that sub-system's root module.
// I use the syntax for single-line comments
// even for multi-line comments,
// so that it's easy to temporarily comment out
// a block of code Comments are called for!
Why not in haiku format?
That will keep them short.- Donald Knuth invented an entire system of programming ( http://www.literateprogramming.com/ ) just to be able to write better documentation for his code
- Jamie Zawinski: 'I always wish people would comment more, ... You’ve got to say in the comment something that’s not there already. ... what is this for? Why would I use it?'
- Brendan Eich: 'It’s at the bigger level, the big monster function or the module boundary, that you need docs. So doc comments or things like them—doc strings. ... There is something to literate programming, especially these integrated tests and doc strings. I’d like to see more of that supported by languages.'
- Dan Ingalls: 'As soon as I have it working, I’ll write some comments. And if I like what I’ve done or it seems like it would be hard to figure out, I’ll write more comments.'
(Quotes from Coders at Work, Peter Seibel)
What is the reasoning behind this?
A bogus argument in my opinion.
I don't think
// x should never be 0
assert x != 0
is all that different from // set x to 1
x = 1A docstring describing arguments to a function would check that the list of arguments to a function is exactly as described in the comment. If you add a new argument, you have to edit the docstring.
And so on.
In fact, Im pretty sure Javadoc and other tools already have that markup.
## Comment Area Markup Method
Literary Programming, Programming was the first, Literary was the second.
the main purpose of the Code comment area markup method is to live Preview directly in the Code Editor Preview panel without exporting or any preprocessing.
Just add a line comment character of the programming language before each line of Markdown.
In the comments of code, you can draw flowcharts,tasklist, display data visualizations, etc.
The method is to add extension instructions in any programming language comment area:
- markdown
- manual eval code, live eval code, print result, display data visualization and other directives
When previewing or converting a format, you only need to simply preprocess: delete line comment characters with regular expressions, example: `sed 's/^;//' x.clj`
Note:
- line comment character of Clojure(Lisp) is `;`
- line comment characters of the current file type can be obtained from the editor's API.
when we edit the code, we can preview the effect in real time. Editing literary code has a live preview panel like most markdown editors.
## Advantages
- fast, live, simple, no interference.
- It don't break the syntax of any programming language, you can compile directly. comment area markup method can be applied to any programming language and any markup (including Org,rst, asciidoc, etc.), which is the greatest advantage.
- you only need a single line code to delete line comment characters using regular expressions, then you can use any Markdown parse or converter.
- Support any code editor that supports Markdwon Live preview, allowing the source code of any programming language to become rich text in real time. In the code's comment area, You can use the markdown to draw flowcharts, tables, task lists, and display images on the live preview panel, enhance the readability of your code.
- If you extend the Markdwon tag, you can implement the eval code, print result, display data visualization and other instruction tags, to achieve live programming, live test.
- When writing (reading or refactoring) code files, It can modify and live preview directly in the editor without exporting or any preprocessing.
- Reliable. Maximum code accuracy is guaranteed, and markup language errors do not affect the code.
- It hasn't interfere anyone to read the code.Markdown is simple, so if it doesn’t have syntax highlighting,it doesn’t have much effect on writing and reading. And having a gray comment area doesn’t affect reading code, especially for people who don’t understand the markup language.Strict distinction between markdown and code, and gray comment area can reduce the amount of information in the source code file, conducive to reading code.
## Disadvantages of traditional literary programming
- because traditional literary programming users are mainly technical writers, speakers, technical document Maintainers, Style is the document priority, greatly increase the amount of information in the code, interfere with the code reading, especially for non-literary programming programmers are unfriendly, or even unreadable, so there are very few applications in the field of programming.
- not universal, specific programming languages and markup languages.
- Requires a complex pre-compiler.
- Complex to use and high learning costs.
- Not intuitive.
Therefore, the method described in this paper, in addition to the document-first genre of traditional literary programming, has innovated a new genre ---- code-first genre, so that literary programming in the field of programming Widely used as possible.
[1]https://github.com/linpengcheng/PurefunctionPipelineDataflow...