forked from geddy/geddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_style_guide.html
339 lines (334 loc) · 10.3 KB
/
js_style_guide.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<html>
<head>
<style type="text/css">
body {
font-family: "lucida grande", verdana, sans-serif;
padding: 1em;
}
td {
padding: 0.2em;
}
code {
font-size: 1.2em;
}
</style>
</head>
<body>
<h1>
JS Styleguide
</h1>
<p>
These are merely guidelines. They should not be adhered to
mechanically, especially if a deviation would make your code
more readable.
</p>
<h2>
General
</h2>
<ol>
<li>
Code SHOULD be indented with spaces only.
</li>
<li>
Each level of indentation SHOULD consist of 2 spaces.
</li>
<li>
Lines SHOULD be 80 characters max.
</li>
</ol>
<h2><a name="Naming"></a>Naming</h2>
<h3><a name="Overview"></a>Overview</h3>
<table>
<tr>
<th>
Construct
</th>
<th>
Convention
</th>
<th>
Examples
</th>
</tr>
<tr>
<td>Pseudoclass name (i.e., constructor function)</td>
<td>CamelCase with an initial capital letter</td>
<td><code>MightyBalrog, MagicWeapon</code>
</td>
</tr>
<tr>
<td>Namespace name</td>
<td>camelCase with an initial lowercase letter</td>
<td><code>clericSpells, savingThrows</code>
</td>
</tr>
<tr>
<td>Public method</td>
<td>camelCase with an initial lowercase letter</td>
<td><code>castDimensionalDoorway, rollForInitiative</code>
</td>
</tr>
<tr>
<td>Public variable</td>
<td>camelCase with an initial lowercase letter</td>
<td><code>materialComponents, hasTrackingAbilities</code></td>
</tr>
<tr>
<td>Private method</td>
<td>camelCase with an initial lowercase letter and underscore</td>
<td><code>_getHealth</code></td>
</tr>
<tr>
<td>Private variable</td>
<td>camelCase with an initial lowercase letter and underscore</td>
<td><code>_backstabAbility</code></td>
</tr>
<tr>
<td>Method arguments</td>
<td>camelCase with an initial lowercase letter</td>
<td><code>halfOrcArmy</code></td>
</tr>
<tr>
<td>Local variables</td>
<td>camelCase with an initial lowercase letter</td>
<td><code>isHumanoid, levelCount</code></td>
</tr>
<tr>
<td>'Constants'</td>
<td>Uppercase with underscores</td>
<td><code>CLERIC_PLAYER, GAME_MASTER</code></td>
</tr>
<tr>
<td>Ennumeration keys</td>
<td>Uppercase with underscores</td>
<td><code>characterClass.MAGIC_USER, armorTypes.PLATE_MAIL</code></td>
</tr>
</table>
<h3>
Notes
</h3>
<ol>
<li>Variable/method names in all lowercase with underscores SHOULD NOT be used unless mimicking another API.
<ul>
<li>Incorrect:<pre><code>wizard_hat, vorpal_blade</code></pre></li>
<li>Correct:<pre><code>wizardHat, vorpalBlade</code></pre></li>
</ul>
</li>
<li>Acronyms in variable/method names SHOULD NOT be upppercased.
<ul>
<li>Incorrect:<pre><code>bartenderNPC, newRPG</code></pre></li>
<li>Correct:<pre><code>bartenderNpc, newRpg</code></pre></li>
</ul>
</li>
<li>Variable/method names SHOULD be written in English.
<ul>
<li>Incorrect:<pre><code>dekaiKatana</code></pre></li>
<li>Correct:<pre><code>giganticSword</code></pre></li>
</ul>
</li>
<li>Variable/method names SHOULD NOT be abbreviated to the point of being unclear.
<ul>
<li>Incorrect:<pre><code>wndMnstr[3]</code></pre></li>
<li>Correct:<pre><code>wanderingMonster[3]</code></pre></li>
</ul>
</li>
</ol>
<h2>
Variables
</h2>
<ol>
<li>Variables SHOULD be initialized where they are declared, if possible in a way that indicates what type of value they will hold. Null initializations are acceptable.
<ul>
<li>Incorrect:<pre><code>var magicItemCount;
var wizardNpc;</code></pre></li>
<li>Correct:<pre><code>var magicItemCount = 0;
var wizardNpc = null;</code></pre></li>
</ul>
</li>
<li>Variable declarations SHOULD NOT include extra spaces before the equals sign to align the variable values.
<ul>
<li>Incorrect:<pre><code>var currentThiefLevel = 8;
var canBackstab = true;
var isNpc = true;</code></pre></li>
<li>Correct:<pre><code>var currentThiefLevel = 8;
var canBackstab = true;
var isNpc = true;</code></pre></li>
</ul>
</li>
<li>Variable names SHOULD NOT include 'temp' or 'tmp'. -- all local variables are by definition temporary.
<ul>
<li>Incorrect:<pre><code>tempString, tmpDate</code></pre></li>
<li>Correct:<pre><code>str, dt</code></pre></li>
</ul>
</li>
<li>Magic numbers SHOULD NOT be used. Use a constant instead.
<ul>
<li>Incorrect:<pre><code>42</code></pre></li>
<li>Correct:<pre><code>ANSWER_TO_THE_QUESTION_OF_LIFE</code></pre></li>
</ul>
</li>
</ol>
<h2>
Coding Style
</h2>
<h3>
Overview
</h3>
<ol>
<li> Function declaration:<pre><code>function checkForTraps(dexterity, level) {
// Do stuff to check for traps here
}
var checkForSecretDoors = function (race, level) {
// Stuff for check here
};</code></pre>
</li>
<li>If statements:<pre><code>if (gotInitiative) {
attackDragon();
}
else if (speaksDragon) {
tryNegotiating();
}
else {
runAway();
}</code></pre>
</li>
<li>For statements:<pre><code>for (var i = 0; i < guards.length; i++) {
rollTwentySided(guards[i]);
}</code></pre>
</li>
<li>While statements:<pre><code>while (charactersInjured) {
castCureLightWounds();
charactersInjured = checkCharacterHealth();
}</code></pre>
</li>
<li>Switch statements:<pre><code>switch (characterClass) {
case 'ranger':
// Ranger special stuff here
// Fallthrough
case 'fighter':
// Do fighter stuff
break;
case 'magicUser':
// Do mage-specific stuff
break;
default:
// do nothing
}</code></pre>
</li>
<li>Try-catch-finally statements:<pre><code>try {
pickPocket();
}
catch (e) {
lookInconspicuous();
reportBack(e);
}
finally {
runLikeHell();
}</code></pre>
</li>
<li>Object literal:<pre><code>var obj = {
spellName: 'Invisible Stalker',
numberOfFighters: 3,
checkForTraps = function() {
// Do trap checking
}
};
var obj = { staff: 'Staff of the Magi', wand:
'Wand of Negation', misc: 'Boots of Elvenkind' };
</code></pre></li>
</ol>
<h3>
Notes
</h3>
<ol>
<li>
Function literals MUST include a space between the word 'function' and the parentheses. (Otherwise it appears to be a function with the name of 'function.')
<ul>
<li>Incorrect:<pre><code>var rollInitiative = function() { // Roll die here };</code></pre></li>
<li>Incorrect:<pre><code>var rollInitiative = function () { // Roll die here };</code></pre></li>
</ul>
</li>
<li>
Line continuations should be indicated by double indentation.
<ul>
<li>Incorrect:<pre><code>var localMonsterRumors = getLocalGossip(inkeeper,
localInn,
numberOfClerics,
pintsOfAlePurchased,
charismaAjustment);</code></pre></li>
<li>Correct:<pre><code>var localMonsterRumors = getLocalGossip(inkeeper,
localInn, numberOfClerics, pintsOfAlePurchased,
charismaAjustment);</code></pre></li>
</ul>
</li>
<li>If-else statements (also while, et al) MAY be written on a single line, but MUST use brackets.
<ul>
<li>Incorrect:<pre><code>if (isUndead) grabFire();</code></pre></li>
<li>Correct:<pre><code>if (isUndead) { grabFire(); }</code></pre></li>
</ul>
</li>
<li>Parenthesis in conditional statements (if, while, for, etc.) SHOULD have a space before them.
<ul>
<li>Incorrect:<pre><code>if(isNpc) {
ignoreTalk();
}</code></pre></li>
<li>Correct:<pre><code>if (isNpc) {
ignoreTalk();
}</code></pre></li>
</ul>
</li>
<li>Parenthesis in function declarations SHOULD NOT have a space before them.
<ul>
<li>Incorrect:<pre><code>function getArmorClass (armorType, dexterity) {
// Get AC stuff here
}</code></pre></li>
<li>Correct:<pre><code>function getArmorClass(armorType, dexterity) {
// Get AC stuff here
}</code></pre></li>
</ul>
</li>
<li>Commas SHOULD be followed by spaces.
<ul>
<li>Incorrect:<pre><code>getExperiencePoints(monster,hitPoints);</code></pre></li>
<li>Correct:<pre><code>getExperiencePoints(monster, hitPoints);</code></pre></li>
</ul>
</li>
<li>The colon in object literal notation SHOULD have no space in front of it, and be followed by a single space.
<ul>
<li>Incorrect:<pre><code>var newCharacter = {
race:'gnome',
class:'figheter'
isNpc:false;
};</code></pre></li>
<li>Also incorrect:<pre><code>var newCharacter = {
race : 'gnome',
class : 'figheter'
isNpc : false;
};</code></pre></li>
<li>Correct:<pre><code>var newCharacter = {
race: 'gnome',
class: 'figheter'
isNpc: false;
};</code></pre></li>
</ul>
</li>
<li>Operators SHOULD both have a space before and after.
<ul>
<li>Incorrect:<pre><code>var message = speaksDrow? getMessageinDrow():'You do not speak Drow.';</code></pre></li>
<li>Correct:<pre><code>var message = speaksDrow ? getMessageinDrow() : 'You do not speak Drow.';</code></pre></li>
<li>Incorrect:<pre><code>var thaco = hit+adjustment-randomFactor;</code></pre></li>
<li>Correct:<pre><code>var thaco = hit + adjustment - randomFactor;</code></pre></li>
</ul>
</li>
<li>Lengthy DOM element IDs or other string parameters SHOULD be placed into variables before using.
<ul>
<li>Incorrect:<pre><code>var elem = document.getElementById('charClass-' + charClass +
+ '_combatStats-' + armorClass + '-' + toHitBonus);</code></pre></li>
<li>Correct:<pre><code>var char = 'charClass-' + charClass;
var combat = 'combatStatus-' + armorClass + '-' + toHitBonus;
var elem = document.getElementById(char + '_' + combat);</code></pre></li>
</ul>
</li>
</ol>
</body>
</html>