Comments

  • 18.1 Use /** ... */ for multi-line comments.

    1. // bad
    2. // make() returns a new element
    3. // based on the passed in tag name
    4. //
    5. // @param {String} tag
    6. // @return {Element} element
    7. function make(tag) {
    8. // ...
    9. return element;
    10. }
    11. // good
    12. /**
    13. * make() returns a new element
    14. * based on the passed-in tag name
    15. */
    16. function make(tag) {
    17. // ...
    18. return element;
    19. }

  • 18.2 Use // for single line comments. Place single line comments on a newline above the subject of the comment. Put an empty line before the comment unless it’s on the first line of a block.

    1. // bad
    2. const active = true; // is current tab
    3. // good
    4. // is current tab
    5. const active = true;
    6. // bad
    7. function getType() {
    8. console.log('fetching type...');
    9. // set the default type to 'no type'
    10. const type = this.type || 'no type';
    11. return type;
    12. }
    13. // good
    14. function getType() {
    15. console.log('fetching type...');
    16. // set the default type to 'no type'
    17. const type = this.type || 'no type';
    18. return type;
    19. }
    20. // also good
    21. function getType() {
    22. // set the default type to 'no type'
    23. const type = this.type || 'no type';
    24. return type;
    25. }

  • 18.3 Start all comments with a space to make it easier to read. eslint: spaced-comment

    1. // bad
    2. //is current tab
    3. const active = true;
    4. // good
    5. // is current tab
    6. const active = true;
    7. // bad
    8. /**
    9. *make() returns a new element
    10. *based on the passed-in tag name
    11. */
    12. function make(tag) {
    13. // ...
    14. return element;
    15. }
    16. // good
    17. /**
    18. * make() returns a new element
    19. * based on the passed-in tag name
    20. */
    21. function make(tag) {
    22. // ...
    23. return element;
    24. }

  • 18.4 Prefixing your comments with FIXME or TODO helps other developers quickly understand if you’re pointing out a problem that needs to be revisited, or if you’re suggesting a solution to the problem that needs to be implemented. These are different than regular comments because they are actionable. The actions are FIXME: -- need to figure this out or TODO: -- need to implement.

  • 18.5 Use // FIXME: to annotate problems.

    1. class Calculator extends Abacus {
    2. constructor() {
    3. super();
    4. // FIXME: shouldn’t use a global here
    5. total = 0;
    6. }
    7. }

  • 18.6 Use // TODO: to annotate solutions to problems.

    1. class Calculator extends Abacus {
    2. constructor() {
    3. super();
    4. // TODO: total should be configurable by an options param
    5. this.total = 0;
    6. }
    7. }