This will have, may be, has been the official JavaScript style guide for developers of Symphony CMS.
Spaces are evil. Always use 4–space wide tabs while developing for Symphony CMS. Yes, I realise that the examples on this page use 4–space indentation and not tabs. Do what I say!
Apparently, some people don’t like using semicolons to terminate statements. Failing to do so may get you flayed.
var value = 'example';
var value = 'example'
Just like you brush your teeth after every meal, clean up trailing whitespace in your JavaScript files before committing. Otherwise the rotten smell of careless neglect will eventually drive away contributors and/or co–workers.
Most reasonable text editors (Textmate and Sublime Text to name a couple) allow intelligent word-wrapping inside of your window. We suggest you use this instead of manually wrapping your lines to a fixed number of characters.
If you really must manually wrap some text, then do it to the standard 80 characters.
Use single quotes, unless you are writing JSON.
var value = 'example';
var value = "example";
Your opening braces go on the same line as the statement. Also, don’t be afraid to use whitespace.
if (true) { console.log('great'); }
if (false) { console.log('never'); }
Don’t overuse var
statements, always indent each variable on its own line.
var foo = 1, bar = 'two';
var foo = 1; var bar = 'two';
var foo = 1, var bar = 'two';
Variables and properties should use lower camel case capitalization. They should also be descriptive. Single-character variables and uncommon abbreviations should generally be avoided (may get you shot).
var translatedString = Symphony.Language.Dictionary[string];
var translated_string = Symphony.Language.Dictionary[string];
var ts = Symphony.Language.Dictionary[string];
Class names should be capitalized using upper camel case.
function YourClass() { ... }
function your_class() { ... }
Constants should be declared as regular variables or static class properties, using all uppercase letters.
var SECOND = 1 * 1000;
var second = 1 * 1000;
Use trailing commas and put short declarations on a single line. Only quote keys when your interpreter complains:
var a = ['hello', 'world']; var b = { good: 'code', 'is generally': 'pretty', };
var a = [ 'hello', 'world' ]; var b = {"good": 'code' , is generally: 'pretty' };
Any non–trivial conditions should be assigned to a descriptive variable:
var isAuthorized = (user.isAdmin() || user.isModerator()); if (isAuthorized) { console.log('winning'); }
if (user.isAdmin() || user.isModerator()) { console.log('losing'); }
Keep your functions short. A good function fits on a slide that the people in the last row of a big room can comfortably read. So don’t count on them having perfect vision and limit yourself to ~10 lines of code per function.
To avoid deep nesting of if–statements, always return a functions value as early as possible.
function isPercentage(val) { if (val < 0) { return false; } if (val > 100) { return false; } return true; }
function isPercentage(val) { if (val >= 0) { if (val < 100) { return true; } else { return false; } } else { return false; } }
Or for this particular example it may also be fine to shorten things even further:
function isPercentage(val) { var isInRange = (val >= 0 && val <= 100); return isInRange; }
Your else and else if statements should find themselves on a new line, and not on the same line as a brace.
// We don't care: if (val < 100) { return false; } // Value is in range: else { return true; }
// We don't care: if (val < 100) { return false; } else { // Value is in range: return true; }
Feel free to give your closures a name. It shows that you care about them, and will produce better stack traces:
req.on('end', function onEnd() { console.log('winning'); });
req.on('end', function() { console.log('failing'); });