-
Notifications
You must be signed in to change notification settings - Fork 5
Code Conventions and Style
First thing to work together on the same project: Conventions. Let's try to use a consistent coding style while building ReFuel so that others can easily read and understand our code.
We take as a 'de facto' standard the rules presented in the article Code Conventions for the JavaScript Programming Language by Douglas Crockford
Please be sure to read these rules and to follow them slavishly.
Indentation: We use tabs, not spaces. Please configure your editor to follow this convention.
Line Length: It is recommended (but not requested) to Keep lines shorter than 80 characters.
Variable Declaration: Define all variables before the use, always at the beginning of the function. Add a comment to the declaration if needed.
Variable Naming: Use descriptive names, avoid single or double characters variable names. All variables must be in lower-camel-case.
Quote or double quote? Use quote (') for all strings in your code (basically), double (") is reserved to jSon format
var brandNewObject = {};
brandNewObject.myProperty = 'something';Function Declaration: Put no space between the name of a function and the parenthesis containing the parameters. Put one space between the right parenthesis and the curly brace that begins the statement body. If a function literal is anonymous, there should be one space between the word 'function' and the left parenthesis. If the space is omited, then it can appear that the function's name is function, which is an incorrect reading.
Function Naming: Use descriptive names, avoid single or double characters function names. All function names must be in lower-camel-case.
function myBrandNewFunction(arrayParam, anotherParam) {
var arrayLength = arrayParam.length;
for (var i = 0; i < arrayLength; i ++) {
if (arrayParam[i] != null) {
console.log(arrayParam[i]);
}
}
}
Naming General Rules: Avoid use of international or special characters. Just use letters and numbers. The only special characters allowed are minus (-) and underscore (_) but not at the beginning or at the end of a name. Also don't use them both in the same name.
Class Naming: Class must be named in Upper-Camel-Case to distinguish them from normal variables.
Constants Naming: Constants Variables must be in UPPERCASE with words separated by underscores.
Global Variables Naming: There is no global variables or functions naming rule because: There is No global variables or functions. Everything you declare that is part of the framework must be inside the ReFuel scope.
ReFuel.DuckPark = function (ducksArray) {
var COUNT_PREFIX = 'There are ';
var COUNT_SUFFIX = ' ducks!';
var ducksStorage = ducksArray;
this.countDucks = function() {
return COUNT_PREFIX + ducksStorage.length + COUNT_SUFFIX;
}
}
var myPark = new ReFuel.DuckPark(['Betty', 'Booty']);
console.log(myPark.countDucks());
These rules and code examples are not complete neither exhaustive, only the most important concepts are covered in this guide, if a matter is not covered in this article refer to Code Conventions for the JavaScript Programming Language and search for the use case that better suits your situation.
Event Naming: Event names must be descriptive but generic, therefore can't contain references to the dispatcher object. An Event name is composed by a prefix that defines the timing of the event (before, on, after), a word defining the action without any declination (Load, Click, Open, Close, Resize, Change, Enter), the state in which the action is (Start, Complete, Progress, Error). You should avoid the creation of new event names if existing events can fit your needs. Most event names are contained in constants to avoid mismatches and to maximize the minifier compression.
Event.LOAD_COMPLETE = 'onLoadComplete';
Event.CHANGE = 'onChange';
Event.dispatchEvent(Event.CHANGE);