Documentation — How, What, and When Should I Comment?
source link: https://devm.io/javascript/javascript-documentation-comment
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Hitchhiking through the JavaScript jungle
Documentation — How, What, and When Should I Comment?
Documentation usually ranks behind testing in the most loved topics in software development. And after that, there’s usually only topics like endlessly managing legacy code bases that you haven’t even developed yourself. I admit, I don't really like writing comments in code. I don't really think much of it. However, there are some places in your code where documentation in the form of comments is absolutely necessary. So, let’s have a look at why I find a lot of comments to be nonsensical, and why it’s good form to be prolific in comments elsewhere.
My code is self-explanatory, I do not need comments
The book “Clean Code” was published in 2008 and much of it is still valid today, even if Uncle Bob, his views, and public appearances are up for discussion. Among other things, “Clean Code” teaches you that you should write self-explanatory code that speaks for itself. What do comments do in the code? In many cases, they explain what the code does. Why not write the code so that it’s obvious what it does?
In any case, this sounds like a good idea. But it's not that simple. What constitutes understandable code is up to the individual. What seems obvious to one person may be completely incomprehensible to another. But the solution isn’t to ask for comments in the code review explaining every single line. Here, it’s much better to sit down together with the code and rewrite it so that it’s understandable for everyone involved. Because of this, I think that pair and mob programming are excellent approaches for producing high quality code. Ultimately, the discussion about comprehensibility in the development process moves forward, to when the code is created and not just after the damage has already been done.
But back to our actual topic: comments. Basically, comments are completely superfluous for the JavaScript engine. Developers write comments either for themselves or others involved in the project. Hopefully, we agree that comments like the ones in Listing 1 have no place in our application’s code.
Listing 1: Comments in code
function doSomething(x, y, z) {
if (!x) {
return false; // return false if x is falsy
}
// ---- first block ----
const value = x + 10; // adds 10 to x
...
// ---- second block ----
...
}
One problem in this example is that some of the comments are obvious. You should avoid comments like this if possible, since they tend to disrupt the reading flow and merely point out facts in the code. Similarly, comments visually divide functions into multiple blocks. If you see that this approach is becoming common in your project, there’s a simple solution: outsource the individual blocks to functions and give them a meaningful name. Mostly, the separator comment already gives you a good hint about potential function names. Admittedly, this isn’t the case in our example.
Inline comments can make sense in some places in the code, namely if they clarify the developer's intention that led to the corresponding code location. And this is only if it’s not immediately obvious. Technical requirements in algorithms are good candidates for these comments, as they can be easily misinterpreted as errors in the code. Here, it’s always good if the comment (or at least the commit containing the comment) contains a reference to a ticket or other further documentation.
But why do inline comments have such a bad reputation? Basically, they’re unnecessary in cleanly implemented code, as the code speaks for itself. Additionally, the biggest danger of comments is that they become outdated. If the source code...
Recommend
-
127
commentary.vim Comment stuff out. Use gcc to comment out a line (takes a count), gc to comment out the target of a motion (for example, gcap to comment out a paragraph), gc in visual mo...
-
118
Comment Your Code Nov 17, 2017 There’s a disturbing thread that pops up every once in a while where People On The Internet say that comments are bad and the only reason you need them is because you and/or your code aren’t g...
-
122
Reddit (Au) Comment Highlights by EasyHighlight unread comments since your last visit to a Reddit comment section.
-
128
StackOverflow comment threads, in a nutshell Add to FavoritesShare
-
9
Github based Comment System, and the Death of Independent Blog? 2020-06-21
-
4
Ask Buffer: Should I Reply to Every Social Media Comment? Expert tips for how small businesses can handle replying to social media comments. Should you reply to every comment? How qu...
-
5
Discussion and Comment of the Week The DEV team is starting a new weekly roundup series to highlight what we believe to be the most thoughtful and/or interesting discussion of the week. We'll also be highligh...
-
1
6 Process Documentation Mistakes Developers Should Avoid May 3, 2022
-
0
Welcome Corner Discussions Join the Welcome Corner discussion, where newcomers and community veterans...
-
5
Iris Classon - In Love with CodeComment and Discuss With Disqus!2024-03-17 13:14 folder
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK