Product
|
Planning
|
Teams
|
Developers
|
Notes
E
dit
A
ttach
Wiki Help
r4 - 13 Nov 2006 - 17:23:54 - TravisVachon
You are here:
OSAF
>
Projects Web
>
CosmoHome
>
CosmoDevelopmentHome
>
JavaScriptStyleguide
---+ JS Styleguide These are merely guidelines. They should not be adhered to mechanically, especially if a deviation would make your code more readable. ---++General 1. Code SHOULD be indented with spaces only. 2. Each level of indentation SHOULD consist of 4 spaces. 1. Files SHOULD include the Apache License Version 2 notice and the OSAF copyright notice at the top. 1. Lines SHOULD be 80 characters max. ---++Naming ---+++Overview | *Construct* | *Convention* | *Examples* | |Class name |!CamelCase with an initial capital letter |<code>MightyBalrog, !MagicWeapon</code> | |Public method |camelCase with an initial lowercase letter |<code>castDimensionalDoorway, rollForInitiative</code> | |Public variable |camelCase with an initial lowercase letter |<code>materialComponents, hasTrackingAbilities</code> | |Private method |camelCase with an initial lowercase letter and underscore |<code>_getHealth</code> | |Private variable |camelCase with an initial lowercase letter and underscore |<code>_backstabAbility</code> | |Method arguments |camelCase with an initial lowercase letter |<code>halfOrcArmy</code> | |Local variables |camelCase with an initial lowercase letter |<code>isHumanoid, levelCount</code> | |Constant |Uppercase with underscores |<code>CLERIC_PLAYER, GAME_MASTER</code> | ---+++Notes 1. Variable/method names in all lowercase with underscores SHOULD NOT be used unless mimicking another API. * Incorrect:<pre><code>wizard_hat, vorpal_blade</code></pre> * Correct:<pre><code>wizardHat, vorpalBlade</code></pre> 1. Acronyms in variable/method names SHOULD NOT be upppercased. * Incorrect:<pre><code>bartenderNPC, newRPG</code></pre> * Correct:<pre><code>bartenderNpc, newRpg</code></pre> 1. Variable/method names SHOULD be written in English. * Incorrect:<pre><code>dekaiKatana</code></pre> * Correct:<pre><code>giganticSword</code></pre> 1. Variable/method names SHOULD NOT be abbreviated to the point of being unclear. * Incorrect:<pre><code>wndMnstr[3]</code></pre> * Correct:<pre><code>wanderingMonster[3]</code></pre> ---++Variables 1. 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. * Incorrect:<pre><code>var magicItemCount; var wizardNpc;</code></pre> * Correct:<pre><code>var magicItemCount = 0; var wizardNpc = null;</code></pre> 1. Variable declarations SHOULD NOT include extra spaces before the equals sign to align the variable values. * Incorrect:<pre><code>var currentThiefLevel = 8; var canBackstab = true; var isNpc = true;</code></pre> * Correct:<pre><code>var currentThiefLevel = 8; var canBackstab = true; var isNpc = true;</code></pre> 1. Variable names SHOULD NOT include 'temp' or 'tmp'. -- all local variables are by definition temporary. * Incorrect:<pre><code>tempString, tmpDate</code></pre> * Correct:<pre><code>str, dt</code></pre> 1. Magic numbers SHOULD NOT be used. Use a constant instead. * Incorrect:<pre><code>42</code></pre> * Correct:<pre><code>ANSWER_TO_THE_QUESTION_OF_LIFE</code></pre> ---++Coding Style ---+++Overview 1. Function declaration:<pre><code>function checkForTraps(dexterity, level) { // Do stuff to check for traps here }</code></pre> 1. If statements:<pre><code>if (gotInitiative) { attackDragon(); } else if (speaksDragon) { tryNegotiating(); } else { runAway(); }</code></pre> 1. For statements:<pre><code>for (var i = 0; i < guards.length; i++) { rollTwentySided(guards[i]); }</code></pre> 1. While statements:<pre><code>while (charactersInjured) { castCureLightWounds(); charactersInjured = checkCharacterHealth(); }</code></pre> 1. 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> 1. Try-catch-finally statements:<pre><code>try { pickPocket(); } catch(e) { lookInconspicuous(); reportBack(e); } finally { runLikeHell(); }</code></pre> 1. 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> ---+++Notes 1. If-else statements (also while, et al) MAY be written on a single line, but MUST use brackets. * Incorrect:<pre><code>if (isUndead) grabFire();</code></pre> * Correct:<pre><code>if (isUndead) { grabFire(); }</code></pre> 1. Parenthesis in conditional statements (if, while, for, etc.) SHOULD have a space before them. * Incorrect:<pre><code>if(isNpc) { ignoreTalk(); }</code></pre> * Correct:<pre><code>if (isNpc) { ignoreTalk(); }</code></pre> 1. Parenthesis in function declarations SHOULD NOT have a space before them. * Incorrect:<pre><code>function getArmorClass (armorType, dexterity) { // Get AC stuff here }</code></pre> * Correct:<pre><code>function getArmorClass(armorType, dexterity) { // Get AC stuff here }</code></pre> 1. Commas SHOULD be followed by spaces. * Incorrect:<pre><code>getExperiencePoints(monster,hitPoints);</code></pre> * Correct:<pre><code>getExperiencePoints(monster, hitPoints);</code></pre> 1. The colon in object literal notation SHOULD have no space in front of it, and be followed by a single space. * Incorrect:<pre><code>var newCharacter = { race:'gnome', class:'figheter' isNpc:false; }</code></pre> * Also incorrect:<pre><code>var newCharacter = { race : 'gnome', class : 'figheter' isNpc : false; }</code></pre> * Correct:<pre><code>var newCharacter = { race: 'gnome', class: 'figheter' isNpc: false; }</code></pre> 1. The conditional operator (?) and the colon used with it SHOULD both have a space before and after. * Incorrect:<pre><code>var message = speaksDrow? getMessageinDrow():'You do not speak Drow.'</code></pre> * Correct:<pre><code>var message = speaksDrow ? getMessageinDrow() : 'You do not speak Drow.'</code></pre> 1. Lengthy DOM element IDs or other string parameters SHOULD be placed into variables before using. * Incorrect:<pre><code>var elem = document.getElementById('charClass-' + charClass + + '_combatStats-' + armorClass + '-' + toHitBonus);</code></pre> * Correct:<pre><code>var char = 'charClass-' + charClass; var combat = 'combatStatus-' + armorClass + '-' + toHitBonus; var elem = document.getElementById(char + '_' + combat);</code></pre>
E
dit
|
W
YSIWYG
|
A
ttach
|
P
rintable
|
V
iew topic
|
Backlinks: We
b
, A
l
l Webs
|
H
istory: r4 <
r3
<
r2
<
r1
|
M
ore topic actions
chandlerproject.org
VISION
TOUR
Download Desktop
Get a Hub Account
Get Chandler Server
FAQ
Get Started
Blog
Mailing Lists
Chat on IRC
Report a Bug
Get Involved
OSAF Community
About OSAF
Projects Wiki Changes
Open Source Applications Foundation
Dansk
Deutsch
English
Español
Finnis
Français
Italiano
Nederlands
Polski
Português
Svenska
简体中文
繁體中文
Except where otherwise noted, this site and its content are licensed by OSAF under an Creative Commons License, Attribution Only 3.0.
See
list of page contributors
for attributions.