Home  >  Article  >  Backend Development  >  javascript - Should single-line comments be placed at the end of the line or above the code?

javascript - Should single-line comments be placed at the end of the line or above the code?

WBOY
WBOYOriginal
2016-12-01 00:25:162076browse

If it is necessary to add a single-line comment, do developers generally put the single-line comment at the end of the code line, or should it be placed on its own line above the code? Or is it a specific situation and detailed analysis? Is it a personal habit or does it have your reasons. I hope everyone can discuss it so that I can learn more.

  • End of line

<code>var var1 = 5; // 声明并初始化变量,值为5</code>
  • Above

<code>// 声明并初始化变量,值为5
var var1 = 5; </code>

Reply content:

If it is necessary to add a single-line comment, do developers generally put the single-line comment at the end of the code line, or should it be placed on its own line above the code? Or is it a specific situation and detailed analysis? Is it a personal habit or does it have your reasons. I hope everyone can discuss it so that I can learn more.

  • End of line

<code>var var1 = 5; // 声明并初始化变量,值为5</code>
  • Above

<code>// 声明并初始化变量,值为5
var var1 = 5; </code>

Generally, my personal habits are:
1. Place variable declarations or initializations on the right side
2. Place short code block declarations at the top
3. If comments are too long, use multi-line comments and place them at the top

The key point is, don’t comment for the sake of commenting. The naming of variables, functions, etc. should be standardized. If the code can be clearly understood, try your best to work in this direction. After all, there is the "beauty of code", but I have never heard of the "beauty of comments".

Line comments are generally placed after the code, and block comments are placed above the code.
For details, please refer to the link description

If you use formatter or the like, put a single line comment at the end; otherwise, some formatting tools will format things a little weird;

Please write comments above. Generally formatted comments can then be exported to the API.

Then there are notes for you to read, so just write whatever you want. No one except you will read your comments anyway. Document comments are generally at the head. Randomly written comments feel useless

Comment format:
1. Single-line comments: //……
2. Block comments: /*&*/
3. Document comments: /** &*/
4. javadoc comment tag syntax


From personal project experience, it is neat to use it as follows:

1. Variables are generally commented with a single line and placed at the end of the line. If the variable or annotation is too long, it can also be placed at the top. If the annotation is too long, use a block comment instead, depending on the situation;

<code>SAXReader reader = new SAXReader();//创建SAXReader
//获取<List>根标签下的所有emp标签
List<Element>elements = root.elements();
/*
 * Element提供了获取中间文本信息的方法:
 * String getText()
 * String getTextTrim()
 */
String name = nameEle.getText();
</code>

2. Calling methods are generally annotated with blocks and placed at the top;

<code>/* 读取XML文件 */
Document doc = xmlFileReader();
</code>

I saw someone mentioning jsdoc. I personally use jsdoc and provide a jsdoc Chinese version document.
Regarding comments, I personally feel that line comments should be written at the line position, and block comments should be written at the beginning of the line.

It’s a personal habit!

I usually put the code in a separate line or a few lines if the code is too long or there are too many words that need to be commented.

Short and similar to yours, just put it behind the code.

Even habitually enter empty://.

Depending on personal habits, it’s better to put it later

Personally, I think there should be three priorities regarding comments: The project requirement specification is the highest level. If the project does not have requirements, then follow the official standards. If there is no official standard, then follow your own standards. . Regarding your own standards, I quote an answer above: "Don't comment for the sake of comments." The purpose of comments is to make complex code clear and easy to understand

Look at personal habits or team habits.

1、简短代码块声明放在上方
2、注释过长就使用多行注释,并放在上方
所以说,个人习惯

我习惯写上面

写上面比较好 可以与上一行代码隔离开 能够清晰一些

可以试试jsDoc规则
http://usejsdoc.org/
jsdoc明确规定了,注释块注释放上面,单行注释放右侧。
除了公司规定就照这个经念就可以

这个具体看公司代码规范了,如果没有代码规范明确限制,自己写注释最好放在上面,不然你一会儿代码上面一个注释,一会代码右边一个注释,自己看的时候就觉得乱了

个人习惯放在上面。
ps:注释用来说明代码的功能块或者不太容易理解的模块。不要随便写注释

至于单行注释写在上面好还是写在下面好,都没关系,我一般注释都是这样:

<code>// XXX功能 start
...
do {
    xxxxx...
} while(x...);
...
// XXX功能 end</code>

说实在虽然放后面比较好看注释,但用起来有时候很纠结,特别是如果想在数组元素后面加注释的时候,在加了注释之后,发现要加一个新元素的时候,需要再去添加,

栗子:

<code>$test = [
    a, //a
    b  //b,如果需要在后面添加一个元素,需要补上,号,虽然可以在最后元素加上,不报错,但是对于强迫症来讲,不喜欢多余的,号
]</code>

如果是一个功能的话会在上面,单独注释一行代码的话会在尾部更随。

注释只是为了增加代码的可阅读性 并且 一般来说 短行代码 在右侧注释比较友好 而长行代码 在头部或下行注释 便于看清不乱

个人习惯 都在上面 ,单行注释右边的话,可能会造成需要拖动滚动条才能看到后面的

尽量写在上方,方便查看

看注释长度。。。

不同语言注释规范也不一样,python就用#或者"""""",最好跟规范走

习惯......

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn