Actions

Difference between revisions of "Developer Area/Coding guidelines/JS files"

From Mahara Wiki

< Developer Area‎ | Coding guidelines
 
(6 intermediate revisions by the same user not shown)
Line 59: Line 59:
 
Good:
 
Good:
  
<div class="plugin tightenable"><code><font color="#000000"> <font color="#0000bb">$found </font><font color="#007700">= </font><font color="#0000bb">false</font><font color="#007700">;<br /></font><font color="#0000bb">$ponies </font><font color="#007700">= </font><font color="#0000bb">0</font><font color="#007700">;<br /></font><font color="#0000bb">$servername </font><font color="#007700">= </font><font color="#0000bb">$overkill</font><font color="#007700">;<br /><br /></font><font color="#ff8000">// Only in relation to controlling for/while loops<br /></font><font color="#007700">for (</font><font color="#0000bb">$i </font><font color="#007700">= </font><font color="#0000bb">10</font><font color="#007700">; </font><font color="#0000bb">$i </font><font color="#007700">&gt; </font><font color="#0000bb">5</font><font color="#007700">; </font><font color="#0000bb">$i</font><font color="#007700">--) {<br />     echo </font><font color="#0000bb">$i</font><font color="#007700">;<br /> }<br /><br /></font><font color="#ff8000">// Aligning = signs<br /></font><font color="#0000bb">$mine   </font><font color="#007700">= </font><font color="#0000bb">0</font><font color="#007700">;<br /></font><font color="#0000bb">$yours  </font><font color="#007700">= </font><font color="#0000bb">0</font><font color="#007700">;<br /></font><font color="#0000bb">$theirs </font><font color="#007700">= </font><font color="#0000bb">0</font><font color="#007700">;<br /><br /></font><font color="#ff8000">// Only if the number of elements to initialise is small.<br /></font><font color="#0000bb">$array </font><font color="#007700">= array(</font><font color="#0000bb">1 </font><font color="#007700">=&gt; </font><font color="#dd0000">'hello'</font><font color="#007700">);<br /><br /></font><font color="#ff8000">// If large number of elements to initialise:<br /></font><font color="#0000bb">$array </font><font color="#007700">= array(<br />     </font><font color="#0000bb">1  </font><font color="#007700">=&gt; </font><font color="#dd0000">'hello'</font><font color="#007700">,<br />     </font><font color="#0000bb">2  </font><font color="#007700">=&gt; </font><font color="#dd0000">'goodbye'</font><font color="#007700">,<br />     </font><font color="#0000bb">10 </font><font color="#007700">=&gt; </font><font color="#dd0000">'erm'<br /></font><font color="#007700">);<br /></font> </font> </code></div>
+
<div class="plugin tightenable"><code><font color="#000000"> <font color="#0000bb">var found </font><font color="#007700">= </font><font color="#0000bb">false</font><font color="#007700">;<br /></font><font color="#0000bb">var ponies </font><font color="#007700">= </font><font color="#0000bb">0</font><font color="#007700">;<br /></font><font color="#0000bb">var servername </font><font color="#007700">= </font><font color="#0000bb">var overkill</font><font color="#007700">;<br /><br /></font><font color="#ff8000">// Only in relation to controlling for/while loops<br /></font><font color="#007700">for (var </font><font color="#0000bb">i </font><font color="#007700">= </font><font color="#0000bb">10</font><font color="#007700">; </font><font color="#0000bb">i </font><font color="#007700">&gt; </font><font color="#0000bb">5</font><font color="#007700">; </font><font color="#0000bb">i</font><font color="#007700">--) {<br />     tmp += </font><font color="#0000bb">i</font><font color="#007700">;<br /> }<br /><br /></font><font color="#ff8000">// Aligning = signs<br /></font><font color="#0000bb">var mine   </font><font color="#007700">= </font><font color="#0000bb">0</font><font color="#007700">;<br /></font><font color="#0000bb">var yours  </font><font color="#007700">= </font><font color="#0000bb">0</font><font color="#007700">;<br /></font><font color="#0000bb">var theirs </font><font color="#007700">= </font><font color="#0000bb">0</font><font color="#007700">;<br /><br /></font><font color="#ff8000">// Only if the number of elements to initialise is small.<br /></font><font color="#0000bb">var object </font><font color="#007700">= [</font><font color="#0000bb">1 </font><font color="#007700">=&gt; </font><font color="#dd0000">'hello'</font><font color="#007700">];<br /><br /></font><font color="#ff8000">// If large number of elements to initialise:<br /></font><font color="#0000bb">var array </font><font color="#007700">= {<br />     </font><font color="#0000bb">1  </font><font color="#007700">: </font><font color="#dd0000">'hello'</font><font color="#007700">,<br />     </font><font color="#0000bb">2  </font><font color="#007700">: </font><font color="#dd0000">'goodbye'</font><font color="#007700">,<br />     </font><font color="#0000bb">10 </font><font color="#007700">: </font><font color="#dd0000">'erm'<br /></font><font color="#007700">};<br /></font> </font> </code></div>
 +
 
 +
