Image for post

Adding comments in the source code is purely a personal choice. Some engineers believe that comments in the code help them and others to understand their code with ease. Some of them believe that the code should be written in such a way that it is self-explanatory. In either case, we want our code to be readable and easily understandable.

If you choose to add comments in your code, you might as well do it in such a way that it becomes a blessing and not a nightmare.

Below are some practices which will help you to write better comments in your code.

  1. Use // for single-line comments.
// bad
/* Base Url */
const baseUrl = 'http://127.0.0.1';

// good
// Base Url
const baseUrl = 'http://127.0.0.1';

2. Comments should be placed in a new line rather than appending it after the code statement.

Single Space, given at the start of the comments, also improves its readability.

// bad
const baseUrl = 'http://127.0.0.1'; //Webapp Base Url

// good
// Webapp Base Url
const baseUrl = 'http://127.0.0.1';

3. Try to use short and precise comments in your code.

// bad
// Base url to be used by the API
const baseUrl = 'http://127.0.0.1';

// good
// API Base Url
const baseUrl = 'http://127.0.0.1';

#programming #software-engineering #tips #software-development #javascript

Best Easy JavaScript Practices— Writing Comments
1.05 GEEK