From 2ca5a9a7715594abcb02588fd5baf760e98a11b4 Mon Sep 17 00:00:00 2001 From: James Bunt Date: Thu, 24 Apr 2014 11:10:36 +1200 Subject: [PATCH] Some additional notes on naming conventions including recommendations on abbreviations, acronyms, constants and class methods. --- README.md | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3409c68ccd..c9755d5490 100644 --- a/README.md +++ b/README.md @@ -995,7 +995,7 @@ ## Naming Conventions - - Avoid single letter names. Be descriptive with your naming. + - Avoid single letter names. Be descriptive with your naming. Names representing types should be nouns. Names representing methods should be verbs or verb phrases. ```javascript // bad @@ -1009,7 +1009,7 @@ } ``` - - Use camelCase when naming objects, functions, and instances + - Use camelCase when naming objects, functions, and instances. Abbreviations and acronyms should also use camel case when used as a name. ```javascript // bad @@ -1020,12 +1020,20 @@ name: 'Bob Parr' }); + // bad + function parseJSON() {}; + var XMLDocument; + // good var thisIsMyObject = {}; function thisIsMyFunction() {}; var user = new User({ name: 'Bob Parr' }); + + // good + function parseJson() {}; + var xmlDocument; ``` - Use PascalCase when naming constructors or classes @@ -1050,6 +1058,16 @@ }); ``` + - Use Screaming Snake Case for constants + + ```javascript + // bad + var some_constant = 5; + + // good + var SOME_CONSTANT = 5; + ``` + - Use a leading underscore `_` when naming private properties ```javascript @@ -1089,7 +1107,7 @@ } ``` - - Name your functions. This is helpful for stack traces. + - Name your functions. This is helpful for stack traces. Note this is not necessary for class methods. ```javascript // bad @@ -1101,6 +1119,20 @@ var log = function log(msg) { console.log(msg); }; + + // bad + var MyObj = { + log: function log(msg) { + console.log(msg); + } + }; + + // good + var MyObj = { + log: function(msg) { + console.log(msg); + } + }; ``` **[⬆ back to top](#table-of-contents)**