Firebug Coding Style

From FirebugWiki

This document attempts to explain the basic styles and patterns, that are used in the Firebug codebase. New code should try to conform to these standards, so that it is as easy to maintain as existing code. Of course every rule has an exception, but it's important to know the rules nonetheless!

The incrementation and decrementation operators are not separated by spaces. So e.g. you should write while(i-- > 0) instead of while(i --> 0) to avoid misinterpretations.

Source File Size

A source file should avoid huge amount of code lines. Couple thousands of lines in a file is already a lot. Firebug is using AMD syntax and more smaller files (modules) is preferred.

Line Length

100 characters or less. There is no exception in *.js files! In some cases this rule can be broken in *.html or *.xul files. But keep in mind long lines are hard to read (also search results are hard to read).

When wrapping lines, operators stay at the end of a line.

if((... &&
...)||
...){}

var string = ... +
...;

Also member operators stay at the end of the line.

var service = Cc[...].
getService(...);

Indentation

Four spaces per logic level.

Commands

Every command must end with a semicolon and must be on its own line.

var foo =1;var bar =2;
foo = someFunction();
bar +=3;return bar;

License

Files should include a license note at the first line of the file:

/* See license.txt for terms of usage */
...

In case of *.xml files (e.g. in overlays), this must be after XML declaration, for example:

<?xmlversion="1.0"?><!-- See license.txt for terms of usage -->
...
</xml>

In case of *.properties or *.manifest files, this must be commented using # character.

# See license.txt for terms of usage
...

Control Structures

Code Blocks

Existing Firebug codebase uses braces on the next line like as follows:

function foo(){// ...}

Yes, there can be exceptions and K&R style can be preferred in some cases. For example, definition of a config object.

Control structures should look like as follows (also notice the spacing between a keyword and the left bracket):

if(...){
...
}elseif(...){
...
}

Another example showing how to deal with spaces:

if((a >0)&&(b >0)){}

switch(...){case1:
...
break;case2:
...
break;default:
...
}

try{
...
}catch(err){
...
}

Loops

To avoid misunderstandings for loops are always written in their long form, i.e. loop heads like for (var i = count; i--; ) should be avoided in favor of for (var i = count - 1; i >= 0; i--).
Also there are spaces between the statements and the operands and you should write i++ instead of ++i.

for(var i =0; i <10; i++){
...
}

Ternary expressions

Ternary expressions must be wrapped in brackets for clarity.

var variable =(condition ?true:false);

Braces and brackets

Firebug prefers no braces, if they are not necessary.

if(...)
dump(true);else
dump(false);

But if one of the branches needs braces use them for all. Also note

if(...){
dump("0");
dump("1");}else{
dump("2");}

If the head of a control structure is wrapped into several lines because it is longer than the maximum line length, also use braces, even when the block just contains one line.

if(...
...){
dump("0");}else{
dump(2);}

Firebug prefers no brackets for operators like typeof.

if(typeof variable =="object")returnfalse;

Functions

Always use parentheses around arrow function parameters. If an arrow function is a simple one-liner, braces around the function body and the return statement are not necessary.

Comments

Multi-line as well as single line comments should always be put into their own line.
So you should write:

// This is a commentvar abc = xyz;

Comments for blocks of code will be placed above them.

if(foo =="bar"){
...
}// foo is not set to "bar"else{
...
}

To describe what specific functions do, especially published APIs, JSDoc comments are used.

Firebug codebase also uses the following horizontal separator for dividing members of one object (this separator uses indentation (4 spaces) since it's used within an object scope that is indented (100 characters long).

The preferred sorting order for the other modules is alphabetically ascending.

Naming

American English is used for all labels and comments. That means, that you should write e.g. synchronize instead of synchronise or color instead of colour.

Functions and Methods

Functions should use camelCase but should not capitalize the first letter.

Functions names must start with a verb. There are two exceptions to this rule, though:

Functions passed as parameters to another function, i.e. callbacks. They can be named just callback or describe the event when they are called.

Event handlers should be prefixed by "on" to describe when the handler is called.

function initialize(){
...
}

function getLocationList(){
...
}

function eachPanel(callback){
...
}

function setBreakpoints(afterBreakpointsAdded){
...
}

onBreakpointAdded:function(context, bp){
...
}

Objects

Constructors for objects should be capitalized and use CamelCase.

function ObjectConstructor(){
...
}

Firebug.MyModule= extend(Firebug.Module,{});

MyObject.prototype=(){
myMethod:function(){
...
}};

Constants

Constants should be capitalized as follows:

var MY_CONSTANT =true;

Use var instead of const, since the code can also be used in the browser environment where const is not supported.

Variables

Variables should use camelCase and not capitalize the first letter. Variable definitions should be done separately, not comma-separated. They don't have to be initialized immediately but before they are used.
If abbreviations are used, they should be written lower case at the beginning of a variable but in capitals when they are in the middle of the name. Also they should not be a single character.