Last active
December 20, 2015 05:29
-
-
Save thomasfr/6079078 to your computer and use it in GitHub Desktop.
Coding Guidelines / Coding Styles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrong: | |
if (condition) alert(foo); | |
// Correct: | |
if (condition) { | |
alert(foo); | |
} | |
//------------------------------------------------- | |
// Wrong: | |
var foo = NULL; | |
// Correct: | |
var foo = null; | |
//------------------------------------------------- | |
// Wrong: | |
if (condition == null) { | |
} | |
// Correct: | |
if (null === condition) { | |
} | |
//------------------------------------------------- | |
// Wrong: (if you compare with true, false, null or undefined) | |
if (condition === true) { | |
} | |
// Correct: | |
if (true === condition) { | |
} | |
//------------------------------------------------- | |
// Wrong: | |
if (condition) { alert(foo); } | |
// Correct: | |
if (condition) { | |
alert(foo); | |
} | |
//------------------------------------------------- | |
// Wrong: | |
if(brochure.merchant.isActive() && brochure.getActiveDate() > dateFrom && brochure.getActiveDate() < dateTo && forceShowAll) { | |
} | |
// Wrong: | |
if(brochure.merchant.isActive() | |
&& brochure.getActiveDate() > dateFrom | |
&& brochure.getActiveDate() < dateTo | |
&& forceShowAll) { | |
} | |
// Correct: | |
var isMerchantActive = brochure.merchant.isActive(); | |
var isWithinDateRange = brochure.getActiveDate() > dateFrom && brochure.getActiveDate() < dateTo; | |
if (isMerchantActive && isWithinDateRange && forceShowAll) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrong: | |
function GA(config) { | |
} | |
// Correct: | |
var GA = function(config) { | |
}; // <-- do not forget the semicolon or tiny kittens and a giraffe have to die |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = "This is a Hello World string"; | |
// Wrong: | |
var element = foo.trim() | |
.split(' ') | |
.pop(); | |
// Correct: | |
var fooElements = foo.trim().split(' '); | |
var element = fooElements.pop(); | |
// also correct: | |
var foo = foo.trim(); | |
var fooElements = foo.split(' '); | |
var element = fooElements.pop(); | |
// also correct (if amount of chained functions is not too much. Decide on your own when to split it up) | |
var element = foo.trim().split(' ').pop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrong: | |
var foo = function() { | |
var me = this; | |
var bar = function() { | |
me.doThat(); | |
}; | |
}; | |
// Correct: | |
var foo = function() { | |
var that = this; // If you have to save to use the scope in another scope use the name 'that' for it. | |
var bar = function() { | |
that.doThat(); | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrong: | |
var foo = "bar" | |
// Correct: | |
var foo = "bar"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrong: | |
var foo = "foo", | |
bar = "bar", | |
hello = [true, false]; | |
// Wrong: | |
var foo = "foo" | |
, bar = "bar" | |
, hello = [true, false]; | |
// Correct: | |
var foo = "foo"; | |
var bar = "bar"; | |
var hello = [true, false]; | |
//------------------------------------------------- | |
// Wrong: | |
var elements = new Array('foo', 'bar'); | |
// Correct: | |
var elements = ['foo', 'bar']; | |
//------------------------------------------------- | |
// Wrong: | |
var map = {}; | |
map.foo = "foo"; | |
map.bar = "bar"; | |
// Wrong: | |
var map = { | |
foo: "foo" | |
, bar: "bar" | |
, baz: "baz" | |
}; | |
// Correct: | |
var map = { | |
foo: "foo", | |
bar: "bar" | |
}; // <-- do not forget the semicolon or tiny little kittens have to die | |
//------------------------------------------------- | |
// Wrong: | |
var Long_Variablename; | |
var long_variable_name; | |
var longvariablename; | |
// Correct: | |
var longVariableName; | |
//------------------------------------------------- | |
// Wrong: | |
var myClass = function() { | |
}; | |
// Correct: | |
var MyClass = function() { | |
}; | |
//------------------------------------------------- | |
// Wrong: | |
var myConstant = 10; | |
// Correct: (If you want to express that a variable is a constant use this syntax) | |
var MY_CONSTANT = 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrong: | |
var text = true; | |
// Correct: | |
var hasText = true; | |
// Wrong: | |
var option = false; | |
// Correct: | |
var hasOption = false; | |
// Wrong: | |
var merchant = true; | |
// Correct: | |
var isMerchant = true; | |
//------------------------------------------------- | |
// Wrong: | |
var isNotProcessed = true; | |
// Wrong: | |
var notProcessed = true; | |
// Correct: | |
var isProcessed = false; | |
//------------------------------------------------- | |
// Use expressive names for all variables | |
// Wrong: | |
var err = new Error("Some error text"); | |
// Correct: | |
var error = new Error("Some error text"); | |
// Wrong: | |
var handler = function(req, res) { | |
}; | |
// Correct: | |
var requestHandler = function(request, response) { | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment