JavaScript-Should a single-line comment be placed at the end or above the code?

Source: Internet
Author: User
If it is necessary to add single-line comment, we develop the general single-line commentPut it at the end of the line of code, or the exclusive line above the code? Or the specific situation, the specific analysis? is a personal habit, or have your reasons. I hope you will discuss, I am good at learning.

    • End of Line

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

Reply content:

If it is necessary to add a single line of comments , do you generally put a single line of comments at the end of the code line, or the exclusive line placed above the code? Or the specific situation, the specific analysis? is a personal habit, or have your reasons. I hope you will discuss, I am good at learning.

    • End of Line

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

In general, personal habits are:
1, variable declaration or initialization, put on the right
2. Short code block declaration on top
3. Comment Too long using multiline comment, and put it above

The point is, do not annotate for comments, variables, functions and other naming norms, the code can clearly understand and try to move in this direction. After all, there is "the beauty of code", and never heard of "the beauty of annotation."

Line comments are usually placed behind the code, the block comments are placed on the top of the code
For details, refer to the link description

If you use Formater and the like, a single-line comment is placed behind it, otherwise, some formatting tools formatted something slightly strange;

Note write it up. Generally formatted annotations, and then you can export the API.

Then you can write the notes you want to read for yourself. No one is going to look at your comments except for you anyway. Notes for a document are generally in the head. It doesn't make sense to write any notes.

Comment Format:
1, single-line (Single-line) Comments://……
2. Block Comment:/*……*/
3. Documentation Notes:/**……*/
4. Javadoc annotation Tag syntax

From the experience of individual projects, the following are used relatively neatly:

1, the variable is generally used single-line annotation, put at the end of the row, if the variable or note is too long, can also put above, if the note is too long, the use of block comments, depending on the situation;

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

2, the calling method is generally annotated with a block, placed above;

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

See someone mention JSDoc, the individual is also used jsdoc , provide a jsdoc Chinese version of the document.
For comments, the individual feels the line comment writes the line bit, and the block comment is written at the beginning.

Personal habits!

I generally code too long, or need to comment too much text, will be placed in the code petition, a single line or a few lines.

A short one, like yours, is placed behind the code.

Even the habitual will enter the empty: // .

Look at the personal habits, put it better behind.

Personally, there are three priorities for annotations: the project requirements specification is the highest level, if the project is not required, then according to the official standards, if there is no official standards, then according to their own standards. For your own standards, refer to the above answer: "Do not comment for comments", the purpose of the note is to make complex code clear and understandable

Look at personal habits or team habits.

1. Short code block declaration on top
2, comment too long on the use of multi-line comments, and placed above
So, personal habits.

I'm used to writing above

It's better to write on it than to leave the previous line of code clear

You can try JSDoc rules.
http://usejsdoc.org/
The JSDoc explicitly stipulates that the comment block comment is placed above, and the single line comment is placed on the right.
In addition to the company's rules.

This specific look at the company code specification, if there is no code specification explicitly limited, write your own comments preferably on the above, or you a moment code above a comment, a code to the right of a comment, you look at the time of the confusion

Personal habits are put on top.
PS: Annotations are used to describe the function blocks of code or modules that are not easily understood. Don't write the notes casually.

As for the single-line comment on the top or written in the following good, it doesn't matter, I generally note this is:

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

The truth is that although it is better to look at the back of the comments, but the use of time is very tangled, especially if you want to add comments after the array elements, after adding a comment, found to add a new element, you need to go back to adding , .

Chestnuts:

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

If it is a function, it will be on the top, comment a line of code alone will be more in the tail.

Comments are only intended to increase the readability of the code and generally the short line of code on the right side of the comment is relatively friendly and long lines of code in the head or downlink comments for easy visibility

Personal habits are above, single-line comment to the right, may cause the need to drag the scroll bar to see the back of the

Try to write on top for easy viewing

Look at the length of the note ...

Different language annotation specifications are not the same, Python use # or "" "" ", it is best to go with the specification

Habit......

  • 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.