Javascript-should single-line comments be placed at the end of the line or above the code?

Source: Internet
Author: User
If it is necessary to add a single line comment, do developers usually put a single line comment at the end of the code line, or do they exclusively put a single line above the code? Or the specific situation, specific analysis? Is it your personal habit or your reason. I hope you will discuss it. At the end of the line {code...} above {... if necessary add Single line commentGenerally Single line commentPut the line at the end of the code, or put the line above the code exclusively? Or the specific situation, specific analysis? Is it your personal habit or your reason. I hope you will discuss it.

  • End of line

Var var1 = 5; // declare and initialize the variable. The value is 5.
  • Top

// Declare and initialize the variable. The value is 5var var1 = 5;

Reply content:

AddSingle line commentGenerallySingle line commentPut the line at the end of the code, or put the line above the code exclusively? Or the specific situation, specific analysis? Is it your personal habit or your reason. I hope you will discuss it.

  • End of line

Var var1 = 5; // declare and initialize the variable. The value is 5.
  • Top

// Declare and initialize the variable. The value is 5var var1 = 5;

Generally, my personal habits are:
1. declare or initialize the variables, and place them on the right.
2. put the short code block statement above
3. use multi-line comments for too long and place them above

The key point is that you should not comment out variables, functions, and other naming rules for comments. if the code can be clearly understood, try 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 behind the code, and block comments are placed above the code.
For details, refer to the link description.

If formater is used, put a single line comment behind it; otherwise, some formatting tools may be slightly different;

Write the comment above. In general, formatted comments can then be exported to the API.

Then you can write the comments for yourself. No one except you will read your comments. The comments of the document are generally in the header. Comments written at will are useless.

Annotation format:
1. single-line comment://……
2. block annotation:/*……*/
3. document notes:/**……*/
4. javadoc annotation label syntax

From the perspective of personal project experience, the usage is neat as follows:

1. variables are generally annotated with a single row and placed at the end of the row. if the variable or annotation is too long, it can also be placed above it. if the annotation is too long, block annotation should be used, depending on the situation;

SAXReader reader = new SAXReader (); // create SAXReader // Get
  
   
List of all emp labels under the root tag
   
    
Elements = root. elements ();/** Element provides the method to obtain intermediate text information: * String getText () * String getTextTrim () */String name = nameEle. getText ();
   
  

2. call methods generally use block annotations and place them on top;

/* Read the XML file */Document doc = xmlFileReader ();

Someone mentioned jsdoc, and I also use it.jsdocTo provide a Chinese version of jsdoc.
For comments, I personally feel that the row comment writes the row bit and the block comment writes the first row.

Personal habits!

Generally, if the code is too long or the text to be annotated is too large, it will be put on the code for access, one line or several lines separately.

Short, like you, put it behind the code.

It is even customary to enter null://.

Look at your habits. it's better to put it back.

I personally think that there should be three priorities for annotations: project requirements should be the highest level. if there is no requirement for a project, it should follow the official standards. if there is no official standard, it should follow its own standards .. For your own standards, refer to the above answer: "Do not comment for comments". The purpose of comments is to make complex code clear and understandable.

Depending on personal habits or group habits.

1. put the short code block statement above
2. if the comment is too long, use multi-line comment and place it above
Therefore, personal habits

I used to write it above

It is better to write the above code and it can be isolated from the previous line of code to make it clearer.

Try jsDoc rules
Http://usejsdoc.org/
Jsdoc clearly stipulates that the comment block comment should be put on the top, and the single line comment should be put on the right.
In addition to the company's rules, you can follow this philosophy.

This depends on the company's code specifications. if there is no clear limitation on the code specifications, it is best to write comments on your own. Otherwise, you will have a comment on the code later and a comment on the right of the code later, I felt confused when I saw it myself.

Put personal habits on top.
Ps: annotations are used to describe the functional blocks of the code or modules that are not easy to understand. Do not write comments casually

It doesn't matter whether a single-line comment is written above or below. I usually comment like this:

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

Although it looks nice to add comments to the end, it is sometimes difficult to use them. especially if you want to add comments to the end of an array element, after adding comments, when you find that you want to add a new element, you need to add it again.,.

Chestnuts:

$ Test = [a, // a B // B. If you need to add an element to the end, add the number. although you can add the last element, no error is returned, but for obsessive-compulsive disorder, do not like redundant numbers]

If it is a function, it will be on top, and comments a single line of code will be at the end.

Comments are only intended to increase the readability of the code. In general, the comments on the right side of the short-line code are more friendly, and the comments on the header or downstream of the long-line code are easy to see.

Personal habits are above. if you comment on the right of a single line, you may need to drag the scroll bar to see

Try to write it above for easy viewing

The length of the comment...

Different language annotation specifications are different. python can use # or ". it is best to follow the standard.

Habits ......

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.