What makes a good code comment?

What makes a good code comment?

Comments should be useful high level descriptions of what the program is doing. They should not restate something that is “obvious”. By using appropriate variable names, much of a program can be (almost) as easy to read as English.

Why is it important to add comments to code?

In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.

Can you comment code too much?

Over-commented code is often more difficult to understand than code without comments. Little notes back and forth from all the different maintainers of a project can often get cluttered. You spend more time reading the comments than you do the actual code.

Is it good to comment every line of code?

Comments should be very rare and valuable, almost always expressing the “why” and never the “how” (the exception being when the how is complex and not easily discernible from the code). Every comment is a hint that you may need to refactor to make the code’s intent clearer.

How does the single line comment look like?

Single-line comments start with two forward slashes ( // ). Any text between // and the end of the line is ignored by Java (will not be executed).

Which is better too many comments or too few comments?

Too many comments may be bad, if the comments do not add information about how the code works. Extra comments do not hurt anything. But too few comments is much worse, because even the author of some code cannot really understand it completely after a few months.

How long should code comments?

Usually for every 10 lines of code I put in a minimum of 5 lines of comments. Comments do not include pseudo code, work of art or commented code(code no longer in use). The guidance usually is 75% of code should be comments but sometimes I think it is a bit much. However, more the meaningful comments the better.

What is the purpose of comments which is better too many comments or too few comments?

Extra comments do not hurt anything. But too few comments is much worse, because even the author of some code cannot really understand it completely after a few months. Comments should not explain each line of obvious code, but explain the high level details of why something is being done.

How many comments should I put in my code?

You should comment exactly as much as you love your work! If you love your code a lot; go ahead and write your heart out all over the code. If you do not love your code at all, please do not write any comments, no one needs any more from you!

How do I comment every line?

On Windows 10 you can use CTRL + } to comment/uncomment on any line or block of code. Option 2. While the text is selected hit Ctrl / Cmd + K and then Ctrl / Cmd + C .

When do you not need to put a comment in code?

Most coders would chuckle and move on, ignoring the code smell. There are also times when you come across a comment that is redundant. If the code is already simple and obvious, there’s no need to add a comment. Like, don’t do this nonsense:

How to make good code reviews better Stack Overflow?

Better code reviews realize that too many nitpicks are a sign of lack of tooling or a lack of standards. Reviewers who come across these frequently will look at solving this problem outside the code review process. For example, many of the common nitpick comments can be solved via automated linting.

Why do you put clarification comments in your code?

Clarification comments are intended for anyone (including your future self) who may need to maintain, refactor, or extend your code. Often, a clarification comment is a code smell. It tells you that your code is too complex. You should strive to remove clarification comments and simplify the code instead because, “good code is self-documenting.”

When to write funny comments to make up for bad code?

Don’t get me wrong, there are times — especially when you are slogging through a crushing workload — where injecting a bit of humor can be good for the soul. But when you write a funny comment to make up for bad code, it actually makes people less likely to refactor and fix the code later.