Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

How to Write Perfect Code with Standard and ESLint

How to Write Perfect Code with Standard and ESLint

In this talk, you'll learn about code linting – how to use Standard and ESLint to catch programmer errors before they cause problems for your users. We'll discuss how to get started with linting, as well as how to improve your setup if you're already linting your code. Feross will also share the secret history of how the Standard linter was created, for the first time ever in a conference talk.

Presented at JSConf Asia 2018 (https://2018.jsconf.asia) by Feross Aboukhadijeh (https://feross.org)

Read more about Standard at https://standardjs.com/

Feross Aboukhadijeh

January 25, 2018
Tweet

More Decks by Feross Aboukhadijeh

Other Decks in Technology

Transcript

  1. 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
  2. QUIZ: DO YOU SEE THE BUG IN THIS CODE? for

    (let i = 0; i < 10; i--) { console.log(i) }
  3. 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! }
  4. DISALLOW COMPARING TYPEOF EXPRESSIONS AGAINST INVALID STRINGS typeof foo ===

    'strnig' typeof foo === 'undefimed' typeof bar !== 'nunber' typeof bar !== 'fucntion'
  5. USE STANDARD $ npm install standard --save-dev $ standard Error:

    Use JavaScript Standard Style main.js:950:11: Expected '===' and instead saw '=='.
  6. QUIZ: WHAT DO THESE EXPRESSIONS EVALUATE TO? [] == false

    // true [] == ![] // true 3 == '03' // true
  7. REQUIRE === AND !== // NOT OK if (x ==

    42) { } // OK if (x === 42) { }
  8. STANDARD IS PRAGMATIC // OK if (x == null) {

    } // THE ABOVE IS EQUIVALENT TO... if (x === null || x === undefined) { }
  9. 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(...);
  10. REQUIRE CURLY BRACE CONVENTIONS // NOT OK while (bar) baz()

    // NOT OK if (foo) { baz() } else qux()
  11. STANDARD IS PRAGMATIC // OK readFile(file, (err, data) => {

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