Code Conventions for the JavaScript Programming Language
javascript.crockford.com
Code Conventions for the JavaScript Programming Language
1–10 of 31 posts
Re: Code Conventions for the JavaScript Programming Language
#2I would rather replace the whole article with "Write readable code". Following these conventions are neither sufficient or necessary to accomplish that.
Re: Code Conventions for the JavaScript Programming Language
#3 "Avoid lines longer than 80 characters."
This one really bugs me. 80 characters doesn't fit a whole lot of stuff. In a case where a line is more than 80 chars long I would rather see this: document.getElementsByClassName("externalLinkBig")[0].setAttribute("class", "externalLinkSmall");
than this rather ugly solution: document.getElementsByClassName("externalLinkBig")[0].setAttribute("class",
"externalLinkSmall");Re: Code Conventions for the JavaScript Programming Language
#4Is 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;
"All functions should be declared before they are used"Yes, but not in the way that JSLint enforces it. See above.
"Inner functions should follow the var statement."
umm, no? This has a different meaning from a function declaration.
"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.
"If privacy is important, use the forms that provide private members. Avoid conventions that demonstrate a lack of competence."
Whatever, show me an efficient, non-convoluted pattern that allows inheritance of privileged members. Avoid conventions that demonstrate a lack of concern for memory consumption and efficiency.
"Global variables should be in all caps."
umm, no. That convention is reserved for 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.
"Avoid use of the continue statement. It tends to obscure the control flow of the function. "
It's ok to use labels, but not continue? I don't understand this logic.
"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.
Re: Code Conventions for the JavaScript Programming Language
#5"Avoid lines longer than 80 characters." This one really bugs me. 80 characters doesn't fit a whole lot of stuff. In a case where a line is more than 80 chars long I would rather see this: document.getElementsByClassName("externalLinkBig")[0].setAttribute("class", "externalLinkSmall"); than this rather ugly solution: document.getElementsByClassName("externalLinkBig")[0].setAttribute("class", "externalLinkSmall");
document
.getElementsByClassName("externalLinkBig")[0]
.setAttribute("class", "externalLinkSmall");Re: Code Conventions for the JavaScript Programming Language
#6"Avoid lines longer than 80 characters." This one really bugs me. 80 characters doesn't fit a whole lot of stuff. In a case where a line is more than 80 chars long I would rather see this: document.getElementsByClassName("externalLinkBig")[0].setAttribute("class", "externalLinkSmall"); than this rather ugly solution: document.getElementsByClassName("externalLinkBig")[0].setAttribute("class", "externalLinkSmall");
document
.getElementsByClassName( 'externalLinkBig' )[0]
.setAttribute( 'class', 'externalLinkSmall' );
80 columns is definitely my preferred (but not "I'll complain") line length because I don't have my editor maximized (cringe) and long lines aren't as easy to read (especially when there's several long lines in a row, it's hard to keep them straight near the far right).Re: Code Conventions for the JavaScript Programming Language
#7"Avoid lines longer than 80 characters." This one really bugs me. 80 characters doesn't fit a whole lot of stuff. In a case where a line is more than 80 chars long I would rather see this: document.getElementsByClassName("externalLinkBig")[0].setAttribute("class", "externalLinkSmall"); than this rather ugly solution: document.getElementsByClassName("externalLinkBig")[0].setAttribute("class", "externalLinkSmall");
'onNextTimespanClick': function(e, el) {
return this
.onNavigationClickStopEvent(e, el)
.scrollToTop()
.animateNavChange(
this.getSelectedNavigationEl(),
$$(
'div.navigation a[href='
+ this.getHashFromUrl(el.href)
+ ']',
this.navigationContainer
)
)
.refreshItems();
},Re: Code Conventions for the JavaScript Programming Language
#8But one difference I noticed was the use of "cuddled" else in Crockford's list:
} else {
instead of PBP's: }
else {
Conway believes this is more readable and inline with K&R style (used in both conventions/practises).Re: Code Conventions for the JavaScript Programming Language
#9Some 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; "…
A great example of site respecting the 80 chars rules is Paul Graham's essays site, reading it is a pure pleasure also because of the fine presentation.
Re: Code Conventions for the JavaScript Programming Language
#10Some 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; "…
set textwidth=80