-
Notifications
You must be signed in to change notification settings - Fork 62
Code Style Guide
maximecb edited this page Nov 17, 2014
·
14 revisions
Overview
These are the basic style guidelines for Higgs JavaScript code. If you are submitting a pull request please try to follow them as closely as possible.
You may want to run your code through jshint before submitting it. This will catch some (but not all) style violations, as well as possible errors. An appropriate .jshintrc
file is in the repo - if you use the command line jshint
tool, it will pick it up automatically or you can copy the settings into the online tool.
Notes:
- These guidelines only apply to Higgs JavaScript; the repository contains some 3rd party benchmarks/tests which use different styles/conventions.
- If you are modifying/reading a Higgs JavaScript file and there are style violations (or jshint errors), please add to or open an issue for that file.
Whitespace
- Four space indent/tab-width.
- Indent/align with spaces, no tab characters
- End files with one blank line.
- Round braces are snug:
// Like so:
if (foobar)
{
}
foo(bar, baz);
// Not like:
if ( foobar )
{
}
foo( bar, baz );
Curly Braces
- Go on their own line:
function foo()
{
return 'bar';
}
if (foobar)
{
}
while (foobar)
{
}
- May be omitted in 'if', 'while', etc if the body is only one line:
// Note: body goes on it's own line.
// Like so:
if (foo)
bar();
// Not like:
if (foo) bar();
// Note: Lack/presence of braces should be consistent within a given statement.
// Like so:
if (foo)
{
bar();
baz();
}
else
{
quux();
}
// Not like:
if (foo)
{
bar();
baz();
}
else
quux();
Semicolons
- After any expression, return statement, or var statement:
var a = b;
if (foo)
{
}
while (foo)
{
}
function baz()
{
return 42;
}
var foo = function()
{
return 42;
};
Strings
- Should use
''
, not""
:
// Like so:
var my_str = 'Hello World!';
var sentence = '"Woof" the dog barked';
var err = 'Unknown command \'foo\'';
// Not like:
var my_str = "Hello World!";
Commas
- Go at the end of the line:
var foo = {
bar : 'bar',
baz : 'baz'
};
Modules
- Prepended with the Higgs license header (TODO: link).
- Enclosed in an IIFE (all files are evaluated in the same scope):
(function(exports)
{
exports.myVar = 808;
exports.myFun = function()
{
}
})(exports);
Other
Please have a look at some of the Higgs source code to get a general impression of the style/conventions being used.