TODO: mention actual arrays, we have only done objects
  
 
Bad:
 
Bad:
  
<div class="plugin tightenable"><code><font color="#000000"> <font color="#ff8000">// Negative name<br /></font><font color="#0000bb">$notfound </font><font color="#007700">= </font><font color="#0000bb">true</font><font color="#007700">;<br /></font><font color="#ff8000">// Underscores<br /></font><font color="#0000bb">$my_variable </font><font color="#007700">= </font><font color="#dd0000"><nowiki>''</nowiki></font><font color="#007700">;<br /></font><font color="#ff8000">// Hungarian. Eew....<br /></font><font color="#0000bb">$count_i </font><font color="#007700">= </font><font color="#0000bb">6</font><font color="#007700">;<br /></font><font color="#ff8000">// Uppercase letters<br /></font><font color="#0000bb">$myFish </font><font color="#007700">= </font><font color="#0000bb">$yourFish</font><font color="#007700">;<br /><br /></font><font color="#ff8000">// Not necessarily "bad", just not for this project<br /></font><font color="#0000bb">$array </font><font color="#007700">= array(<br />                 </font><font color="#0000bb">1 </font><font color="#007700">=&gt; </font><font color="#dd0000">'hello'</font><font color="#007700">,<br />                 </font><font color="#0000bb">2 </font><font color="#007700">=&gt; </font><font color="#dd0000">'goodbye'</font><font color="#007700">,<br />                 </font><font color="#0000bb">10 </font><font color="#007700">=&gt; </font><font color="#dd0000">'erm'<br />               </font><font color="#007700">);<br /></font> </font> </code></div></div><div id="section_8">
+
<div class="plugin tightenable"><code><font color="#000000"> <font color="#ff8000">// Negative name<br /></font><font color="#0000bb">notfound </font><font color="#007700">= </font><font color="#0000bb">true</font><font color="#007700">;<br /></font><font color="#ff8000">// Underscores<br /></font><font color="#0000bb">var my_variable </font><font color="#007700">= </font><font color="#dd0000"><nowiki>''</nowiki></font><font color="#007700">;<br /></font><font color="#ff8000">// Hungarian. Eew....<br /></font><font color="#0000bb">var count_i </font><font color="#007700">= </font><font color="#0000bb">6</font><font color="#007700">;<br /></font><font color="#ff8000">// Uppercase letters<br /></font><font color="#0000bb">var myFish </font><font color="#007700">= </font><font color="#0000bb">var yourFish</font><font color="#007700">;<br /><br /></font><font color="#ff8000">// Not necessarily "bad", just not for this project<br /></font><font color="#0000bb">var object </font><font color="#007700">= {<br />                 </font><font color="#0000bb">1 </font><font color="#007700">: </font><font color="#dd0000">'hello'</font><font color="#007700">,<br />                 </font><font color="#0000bb">2 </font><font color="#007700">: </font><font color="#dd0000">'goodbye'</font><font color="#007700">,<br />                 </font><font color="#0000bb">10 </font><font color="#007700">: </font><font color="#dd0000">'erm'<br />               </font><font color="#007700">};<br /></font> </font> </code></div></div><div id="section_8">
  
 
==Binary Operators==
 
==Binary Operators==
Line 69: Line 71:
 
Always have a space on each side:
 
Always have a space on each side:
  
<div class="plugin tightenable"><code><font color="#000000"> <font color="#007700">echo </font><font color="#0000bb">1 </font><font color="#007700">+ </font><font color="#0000bb">2</font><font color="#007700">;<br /> if (</font><font color="#0000bb">1 </font><font color="#007700">== </font><font color="#0000bb">2</font><font color="#007700">)<br /> echo </font><font color="#dd0000">'hello' </font><font color="#007700">. </font><font color="#dd0000">' world'</font><font color="#007700">;<br /></font> </font> </code></div>
+
<div class="plugin tightenable"><code><font color="#000000"> <font color="#007700">return </font><font color="#0000bb">1 </font><font color="#007700">+ </font><font color="#0000bb">2</font><font color="#007700">;<br /> if (</font><font color="#0000bb">1 </font><font color="#007700">== </font><font color="#0000bb">2</font><font color="#007700">)<br /> return </font><font color="#dd0000">'hello' </font><font color="#007700">. </font><font color="#dd0000">' world'</font><font color="#007700">;<br /></font> </font> </code></div>
  
 
<nowiki>|| and &amp;&amp; will always be used for 'or' and 'and' respectively. Resolve priority collisions with extra brackets as required.</nowiki>
 
<nowiki>|| and &amp;&amp; will always be used for 'or' and 'and' respectively. Resolve priority collisions with extra brackets as required.</nowiki>
Line 77: Line 79:
 
'''Declaration:'''
 
'''Declaration:'''
  
<div class="plugin tightenable"><code><font color="#000000"> <font color="#ff8000">/**<br /> * PHPDoc comment describing the function<br /> *<br /> * Note how the default value has no spaces<br /> */<br /></font><font color="#007700">function </font><font color="#0000bb">foo</font><font color="#007700">(</font><font color="#0000bb">$a</font><font color="#007700">, Class </font><font color="#0000bb">$b</font><font color="#007700">, </font><font color="#0000bb">$c</font><font color="#007700">=</font><font color="#dd0000"><nowiki>''</nowiki></font><font color="#007700">) {<br />     </font><font color="#ff8000">// source<br />     </font><font color="#007700">return </font><font color="#0000bb">$value</font><font color="#007700">;<br /> }<br /><br /> class </font><font color="#0000bb">FooBar </font><font color="#007700">extends </font><font color="#0000bb">Bar </font><font color="#007700">{<br /><br />     </font><font color="#ff8000">/**<br />      * PHPDoc for the field<br />      *<br />      * For this project, do not use doThis.<br />      */<br />     </font><font color="#0000bb">public </font><font color="#007700">function </font><font color="#0000bb">do_this</font><font color="#007700">(</font><font color="#0000bb">$c</font><font color="#007700">, </font><font color="#0000bb">$d</font><font color="#007700">) {<br />         </font><font color="#ff8000">// source<br />     </font><font color="#007700">}<br /><br /> }<br /></font> </font> </code></div>
+
<div class="plugin tightenable"><code><font color="#000000"> <font color="#ff8000">/**<br /> * (JSDoc?) comment describing the function<br /> *<br /> * Note how the default value has no spaces<br /> */<br /></font><font color="#007700">function </font><font color="#0000bb">foo</font><font color="#007700">(</font><font color="#0000bb">a</font><font color="#007700">, Object </font><font color="#0000bb">b</font><font color="#007700">, </font><font color="#0000bb">c</font><font color="#007700">=</font><font color="#dd0000"><nowiki>''</nowiki></font><font color="#007700">) {<br />     </font><font color="#ff8000">// source<br />     </font><font color="#007700">return </font><font color="#0000bb">value</font><font color="#007700">;<br /> }<br /><br /></font></font> </code></div>
  
 
'''Calling:'''
 
'''Calling:'''
  
<div class="plugin tightenable"><code><font color="#000000"> <font color="#0000bb">$result </font><font color="#007700">= </font><font color="#0000bb">foo</font><font color="#007700">(</font><font color="#0000bb">$bar</font><font color="#007700">, </font><font color="#0000bb">$baz</font><font color="#007700">);<br /></font><font color="#0000bb">$mine </font><font color="#007700">= </font><font color="#0000bb">$object</font><font color="#007700">-&gt;</font><font color="#0000bb">method</font><font color="#007700">(</font><font color="#0000bb">$a</font><font color="#007700">, </font><font color="#0000bb">$b</font><font color="#007700">);<br /><br /></font></font></code></div></div>
+
<div class="plugin tightenable"><code><font color="#000000"> <font color="#0000bb">result </font><font color="#007700">= </font><font color="#0000bb">foo</font><font color="#007700">(</font><font color="#0000bb">bar</font><font color="#007700">, </font><font color="#0000bb">baz</font><font color="#007700">);<br /></font><font color="#0000bb">mine </font><font color="#007700">= </font><font color="#0000bb">object</font><font color="#007700">.</font><font color="#0000bb">method</font><font color="#007700">(</font><font color="#0000bb">a</font><font color="#007700">, </font><font color="#0000bb">b</font><font color="#007700">);<br /><br /></font></font></code></div></div>
 
 
=Javascript=
 
All files must adhere to the [[BasicJSFileTemplates]].
 
Javascript files should roughly follow [http://javascript.crockford.com/code.html Crockford's code conventions].
 

Latest revision as of 01:06, 30 August 2012

This page documents the coding conventions used by Mahara for JavaScript files. Everything that ends up in the git repository (other than third-party code) should follow these standards.

You can also have a look at the Reviewer's manual to see what reviewers will look at when looking at code submissions.

For other languages, use the links at the main guideline page.

These guidelines are based on Crockford's code conventions.


The Basics

All files must adhere to the BasicJSFileTemplates.

Indentation is using four (4) spaces. There is no requirement that the scripts are wrapped at 80 columns, however developers are asked to wrap long lines sensibly.

Comments

Comments of up to three lines in length should use the // comment marker. Comments longer than three lines should use /* ... */. Here is an example comment:

/*
   This part is difficult, and requires a long
   explanation. This is that explanation. I am
   still typing to see if I can make it to four
   lines.
*/

Please note that the opening and ending markers are on their own lines - nothing else should be on these lines.


Language Constructs

Strings

Strings should be quoted with single quotes (') unless you are interpolating a variable.


if/while/for statements

There is one space after each language construct, and no spaces between brackets and the arguments inside them, unless what is inside them becomes too long for one line, in which case a special format will be used.

Examples of what is good:

if (1 == 2) {
    return
'omg';
}

while (
i < 3) {
    return
'I am ' + i;
}

for (
i = 0, j = 7; i < 34, j != 3; i++, j--) {
    return (
i * j) + ' is a silly number';
}

// Note bracing style of else if/else clauses
// Also note: do not use elseif
if (a == b) {
    return
"a = b";
}
else if (
c == d) {
    return
"c = d";
}
else {
    return
"e = ?";
}

// Ternary operator
a = (b == true) ? 'hello' : 'goodbye';
// When used with other things on each side in the same statement:
a = 'foo' + ((b == true) ? 'hello' : 'goodbye') + 'bar';

// A complicated condition. Note location of condition,
// and location of ending bracket and brace.
// todo: what was the decision here regarding conditions on the "if (" line?
if (
    
sacrificedgoats == availablegoats
    
&& (dogs == cats
    
|| 1 != 2)
    || (
yesterday >> tomorrow == 4)
) {
    
// Do stuff
}

Examples of what is bad:

if ( a == b )
{
    return
'hi';
}

if(
c == d) {
    return
'hi';
} else {
    return
'bye';
}

if (
e==f)
{
    return
'hi';
}
else
{
    return
'bye';
}

What should be never used:

// For a really short condition
if (a == b) c = 1;

// Just don't. Not even ow.
do {
    echo
'hi';
} while (
a == 1);

Variables and Arrays

Variables should be named with no underscores, and no capital letters. This policy may become more lenient in future if variable names are discovered that are not easily readable without underscores.

Do not name variables as negatives where possible - always use positive names and logical inversion (!) where required.

Hungarian notation is forbidden.

When related variables are initialised, the = signs for them should line up. Other than that, aligning = signs for variable assignment is up to the developer, although they should use common sense in such decisions.

Good:

var found = false;
var ponies = 0;
var servername = var overkill;

// Only in relation to controlling for/while loops
for (var i = 10; i > 5; i--) {
    tmp +=
i;
}

// Aligning = signs
var mine   = 0;
var yours  = 0;
var theirs = 0;

// Only if the number of elements to initialise is small.
var object = [1 => 'hello'];

// If large number of elements to initialise:
var array = {
    
1  : 'hello',
    
2  : 'goodbye',
    
10 : 'erm'
};

TODO: mention actual arrays, we have only done objects

Bad:

// Negative name
notfound = true;
// Underscores
var my_variable = '';
// Hungarian. Eew....
var count_i = 6;
// Uppercase letters
var myFish = var yourFish;

// Not necessarily "bad", just not for this project
var object = {
                
1 : 'hello',
                
2 : 'goodbye',
                
10 : 'erm'
              
};

Binary Operators

Always have a space on each side:

return 1 + 2;
if (
1 == 2)
return
'hello' . ' world';

|| and && will always be used for 'or' and 'and' respectively. Resolve priority collisions with extra brackets as required.

Functions and Methods

Declaration:

/**
* (JSDoc?) comment describing the function
*
* Note how the default value has no spaces
*/
function foo(a, Object b, c='') {
    
// source
    
return value;
}

Calling:

result = foo(bar, baz);
mine = object.method(a, b);