62 ms·
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 inconsis
by TNO 17y ago
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;
"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.
- maigret 17y agoAbout the 80 lines width: yes, and it is much more important now than never (in my opinion). Have a look at most news websites, or at a normal book, or a newspaper, or LaTeX (and even Word!) standard text-width, it's all fitting in around that size because it is very efficient to read. Unlimited width for text is a UI anti-pattern, as the eye gets more and more pain to find the next line. 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.
- isleyaardvark 17y agoTypographers have known this for quite some time. It's called "measure" and it should be around 65-75 characters (according to the typographer's bible, The Elements of Typographical Style by Binghurst: http://en.wikipedia.org/wiki/The_Elements_of_Typographic_Style http://en.wikipedia.org/wiki/The_Elements_of_Typographic_Sty...)
- mdg 17y agoIn my ~/.vimrc: set textwidth=80
- isleyaardvark 17y agoFew of the examples you listed are arbitrary, in fact Crockford describes precisely why he chose them as conventions. In nearly all cases he came up with these conventions to help coders avoid common pitfalls due to unfortunate aspects of the JavaScript language. 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)?
- unwind 17y agoFrom what I can tell about Python, for example, adding underbars enforces privacy, JavaScript doesn't. That's not true, an initial underscore is simply used as the convention to indicate that something is private: >>> class Foo(object): ... def __init__(self): ... self._bar = 48 ... >>> a=Foo() >>> a._bar 48
- sid0 17y ago> Is the 80 col limit still valid in the modern day with such large screen displays? Yes. With my previous notebook screen (1440x900) I could fit two 83-character-wide Emacs buffers side by side. With my current screen (1600x900) I can fit two 94-character-wide Emacs buffers side by side.