Skip to content

Syntax and Coding Style Conventions

Thomas Aylott edited this page Jun 21, 2011 · 1 revision

These are syntax and style conventions extracted from the MooTools core code. This page has not been written by an official MooTools developer and is solely intended to provide a basis for plugin developers to assimilate their coding style to the framework’s.

Whitespace

MooTools uses tabs for indentation. Trailing whitespace is discouraged as well as indentation for empty lines.

Comments

The commenting style of the framework preserves the Natural Docs syntax. Since Natural Docs is no longer being used by the MooTools project, the commenting style is up to the plugin developer.

Brackets

  • They do not take their own lines when beginning a statement.

function $A(iterable){
	...
}

  • The compound statements that involve only a simple statement when executed make the use of brackets and line break optional. This includes the case statement. The else statement is an exception.

if (typeof a1 == 'string') return add(this, a1, a2, a3);


case 1: return 'element';


} else {
	return ctx.getElementsByTagName(tag);
}

Statement terminator ;

Simple Statements.

It’s always included.


var unlinked;

Compound Statements.

It’s not included after the closing bracket, with the exception of the global object literal definition.


var MooTools = {
	'version': '1.2.0',
	'build': ''
};

Function Expressions

It’s always included after the closing bracket.


var foo = function(){
	...
};

Function Declarations

It’s never included after the closing bracket.


function foo(){
	...
}

Separation

Spaces

  • As previously pointed out, brackets accompany function parameters without a space, which accompany the function keyword without a space.
  • Operators are always separated with single spaces within the expression. The negation operator is an exception.
  • Object literal expressions do not include extra spaces at the beginning or end when used as parameter.

this.headers.extend({'Accept': 'application/json', 'X-Request': 'JSON'});

  • All reserved keywords are followed by spaces, with the exception of the function definition.

object.implement = function(a1, a2, a3){
	if (typeof a1 == 'string') return add(this, a1, a2, a3);
	for (var p in a1) add(this, p, a1[p], a2);
	return this;
};

Linebreaks

  • Statements within a bracketed block are separated with a single line break. Optionally, for the sake of readability, it’s observed that extra line breaks are used to separate logical pieces of code within the same compound statement. See Request::send for an example of this.
  • Classes initialization, function definitions, generic objects and global function calls are separated from each other with two line breaks (an empty line between them).
  • Blocks of code that belong to the same component are separated with three line breaks (two empty lines), followed by comments describing the component.
  • Each component commented description and license is succeeded with two line breaks (an empty line).
  • Classes definition include empty lines after the opening bracket and before the closing bracket.

Request.JSON = new Class({

	Extends: Request,
        ...
	success: function(text){ }

}); 

Indentation

A single tab is used to indent code within a bracketed compound statement, unless it has been shortened. This also applies to subsequent, inner statements.

Quotation

The use of the single quotation mark is preferred.

In Object literal expressions, quotation is generally omitted for the key part.


sender.send({data: this, url: url || sender.options.url});