Some are good, some arbitrary: Is the 80 col limit still valid in the modern day with such large screen displays? . His line indentation convention is inconsistent. In one place he says to use 8 spaces, in another he says to use 4. Then when you look at a switch, its 0. . variable declarations: If he's referring to the way that JSLint enforces it, that's stupid. ex: function foo(){ myGlobal = "foo"; } var myGlobal; "…
function foo(){
myGlobal = "foo";
}
var myGlobal;
This is clear if you only have this one function, but with a large js file I know I'd rather not be searching through the code for global variables. That's also why "Inner functions should follow the var statement", it's an easy way to keep track of scope."Do not use _ (underbar) as the first character of a name. It is sometimes used to indicate privacy, but it does not actually provide privacy. " It's private because you weren't invited to use it, not because I have a bomb waiting for you.
No, it's private because it throws an error if you try to use it outside of the proper scope. From what I can tell about Python, for example, adding underbars enforces privacy, JavaScript doesn't.
"Global variables should be in all caps." umm, no. That convention is reserved for constants.
That convention in other languages is for constants. Crockford points out that js doesn't have constants. He also constantly points out the dangers of global variables, how you should minimize their use. The purpose of all caps is to give a visual cue that they're global, the purpose of all caps in other languages is to give a visual cue that they're constants.
"Each group of statements (except the default) should end with break, return, or throw. Do not fall through." Arbitrary. I think DRY is preferable when it's warranted.
Crockford used to think the same. He tells the story of a user who suggested fall-through should be flagged by JSLint. Crockford gave him a detailed response explaining that there was nothing wrong with fall-through. The user agreed, and included a bug report for JSLint in his response. Crockford found the bug in JSLint and it was caused by... fall-through.
"Avoid doing assignments in the condition part of if and while statements. " It's a legitimate convention if wrapped with an extra pair of parenthesis. Note that Mozilla's strict mode will honor this convention.
Crockford points out the problems you can run into doing that:
Is
if (a = b) {
a correct statement? Or was if (a == b) {
intended?It's easy enough to tell if you're the one who wrote it, but what happens to the poor sorry schlub who's trying to debug the code later on (especially if that schlub is you 6 months later)?