Skip to content

Instantly share code, notes, and snippets.

@Ricket
Created September 9, 2011 02:53
Show Gist options
  • Save Ricket/1205383 to your computer and use it in GitHub Desktop.
Save Ricket/1205383 to your computer and use it in GitHub Desktop.
My notes from Douglas Crockford's speech "JavaScript Programming Style & Your Brain"
Douglas Crockford
Sr. JavaScript Architect, Yahoo!
JavaScript Programming Style & Your Brain
He made JSLint
Prefer forms that are error resistant and more reliable
return
{
something;
}
^ this is an error in JavaScript; put your curly braces on the same line ALWAYS
switch statements (fallthrough hazard)
"That hardly ever happens" == "It happens"
Good use of style can help reduce the occurrence of errors.
For English: William Strunk - The Elements of Style
Programs must communicate clearly to people.
Use elements of good composition where applicable.
Use spaces to disambiguate:
No space before ( when used to invoke a function
No space between a function name and parameter list
One space between all other names and (
Avoid the JavaScript 'with' statement because it has ambiguous situations.
Always use ===, never ==; it doesn't do type conversion.
Multiline string literals are bad, they break indentation and have a syntax error if there's a space after the backslash
if (a = b) {...}
^ the reader will almost always assume this is a bug; if you meant to assign b to a and then test a, then write that (in two lines).
Scope - block scope vs function scope.
JavaScript has function scope
Declare all variables at the top of the function (not top of if/for/other block).
Declare all functions before you call them.
for(var i ...) - i is not scoped to the loop
Avoid ++, use += 1
Only one more character
In javascript, x++ is actually ++x
Multiple ++'s consecutively look like an error
var a = b = 0;
Makes a global variable b, sets it to 0, and sets a to value of b.
NOT the same as `var a=0, b=0;`
Write in a way that clearly communicates your intent.
Always put curly braces on if statements.
As our processes become more agile, our coding must be more resilient [to bugs].
Language subsetting.
Only a madman would use all of C++.
Good style is good for your gut.
@panlovecat
Copy link

That's useful note,Thanks:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment