Programmers should avoid writing comments, while programmers should avoid writing comments.

Source: Internet
Author: User

Programmers should avoid writing comments, while programmers should avoid writing comments.

"How efficient a programmer is depends on how familiar he is with the current project, including the variable name, data structure, programming interfaces, tool classes, and even directories. The more details he remembers, the higher the efficiency."

Comments are not used to translate program code. If you use the code to make it clear, you should not use the natural language to write it. It is the first priority to concentrate on writing the most elegant and high-quality code. This is not to say that you can not write comments at all, but not to addUnnecessary commentsAnd disrupt your thinking. I agree with the two articles, "the worst two variable names in the world" and "Avoid writing comments in code" Translated by Lu Qiming. there is a good variable name and method name, which is actually a good comment, and will also be "automatically updated". You do not need to update comments synchronously when optimizing this code in the future.

The purpose of writing comments is not to explain how the code is executed, but to explain why the code is executed. This is precisely the person who studies the code and comments, here is an example from the original post of Atwood's blog:

r = n / 2;while ( abs( r - (n/r) ) > t ) {r = 0.5 * ( r + (n/r) );}System.out.println( "r = " + r );

This code is completely incomprehensible without comments. If you add some comments, it will be much better:

// Solve the square root of n using the Newton-laferson approximation r = n/2; while (abs (r-(n/r)> t) {r = 0.5 * (r + (n/r);} System. out. println ("r =" + r );

In this way, the function of the comment is fully reflected, but what if the comment is not needed? Like this:

private double SquareRootApproximation(n) {r = n / 2;while ( abs( r - (n/r) ) > t ) {r = 0.5 * ( r + (n/r) );}return r;}System.out.println( "r = " + SquareRootApproximation(r) );
"I didn't add a line of comments, but this piece of mysterious code is now very easy to understand ."

Each of us should have encountered such a thing in the project. Instead of asking him to write a bunch of comments, we should tell him the importance of naming and completely and accurately describe the represented thing, this will undoubtedly improve the readability of the entire project. There are also some traps in naming. If a name cannot be accurate, it is even more confusing to describe the correct behavior with a wrong comment, in addition, if you do not think too much about naming a class, you may find it difficult to change the name later. You may need to change the header file name and implement the file name, various calls and import locations, or even a note automatically generated when a file is created, this requires that when we create a class and method, the mind should at least "optimize" the things to be done several times to accurately express what you want to express. For some people, this process will be very tiring, but it is indeed necessary.

The question in this article "never use bool as a function parameter" is very interesting. For Boolean variables, you can give them a name that implies the meaning of "true and false.



Can I write a program in C language without comments? Solution

The purpose of annotations is to make your code easier to maintain and modify. for computers, annotations are meaningless.
Therefore, good code is not a code that computers can understand, but a code that people can understand.
I personally think that not comments are not necessary. We should try to avoid unnecessary comments by improving the code quality.
For example, if you use a variable name with a clear meaning, the function name must be consistent with the function, and the function must be single-functional.
However, for the use of some special algorithms or special values, it is necessary to use necessary annotations to explain the reasons.

How can programmers not forget code comments when writing code? Is there any good tool?

No tool, but this is a matter of personal habits! However, it is not entirely a personal habit, and it also has something to do with the environment where it is located. I used to write code and do not like commenting, but the company strongly requires commenting, in addition, my friends have the habit of commenting,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.