ProposedCodingConventions
Aim: to make the Tiddlywiki source code more compact while still maintaining readability.
These two aims make most of the choices for us: readability means we should adopt a "standard" layout convention, source code compactness means we should adopt the most compact of the "standard" conventions. This turns out to be the K&R conventions, see:
http://en.wikipedia.org/wiki/Indent_style#K.26R_style
K&R puts the first opening brace on the same line as control statement, indents the statements within the braces, and puts the closing brace on the same indentation level as the control statement (on a line of its own). The opening brace for function definitions is on a line of its own.
Rudimentary example follows at end of this page.
Whitespace
1) Indentation should be by tabs not spaces.
2) There is no whitespace between a keyword and any following bracket, eg: while(true)...
3) There should be whitespace around assignment and conditional operators, eg:
var a = b;
if(b < c)
4) Exception to rule (3): for statements, use(eg):
for(var i=0; i<t.length ; i++)...
Line breaks
Long statements may be broken after an operator. Continuation statements should be indented by two tabs from original statement (and not aligned to the assignment operator).
Conditional assignment
Conditional assigment is prefered when it can be used.
Return statements
Return values should not be enclosed in brackets eg return false; not return(false);
Example
Wikifier.prototype.example = function(output)
{
...
while(formatterMatch) {
this.matchText = formatterMatch[0];
...
for(var i=1; i<formatterMatch.length; i++) {
if(formatterMatch[i]) {
this.formatter.formatters[i-1].handler(this);
this.formatter.formatterRegExp.lastIndex = this.nextMatch;
break;
}
}
formatterMatch = this.formatter.formatterRegExp.exec(this.source);
}
if(a < b) {
this.nextMatch = this.source.length*2;
} else if(a < c) {
this.nextMatch = this.source.length+4;
} else {
this.nextMatch = this.source.length;
}
}