├── JavaScript.md └── README.md /JavaScript.md: -------------------------------------------------------------------------------- 1 | # JavaScript 2 | 3 | ## Identifiers 4 | 5 | - **RULE**: Identifiers bound to constructors **must** start with a capital. 6 | 7 | ```javascript 8 | // GOOD 9 | 10 | var User = function (name, age) { 11 | this.name = name; 12 | this.age = age; 13 | }; 14 | var user = new User('bob', 32); 15 | 16 | ``` 17 | 18 | 19 | ```javascript 20 | // BAD 21 | 22 | var user = function (name, age) { 23 | this.name = name; 24 | this.age = age; 25 | }; 26 | var u = new user('bob', 32); 27 | 28 | ``` 29 | 30 | - **RULE**: Identifiers bound to a variable or function **must** start with a *minuscule*. 31 | - **RULE**: Identifiers bound to a variable or function **must** be CamelCased or lowercase, 32 | depending on their length. 33 | 34 | ```javascript 35 | // GOOD 36 | 37 | var someKindOfProcess = function () {}; 38 | var tmpvar = 42; 39 | 40 | ``` 41 | 42 | 43 | ```javascript 44 | // BAD 45 | 46 | var some_kind_of_process = function () {}; 47 | var somekindofprocess = function () {}; 48 | 49 | ``` 50 | 51 | ## Variable declarations 52 | 53 | - **RULE**: Variables **must** always be declared, prior to use. 54 | - **RULE**: Variable declarations **should** appear at the top of functions, 55 | and not inside other blocks. The exception is *for* loops. 56 | 57 | > Variable declarations are moved up to the top of the function scope anyway, 58 | > so that's where they belong. 59 | 60 | 61 | ```javascript 62 | // GOOD 63 | 64 | function (a, b) { 65 | var k; 66 | 67 | if (a == b) { 68 | k = true; 69 | } 70 | ... 71 | } 72 | 73 | for (var i = 0; i < l; i ++) { ... } 74 | 75 | ``` 76 | 77 | 78 | ```javascript 79 | // BAD 80 | 81 | function (a, b) { 82 | if (a == b) { 83 | var k = true; 84 | } 85 | ... 86 | } 87 | 88 | ``` 89 | 90 | ## Control-flow 91 | 92 | - **RULE**: Control-flow statements, such as `if`, `while` and `for` **must** have a space between the keyword and the left parenthesis. 93 | 94 | > They aren't functions, and thus better distinguished like this. 95 | 96 | ```javascript 97 | // GOOD 98 | 99 | if (a) { 100 | return true; 101 | } 102 | 103 | ``` 104 | 105 | ```javascript 106 | // BAD 107 | 108 | if(a) { 109 | return true; 110 | } 111 | 112 | ``` 113 | 114 | ## Functions 115 | 116 | - **RULE**: Anonymous functions **must** have a space between the `function` keyword and the left parenthesis. 117 | 118 | > To emphasise the lack of identifier and differentiate them with named functions. 119 | 120 | 121 | ```javascript 122 | // GOOD 123 | 124 | function (a, b) {} 125 | 126 | ``` 127 | 128 | 129 | ```javascript 130 | // BAD 131 | 132 | function(a, b) {} 133 | 134 | ``` 135 | 136 | - **RULE**: Named functions **must not** have a space between the function name and the left parenthesis. 137 | - **RULE**: Function calls **should not** have a space between the function name and the left parenthesis. 138 | 139 | 140 | ```javascript 141 | // GOOD 142 | 143 | function add(a, b) {} 144 | 145 | ``` 146 | 147 | 148 | ```javascript 149 | // BAD 150 | 151 | function add (a, b) {} 152 | 153 | ``` 154 | 155 | ## Semicolons 156 | 157 | - **RULE**: Semicolons `;` **must** be added at the end of every statement, **except** when the next character is a closing bracket `}`. 158 | In that case, they may be omitted. 159 | 160 | 161 | ```javascript 162 | // GOOD 163 | 164 | var f = function add(a, b) { 165 | if (a == b) { return a * 2 } // No `;` here. 166 | return a + b; 167 | }; 168 | 169 | ``` 170 | 171 | 172 | ```javascript 173 | // BAD 174 | 175 | var f = function add (a, b) { 176 | return a + b 177 | } 178 | 179 | ``` 180 | 181 | ## Braces 182 | 183 | - **RULE**: Braces **should** be used in all circumstances. They **may** be omitted 184 | around simple statements. 185 | 186 | 187 | ```javascript 188 | // GOOD 189 | 190 | if (x) { return true } 191 | 192 | ``` 193 | 194 | ```javascript 195 | // BAD 196 | 197 | if (x) 198 | while (1) 199 | i ++; 200 | else 201 | ... 202 | ``` 203 | 204 | 205 | ```javascript 206 | // OK 207 | 208 | if (x) return true; 209 | 210 | ``` 211 | 212 | ```javascript 213 | // OK 214 | 215 | if (x) 216 | return true; 217 | 218 | ``` 219 | - **RULE**: Opening Braces **must never** be on a line of their own. 220 | 221 | > Vertical screen space is precious. 222 | 223 | ```javascript 224 | // GOOD 225 | 226 | if (x) { 227 | return true; 228 | } 229 | 230 | ``` 231 | 232 | 233 | ```javascript 234 | // BAD 235 | 236 | if (x) 237 | { 238 | return true; 239 | } 240 | 241 | ``` 242 | 243 | ```javascript 244 | // BAD 245 | 246 | if (x) { 247 | return true; } 248 | 249 | ``` 250 | 251 | 252 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | STYLEGUIDE 2 | ========== 3 | 4 | Click on the individual files to see their style-guide. 5 | --------------------------------------------------------------------------------