Naming Conventions

  • 23.1 Avoid single letter names. Be descriptive with your naming. eslint: id-length

    1. // bad
    2. function q() {
    3. // ...
    4. }
    5. // good
    6. function query() {
    7. // ...
    8. }

  • 23.2 Use camelCase when naming objects, functions, and instances. eslint: camelcase

    1. // bad
    2. const OBJEcttsssss = {};
    3. const this_is_my_object = {};
    4. function c() {}
    5. // good
    6. const thisIsMyObject = {};
    7. function thisIsMyFunction() {}

  • 23.3 Use PascalCase only when naming constructors or classes. eslint: new-cap

    1. // bad
    2. function user(options) {
    3. this.name = options.name;
    4. }
    5. const bad = new user({
    6. name: 'nope',
    7. });
    8. // good
    9. class User {
    10. constructor(options) {
    11. this.name = options.name;
    12. }
    13. }
    14. const good = new User({
    15. name: 'yup',
    16. });

  • 23.4 Do not use trailing or leading underscores. eslint: no-underscore-dangle

    Why? JavaScript does not have the concept of privacy in terms of properties or methods. Although a leading underscore is a common convention to mean “private”, in fact, these properties are fully public, and as such, are part of your public API contract. This convention might lead developers to wrongly think that a change won’t count as breaking, or that tests aren’t needed. tl;dr: if you want something to be “private”, it must not be observably present.

    1. // bad
    2. this.__firstName__ = 'Panda';
    3. this.firstName_ = 'Panda';
    4. this._firstName = 'Panda';
    5. // good
    6. this.firstName = 'Panda';
    7. // good, in environments where WeakMaps are available
    8. // see https://kangax.github.io/compat-table/es6/#test-WeakMap
    9. const firstNames = new WeakMap();
    10. firstNames.set(this, 'Panda');

  • 23.5 Don’t save references to this. Use arrow functions or Function#bind.

    1. // bad
    2. function foo() {
    3. const self = this;
    4. return function () {
    5. console.log(self);
    6. };
    7. }
    8. // bad
    9. function foo() {
    10. const that = this;
    11. return function () {
    12. console.log(that);
    13. };
    14. }
    15. // good
    16. function foo() {
    17. return () => {
    18. console.log(this);
    19. };
    20. }

  • 23.6 A base filename should exactly match the name of its default export.

    1. // file 1 contents
    2. class CheckBox {
    3. // ...
    4. }
    5. export default CheckBox;
    6. // file 2 contents
    7. export default function fortyTwo() { return 42; }
    8. // file 3 contents
    9. export default function insideDirectory() {}
    10. // in some other file
    11. // bad
    12. import CheckBox from './checkBox'; // PascalCase import/export, camelCase filename
    13. import FortyTwo from './FortyTwo'; // PascalCase import/filename, camelCase export
    14. import InsideDirectory from './InsideDirectory'; // PascalCase import/filename, camelCase export
    15. // bad
    16. import CheckBox from './check_box'; // PascalCase import/export, snake_case filename
    17. import forty_two from './forty_two'; // snake_case import/filename, camelCase export
    18. import inside_directory from './inside_directory'; // snake_case import, camelCase export
    19. import index from './inside_directory/index'; // requiring the index file explicitly
    20. import insideDirectory from './insideDirectory/index'; // requiring the index file explicitly
    21. // good
    22. import CheckBox from './CheckBox'; // PascalCase export/import/filename
    23. import fortyTwo from './fortyTwo'; // camelCase export/import/filename
    24. import insideDirectory from './insideDirectory'; // camelCase export/import/directory name/implicit "index"
    25. // ^ supports both insideDirectory.js and insideDirectory/index.js

  • 23.7 Use camelCase when you export-default a function. Your filename should be identical to your function’s name.

    1. function makeStyleGuide() {
    2. // ...
    3. }
    4. export default makeStyleGuide;

  • 23.8 Use PascalCase when you export a constructor / class / singleton / function library / bare object.

    1. const AirbnbStyleGuide = {
    2. es6: {
    3. },
    4. };
    5. export default AirbnbStyleGuide;

  • 23.9 Acronyms and initialisms should always be all capitalized, or all lowercased.

    Why? Names are for readability, not to appease a computer algorithm.

    1. // bad
    2. import SmsContainer from './containers/SmsContainer';
    3. // bad
    4. const HttpRequests = [
    5. // ...
    6. ];
    7. // good
    8. import SMSContainer from './containers/SMSContainer';
    9. // good
    10. const HTTPRequests = [
    11. // ...
    12. ];
    13. // also good
    14. const httpRequests = [
    15. // ...
    16. ];
    17. // best
    18. import TextMessageContainer from './containers/TextMessageContainer';
    19. // best
    20. const requests = [
    21. // ...
    22. ];

  • 23.10 You may optionally uppercase a constant only if it (1) is exported, (2) is a const (it can not be reassigned), and (3) the programmer can trust it (and its nested properties) to never change.

    Why? This is an additional tool to assist in situations where the programmer would be unsure if a variable might ever change. UPPERCASE_VARIABLES are letting the programmer know that they can trust the variable (and its properties) not to change.

    • What about all const variables? - This is unnecessary, so uppercasing should not be used for constants within a file. It should be used for exported constants however.
    • What about exported objects? - Uppercase at the top level of export (e.g. EXPORTED_OBJECT.key) and maintain that all nested properties do not change.
    1. // bad
    2. const PRIVATE_VARIABLE = 'should not be unnecessarily uppercased within a file';
    3. // bad
    4. export const THING_TO_BE_CHANGED = 'should obviously not be uppercased';
    5. // bad
    6. export let REASSIGNABLE_VARIABLE = 'do not use let with uppercase variables';
    7. // ---
    8. // allowed but does not supply semantic value
    9. export const apiKey = 'SOMEKEY';
    10. // better in most cases
    11. export const API_KEY = 'SOMEKEY';
    12. // ---
    13. // bad - unnecessarily uppercases key while adding no semantic value
    14. export const MAPPING = {
    15. KEY: 'value'
    16. };
    17. // good
    18. export const MAPPING = {
    19. key: 'value'
    20. };