Skip to content

Instantly share code, notes, and snippets.

@erichocean
Created November 7, 2008 18:43
Show Gist options
  • Save erichocean/22937 to your computer and use it in GitHub Desktop.
Save erichocean/22937 to your computer and use it in GitHub Desktop.
//
// indentation should be two spaces, not tabs
//
// THIS (soft-tabs, two spaces):
var p = 12;
// NOT THIS (hard tabs):
var p = 12; // wrong: uses a tab, not two spaces
//
// binary return statements
//
// DO THIS:
return (doc) ? doc.getWidth() : 600 ; // note space after '600' and before semi-colon
// NOT THIS:
if(doc){
return doc.getWidth();
}
else{
return 600;
}
//
// returns statements within ifs should be consistent
//
// DO THIS:
function() {
var yn = getBooleanFromSomewhere();
if (yn) {
// do some work
return 'foo';
}
else return 'bar';
}
// NOT THIS:
function() {
var yn = getBooleanFromSomewhere();
if (yn) {
// do some work
return 'foo';
}
return 'bar'; // return statment is logically 'else' but isn't on an else branch
}
//
// if statement condition and brace spacing
//
// DO THIS:
if (d) {
// more than one line of code...
}
// NOT THIS:
if(d){
// more than one line of code...
}
//
// single-statement ifs should be one-liners with no braces
//
// DO THIS:
if (x) this.someAction();
else this.someOtherAction();
// ALSO OKAY:
if (x) { this.someAction(); }
else { this.someOtherAction(); }
// BUT NOT THIS (too verbose):
if (x) {
this.someAction();
}
else {
this.someOtherAction();
}
// AND NEVER THIS (error prone):
if (x)
this.someAction();
else
this.someOtherAction();
//
// multi-statement if statements should have braces
//
// DO THIS:
if (x) {
this.someAction();
this.someOtherAction();
}
else {
this.someAction2();
this.someOtherAction2();
}
// ALSO OKAY:
if (x) {
this.someAction();
this.someOtherAction();
} else {
this.someAction2();
this.someOtherAction2();
}
//
// assignments
//
// DO THIS:
var doc = d.contentDocument();
// NOT THIS:
var doc=d.contentDocument();
//
// private methods
//
// OKAY:
this._somePrivateMethod();
// NEVER OKAY:
var d = getAnObject();
d._somePrivateMethod(); // private methods should only be callable against 'this'
//
// private properties
//
// OKAY:
var p = this._somePrivateProperty;
// NEVER OKAY:
var d = getAnObject();
var p = d._somePrivateProperty; // private properies should only be accessible against 'this'
//
// hashes (object literals)
//
// DO THIS:
{
identifier: function() {
},
otherIndentfier: 'variable',
anotherIndentfier: ['arrays', 'are', 'okay'],
finalIdentifier: 12 // no "comma" after the last hash member
}
// NOT THIS:
{
identifier:function(){ // wrong: should be spaces between :f and ){
},
otherIndentfier: 'variable', // wrong: should be a blank line after between previous hash member
// ^^ wrong: should not be extra lines between hash members
anotherIndentfier: ['arrays','are','okay'], // wrong: array has bad spacing
// // <-- don't put random empty comments between hash members either
finalIdentifier: 12, // wrong: there should not be "comma" after the last identifier
// (wrong: there should be no blank lines after the final hash member)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment