Writing Comments in a Good way
by Pabashani Herath
Last updated
Was this helpful?
by Pabashani Herath
Last updated
Was this helpful?
All the knowledge from this series is based on Robert C. Martinβs βClean Code.β I hope I can encourage people to read this book and write better codes in this way.
In our code and code formatting, I will discuss commentary in this post. My aim is to try and figure out when we are supposed to add comments to our code. Because developers need to read the code every day, we have to use a good design style to make it easier to read it.
"Donβt comment bad code β rewrite it." β Brian W. Kernighan and P. J. Plaugher
Bad code is one of the most common reasons to write comments. We write a module and we realize it is disorganized and confusing. This is a mess, we realize. And weβre telling us, βOh, Iβd like to comment better! βActually, not so! You better clean it! You better clean it!
Simple and concise code with a few remarks is much superior with multiple comments to cluttered and complex code.
"Instead of wasting your time writing the comments that describe your mess, spend cleaning up this mess."
Itβs time to discuss coding comments. I think that three things can be generally: good, bad, and funny (especially if you donβt have to deal with it). Now, letβs think of the comments β What kinds of problems are focusing on? β.
We can rapidly modify and test software behavior, even in production.
Software licenses can be entered in the code.
We can supply some information to a different developer etc.
If we consider comments in code, we have comments in mind which clarify what the code is doing. The challenge with comments is they are not always published. The code is modified quite frequently, but the old comments are similar. Therefore, the comments no longer represent the code.
"The best comment is a good name for a method or class."
Always try to explain yourself in code.
Donβt be redundant.
Donβt add obvious noise.
Donβt use closing brace comments.
Donβt comment out code. Just remove.
Use as an explanation of intent or clarification of code.
Use as a warning of consequences.
Use as Legal and Informative comments.
Use as TODO comments.
It only takes a few seconds to clear the majority of your thoughts in code. In many cases, itβs all about developing a method that tells the same thing as your statement.
Apparently, many programmers have found it uncommon, if ever, to be a good way to describe code. Itβs clearly wrong. What are you going to want to see? The following:
Or you can avoid comment using a function with descriptive as below:
Another example:
Maybe in the example above you donβt think the point is bad. Itβs not that bad, of course. However, consider the lost chance to obtain a useful method or property:
"Donβt Use a Comment When You Can Use a Function or a Variable."
You know, saying the same thing over and over againβ¦
You can understand what is happening only by reading the file. This means that making comments is pointless after you have explained yourself.
You no longer need a statement when you have a good name for your field or method explaining the nature of the field or method. For example, when called, a method which is called βSendEmailβ needs no further comments. The name of the way an e-mail is sent is obvious.
The developers in many cases do not say what they want to do. You may then end up with a message that a customer is sending an e-mail, then eventually debugging and trying to understand why the e-mail is not sent.
Finally, you understand that the way you are named does not submit an e-mail but just creates the email object.
Normally you end up with rules in big companies and projects which require comments from each method and class. You end up with several observations that donβt really add meaning.
As an example:
Typically, this kind of thing is fine. People internalize the wisdom to take those that come after them into account and to mistake too much information instead of too little information. But it just becomes meaningless noise at some point.
Programmer maintenance requires context β not lessons in programming. You know what foreach) (does and you know the fundamentals of calling a function. Save some time and some noise with the codebase and donβt try to clarify your feedback on language.
Funny comments that developers using for comments:
Often programmers comment on the closing braces. This may be important with long functions and deeply embedded structures, but only for the sort of small and encapsulated features, we select. So if you want your closing braces to be described, try to shorten your functionalities.
βWhat the error is?! βIs it possible to?β Itβs Internet ether that I can see some of your indignations. βThat will genuinely help you to grasp the ends of a wide loop!
Iβll admit that if you have massive loops with multiple levels of nested scope, such a statement can also help. In this case, the issue is not the statement itself, but the maintenance nightmare.
Forget regarding these remarks. This problem coding style is normalized and the team gets stubborn. Rather than commenting on the end of the reach, extract methods until the blocks are so wide that you can no longer see where they were initiated.
Do not leave the code commented out, please! Please! People would be scared to take it away. You use a version control system, I believe. So delete the commented file. As senior developers, you should not approve these kinds of codes of your junior developers.
"Before you commit, remove all βcommented-outβ code"
This function should look like this:
Sometimes you need can feel it is necessary to explain what you tried to do due to some complex algorithm or another reason. However, be sure you are not able to improve the code design.
When expressing intention, a statement is often useful. What we have done in the code is not necessary to comment on, since the reader can display the code itself. What we want to do in the code is more important.
We canβt tell exactly what our goal is in some cases. That is why we must add notes that clarify more and justify why we have not taken a particular action. Perhaps thereβs a flaw in a library we had to stop or we had a peculiar request for the client.
Often we know that certain code lines are very necessary and that the program, for example, could crash without them. In this situation, other developers might be warned of the value of certain code lines.
A good example is a mutex variable used for access to a common resource. Perhaps not all developers will know the meaning of this mutex and it needs notice.
An example worth more than 1000 words. :)
There are situations where the legal justification is that you have to make a comment. The code may be interpreted and modified according to particular license conditions. In this case, you can add a comment specifying this, but not all license terms. From the article, you can point to a particular document or URL connection explaining the conditions of the license. With this detail, you donβt want 200 lines of comments.
Often a comment will add value to a file. For example, when we provide more specifics about what a method returns. Beware that there are cases in which the need for return values comments can be omitted by a good name of a process.
Example:
Iβm never proposing a TODO comment! If you know that you have something to do, just do that. Please build a ticket and make it available in some way if you can not do it right now. You can use task management tools for that.
Otherwise, TODO is likely to be forgotten.
Example:
Often you forget to check the TODOs you added before publishing the file. Therefore, I have needed a long period that emphasizes them and tells me that notes or items have not yet been completed.
This was my article about using comments in your code. Even though there are some forms of good comments, I think that you should try to minimize the usage of comments in general.
Donβt forget that the simplest rules are the best ones!
Nothing like a nice comment can be so helpful. Nothing more than meaningless dogmatic remarks can cover a module. Nothing could be as harmful as a nasty old message, which propagates lies.
There are several extensions for VScode IDE like the below-mentioned ones.