Firebug Coding Style

From FirebugWiki

(Difference between revisions)
Jump to: navigation, search
(Naming)
(Line Length)
Line 14: Line 14:
=== Line Length ===
=== Line Length ===
-
100 characters or less. There is no exception is *.js files! In some cases this rule can be broken in *.html or *.xul files.
+
100 characters or less.
 +
There is no exception is <code>*.js</code> files! In some cases this rule can be broken in <code>*.html</code> or <code>*.xul</code> files. But keep in mind long lines are hard to read (also search results are hared to read).
=== Indentation ===  
=== Indentation ===  

Revision as of 11:47, 26 April 2011

This document attempts to explain the basic styles and patterns, that are used in 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!

Contents

Resources


Formatting Code

Whitespace

No tabs. No whitespace at the end of a line.

Line Length

100 characters or less. There is no exception is *.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 hared to read).

Indentation

Four spaces per logic level.

Licence

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

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

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

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

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

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


Control Structures

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.

var foo = { prop1: "value1" };

var bar = {
    prop1: "value1",
    prop2: "value2",
};

Anyway, class and functions definitions should always have the braces on the next line as follows:

Firebug.MyModule = extend(Firebug.Module,
{
    initializeUI: function()
    {
    },
});
function myFunction()
{
    // ....
}

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

if (...)
{
}
else if (...)
{
}

Another example showing how to deal with spaces:

if ((a > 0) && (b > 0))
{
}
switch (...)
{
    case 1:
    {
    }
}
for (var i=0; i<10; i++)
{
}
try
{
}
catch (err)
{
}

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:

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

Horizontal Lines

Sometimes is helpful to divide portions of a file by a horizontal line. For this, you should use following comment (100 characters long):

// ********************************************************************************************* //

Firebug codebase also uses 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).

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //


Naming

Functions and Methods

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

function foo()
{
}
function myFoo()
{
}

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 be also used in browser environment where const is not supported.


Variables

Variable should use camelCase and not capitalize the first letter.

var thisIsMyVariable = true;


Prefixes

Firebug codebase doesn't use any prefixes for member fields.

Good Practices

Vertical Indentation

Method defintions should be separated by a new line. Note the new line between initialize and shutdown methods.

Firebug.MyModule = extend(Firebug.Module,
{
    initialize: function()
    {
    },

    shutdown: function()
    {
    }
});

Also portions of code logically belonging together should be separated by a new line from other code. Note the new line between super.initialize and this.onMutateText.

initialize: function()
{
    super.initialize.apply(this, arguments);

    this.onMutateText = bind(this.onMutateText, this);
    this.onMutateAttr = bind(this.onMutateAttr, this);
    this.onMutateNode = bind(this.onMutateNode, this);
}

Horizontal lines should be surrounded by new lines too

function myFunc1()
{
}

// ********************************************************************************************* //

function myFunc2()
{
}

Example File

Example of a typical Firebug file implementing a module object.

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

FBL.ns(function() { with (FBL) {

// ********************************************************************************************* //
// Constants

var MY_CONSTANT = true;

// ********************************************************************************************* //
// Module Implementation

Firebug.MyModule = extend(Firebug.Module,
{
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Initialization

    initializeUI: function()
    {
    },

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Toolbar Actions

    myButtonHandler: function()
    {
    }
});

// ********************************************************************************************* //
// Registration

Firebug.registerModule(Firebug.StartButton);

// ********************************************************************************************* //
}});

The code should also have comments.

Personal tools