第1页
The Better Parts
第2页
Antoine de Saint-Exupéry
第4页
Il semble que la perfection soit atteinte non quand il n’y a plus rien à ajouter, mais quand il n’y a plus rien à retrancher.
Antoine de Saint-Exupéry Terre des Hommes, 1939
It seems that perfection is attained not when there is nothing more to add, but when there is nothing more to subtract.
第5页
Good Parts
It seems that perfection is attained not when there is nothing more to add, but when there is nothing more to subtract.
第6页
If a feature is sometimes useful and sometimes dangerous
and if there is a better option then
always use the better option.
第7页
We are not paid to use every feature of the language.
We are paid to write programs that work well
and are free of error.
第8页
We are not paid to use every feature of the language.
We are paid to write programs that work well
and are free of error.
第9页
A good programming language should teach you.
第10页
I made every mistake with JavaScript you could make.
第13页
Arguments against good parts
• Just a matter of opinion. • Every feature is an essential tool. • I have a right to use every feature. • I need the freedom to express myself. • I need to reduce my keystrokes. • It is an insult to suggest that I would ever
make a mistake with a dangerous feature. • There is a good reason those features were
added to the language.
第14页
“Foot Guns”
Brendan Eich
第15页
The purpose of a programming language is to aid programmers in producing
error-free programs.
第16页
It is not possible to write good programs in JavaScript.
第17页
It is not only possible to write good programs in JavaScript,
it is necessary.
It is not possible to write good programs in JavaScript.
第18页
The fantasy of infallibility. The futility of faultlessness.
第19页
Danger Driven Development
第20页
;
第21页
A.The time it takes to write the code. B.The time it takes to make the code
work right.
Always take the time to code well.
第22页
New Good Parts in ES6
• Proper tail calls: return func();
第23页
New Good Parts in ES6
• Proper tail calls
• Splat...aka ...rest aka ...spread
function curry(func, ...first) { return function (...second) { return func(...first, ...second); };
}
function curry(func) { var slice = Array.prototype.slice, args = slice.call(arguments, 1); return function () { return func.apply(null, args.concat(slice.call(arguments, 0))); };
}
第24页
New Good Parts in ES6
• Proper tail calls • Splat • Module
第25页
New Good Parts in ES6
• Proper tail calls • Splat • Module • Let
let is the new var
第26页
New Good Parts in ES6
• Proper tail calls • Splat • Module • Let • Destructuring let {that, other} = some_object;
let that = some_object.that, other = some_object.other;
第27页
New Good Parts in ES6
• Proper tail calls • Splat • Module • Let • Destructuring • WeakMap
第28页
Good Parts Reconsidered
• I stopped using new years ago. Use Object.create instead.
第29页
Good Parts Reconsidered
• I stopped using new years ago. • I have stopped using Object.create.
第30页
Good Parts Reconsidered
• I stopped using new years ago. • I have stopped using Object.create. • I have stopped using this. [ADsafe.org]
第31页
Loops Reconsidered
• I don’t use for. I now use array.forEach and its many brothers.
• I don’t use for in. I now use Object.keys(object).forEach.
• ES6 will have proper tail calls. At that point I will stop using while.
第32页
function repeat(func) { while (func() !== undefined) { }
}
function repeat(func) { if (func() !== undefined) { return repeat(func); }
}
第33页
The Next Language
第34页
Programmers are as emotional and irrational
as normal people.
第35页
It took a generation to agree that high level languages were a good
idea.
It took a generation to agree that goto was a bad idea.
It took a generation to agree that objects were a good idea.
It took two generations to agree that lambdas were a good idea.
第36页
Systems languages
Application languages
第37页
Classical Inheritance
Prototypal Inheritance
第38页
Classi cation Taxonomy
第39页
Prototypal Inheritance
• Memory conservation. • May have made sense in 1995. • Confusion: Own vs inherited. • Retroactive heredity. • Performance inhibiting.
第40页
Prototypal Inheritance
Class Free
第41页
Block Scope
{ let a; { let b; …a… …b… } …a…
}
第42页
Function Scope
function green() { let a; function yellow() { let b; …a… …b… } …a…
}
第43页
Function Scope
function green() {
let a;
function yellow() {
let b;
…a…
…b… } …a…
a
}
第44页
Function Scope
function green() {
let a;
function yellow() {
let b;
…a…
…b… } …a…
a
}
b
第45页
Inner survives the outer
function green() { let a; return function yellow() { let b; …a… …b… }; …a…
}
第46页
function constructor(spec) { let {member} = spec;
}
第47页
function constructor(spec) { let {member} = spec, {other} = other_constructor(spec);
}
第48页
function constructor(spec) { let {member} = spec, {other} = other_constructor(spec), method = function () { // member, other, method };
}
第49页
function constructor(spec) { let {member} = spec, {other} = other_constructor(spec), method = function () { // member, other, method }; return Object.freeze({ method, other });
}
第50页
A Bug Story
private int index;
第51页
int
• Overflow errors
• Complement: saving gates in subtraction
• Memory used to be really expensive and scarce
第52页
Number types
Java: byte char short int long float double
Wastes the programmer’s time by having to select the right type. Errors result from choosing the right type. No real benefit.
JavaScript: number (same as double)
Having only one type is a huge improvement. Unfortunately, it is the wrong type.
第53页
Number types
Java: byte char short int long float double
Wastes the programmer’s time by having to select the right type. Errors result from choosing the right type. No real benefit.
JavaScript: number (same as double)
Having only one type is a huge improvement. Unfortunately, it is the wrong type.
第54页
0.1 + 0.2 == 0.3
false
第55页
Binary Floating Point
Made a lot of sense in the 1950s. Scientific | Business
第56页
DEC64
Coefficient
Exponent
Number = Coefficient * 10Exponent
第57页
dec64.com
Coefficient
Exponent
https://github.com/douglascrockford/DEC64/
第58页
The JSON Data Interchange Format
第59页
JSON,XML
第60页
Advice to data format standard designers
• Don’t break JSON. • Make it signi cantly better. • Get a better name.
第61页
JSON
JavaScript Object Notation
第62页
Responsibility
1. The People 2. The Team 3. Management
第63页
And finally, one piece of advice:
Don’t make bugs.
第64页
Thank you and good night.