blob: 0d7b04fdf6068f5eb33a18c29ec85e30a0a4912e [file]
#summary Style guide for JavaScript code contributed to Melange
#labels Importance-Useful,Phase-Guidelines,Contents-Draft
As more JavaScript code in Melange is going to be developed, here's a draft for code guidelines. These guidelines are inspired by [PythonStyleGuide] page (but adapted to JS), and from [http://javascript.crockford.com/code.html Code conventions for the Javascript Programming Language]. In this very moment our code doesn't respect them, but these are going to be strict rules when [JavascriptRefactoring] will come to an end. More infos on _why_ these rules are important later.
= Javascript Language Rules =
1. _Private and public/privileged vars/functions_: use the *this* keyword to declare public vars and privileged functions. Have a look at this [http://javascript.crockford.com/private.html great description on how to achieve them]
2. _With statement_: Do *not* use *with* statement. If you want to provide shortcuts, just create a variable that points to the object you want to access repeatedly.
3. _Iterations_: as we've jQuery always loaded, use [http://docs.jquery.com/Utilities/jQuery.each jQuery's each() function] to iterate over objects/arrays
4. _Ajax calls_: *always* use {{{?=_"+(new Date().getTime())}}} when invoking an URL to avoid caching issues
5. _Global namespace pollution_: at least *avoid* it (more infos when we come to an agreement for [JavascriptRefactoring])
= Javascript Style Rules =
1. _Semicolons_: *Always*
2. _Line length_: *80* columns maximum. If you need to break one line, indent it by *2* spaces
3. _Indentation_: *2* spaces (*no tabs*)
4. _Whitespaces_: *no* whitespaces inside parentheses, brackets or braces
5. _HTML Django templates_: there should be *no* code in them
6. _Comments_: start them with *lower cases*, we might want to introduce [http://code.google.com/p/jsdoc-toolkit/ JSDocToolkit] style in the next future
7. _Functions_: *no space* between the _name of the function_ and the parenthesis
8. _Anonymous functions_: *1 space* between the word _function_ and the parenthesis
9. _Immediately invoked functions_: wrap them *between parenthesis*, as of:
{{{
var myvar = (function () {
return true;
}());
}}}
10. _Objects and arrays_: use {{{ {} }}} to declare objects (not {{{new Object()}}}) and {{{[]}}} to declare arrays (not {{{new Array()}}})