Slide 1

Slide 1 text

WRITE PERFECT CODE with Standard and ESLint

Slide 2

Slide 2 text

npm install standard --save-dev

Slide 3

Slide 3 text

THE END

Slide 4

Slide 4 text

WRITE PERFECT CODE with Standard and ESLint

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

What's a linter?

Slide 7

Slide 7 text

A LINTER CATCHES BUGS

Slide 8

Slide 8 text

What's a bug?

Slide 9

Slide 9 text

WHEN YOUR CODE DOESN'T WORK

Slide 10

Slide 10 text

“BUG”

Slide 11

Slide 11 text

BUGBEAR BUGABOO BOOGEYMAN

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

We could, for instance, begin with cleaning up our language by no longer calling a bug a bug but by calling it an error. — Edsger W. Dijkstra

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

ESLINT Catches programmer errors

Slide 17

Slide 17 text

JSLINT ➡ JSHINT ➡ ESLINT

Slide 18

Slide 18 text

PROGRAMMER ERRORS BEST PRACTICES STYLE ISSUES

Slide 19

Slide 19 text

CATCH PROGRAMMER ERRORS Shorten the Feedback Loop

Slide 20

Slide 20 text

QUIZ: DO YOU SEE THE BUG IN THIS CODE? for (let i = 0; i < 10; i--) { console.log(i) }

Slide 21

Slide 21 text

ENFORCE “FOR” LOOP UPDATE CLAUSE MOVING THE COUNTER IN THE RIGHT DIRECTION for (let i = 0; i < 10; i--) { // This loop never ends! } for (let i = 10; i >= 0; i++) { // This loop never ends! }

Slide 22

Slide 22 text

DISALLOW ASSIGNMENT OPERATORS IN CONDITIONAL STATEMENTS if (user.jobTitle = 'manager') { // user.jobTitle is now incorrect }

Slide 23

Slide 23 text

DISALLOW DUPLICATE ARGUMENTS IN FUNCTION DEFINITIONS function foo(a, b, a) { console.log('value of the second a:', a) }

Slide 24

Slide 24 text

DISALLOW COMPARING TYPEOF EXPRESSIONS AGAINST INVALID STRINGS typeof foo === 'strnig' typeof foo === 'undefimed' typeof bar !== 'nunber' typeof bar !== 'fucntion'

Slide 25

Slide 25 text

DISALLOW INVALID REGULAR EXPRESSION STRINGS IN REGEXP CONSTRUCTORS RegExp('[') RegExp('.', 'z') new RegExp('\\')

Slide 26

Slide 26 text

Plus dozens more error checks

Slide 27

Slide 27 text

How does one use ESLint?

Slide 28

Slide 28 text

.ESLINTRC

Slide 29

Slide 29 text

.ESLINTRC { "extends": ["standard"] } npm install eslint-config-standard --save-dev

Slide 30

Slide 30 text

STANDARD VS. AIRBNB VS. GOOGLE

Slide 31

Slide 31 text

.ESLINTRC { "extends": ["standard"], "rules": [ "quotes": ["error", "double"], "rest-spread-spacing": ["error", "always"], "semi": ["error", "always"] ] }

Slide 32

Slide 32 text

WHY USE STANDARD?

Slide 33

Slide 33 text

USE STANDARD $ npm install standard --save-dev $ standard Error: Use JavaScript Standard Style main.js:950:11: Expected '===' and instead saw '=='.

Slide 34

Slide 34 text

VIBRANT COMMUNITY DOWNLOADED 1 MILLION TIMES PER MONTH EDITOR PLUGINS FOR EVERY MAJOR EDITOR BUILT INTO WEBSTORM

Slide 35

Slide 35 text

PACKAGE.JSON { "name": "my-cool-package", "devDependencies": { "standard": "*" }, "scripts": { "test": "standard && node test.js" } }

Slide 36

Slide 36 text

Should one use ESLint directly?

Slide 37

Slide 37 text

BEWARE OF BIKESHEDDING

Slide 38

Slide 38 text

BEWARE OF DUPLICATION

Slide 39

Slide 39 text

PROGRAMMER ERRORS BEST PRACTICES STYLE ISSUES

Slide 40

Slide 40 text

PROGRAMMER ERRORS BEST PRACTICES STYLE ISSUES

Slide 41

Slide 41 text

QUIZ: WHAT DO THESE EXPRESSIONS EVALUATE TO? [] == false [] == ![] 3 == '03'

Slide 42

Slide 42 text

QUIZ: WHAT DO THESE EXPRESSIONS EVALUATE TO? [] == false // true [] == ![] // true 3 == '03' // true

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

REQUIRE === AND !== // NOT OK if (x == 42) { } // OK if (x === 42) { }

Slide 45

Slide 45 text

STANDARD IS PRAGMATIC // OK if (x == null) { } // THE ABOVE IS EQUIVALENT TO... if (x === null || x === undefined) { }

Slide 46

Slide 46 text

QUIZ: DO YOU SEE THE PROGRAMMER ERROR? hashOut.data = hashes + SSL_MD5_DIGEST_LEN; hashOut.length = SSL_SHA1_DIGEST_LEN; if ((err = SSLFreeBuffer(&hashCtx)) != 0) goto fail; if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0) goto fail; if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail; if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0) goto fail; err = sslRawVerify(...);

Slide 47

Slide 47 text

if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) goto fail; goto fail;

Slide 48

Slide 48 text

if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) { goto fail; } goto fail;

Slide 49

Slide 49 text

REQUIRE CURLY BRACE CONVENTIONS // NOT OK while (bar) baz() // NOT OK if (foo) { baz() } else qux()

Slide 50

Slide 50 text

STANDARD IS PRAGMATIC // OK readFile(file, (err, data) => { if (err) throw err }) // OK readFile(file, (err, data) => { if (err) { throw err } })

Slide 51

Slide 51 text

FINAL TIPS

Slide 52

Slide 52 text

EVEN IF YOU DON'T USE STANDARD CONSIDER standard-engine

Slide 53

Slide 53 text

WHAT ABOUT PRETTIER?

Slide 54

Slide 54 text

PROGRAMMER ERRORS BEST PRACTICES STYLE ISSUES

Slide 55

Slide 55 text

PROGRAMMER ERRORS BEST PRACTICES STYLE ISSUES

Slide 56

Slide 56 text

Support me on Patreon PATREON.COM/FEROSS

Slide 57

Slide 57 text

Thanks! FEROSS.ORG