jshint: learning javascript the hard way

28
JSHint Learning JavaScript the Hard Way Adrian-Tudor Pănescu [email protected]

Upload: adrian-tudor-panescu

Post on 05-Jul-2015

347 views

Category:

Technology


11 download

DESCRIPTION

If you want to learn JavaScript nowadays, you will normally go to W3Schools, MDN, or Codecademy, start with a simple "helloWorld()" tutorial and build up from there. Here is an alternative to this method of learning the intricacies of an otherwise cool language: take a junior programmer coming from the Java/ Python/ C++ world, give him an existing NodeJS/ MongoDB/ Redis/ Web Sockets/ single-page HTML5 Web application, and one month to add new major features to it. Our junior does not know JavaScript, but is in love with static analysis/ linting tools, and thinks of using JSLint in order to both learn the language and complete his tasks in time with a minimal number of bugs. As this is the way in which I learned JavaScript, I will present you my experience, including the fact that the programmers before me had little idea of how the language actually works, what lessons I was (self-)taught in the process, and how this knowledge and some practices stuck with me. While this was an unconventional learning experience, I believe that the method can be quite efficient, maybe not for first-year Computer Science students, but for programmers coming from other language realms.

TRANSCRIPT

Page 1: JSHint: Learning JavaScript the Hard Way

JSHint Learning JavaScript the Hard Way

Adrian-Tudor Pă[email protected]

Page 2: JSHint: Learning JavaScript the Hard Way

About me

● ~5 years of Computer Science● Germany, Switzerland, Romania● NEC, CERN, Cynny Social Cloud● Web Applications Development

[email protected]

Page 3: JSHint: Learning JavaScript the Hard Way

JavaScript

● Used first time around 2010● Recently moved from Python● Open Source: Invenio, Intro.js, silog

[email protected]

Page 4: JSHint: Learning JavaScript the Hard Way

First Real JavaScript Project

● Inherited project with some (many) issues

● Web Analytics/ Advertisements Web App

● Shared client/ server code base

[email protected]

Page 5: JSHint: Learning JavaScript the Hard Way

Taming the Beast

● First step: understand the code● pylint/ PEP8 → JSHint● Cleanup → Discovery ↔ Learning

[email protected]

Page 6: JSHint: Learning JavaScript the Hard Way

JSHint

● Static code analysis● Highly-configurable● Open-source, used by:

○ Mozilla○ Twitter○ Facebook○ Apache

[email protected]

Page 7: JSHint: Learning JavaScript the Hard Way

What would JSHint complain about?

gcd = function(first_number, second_number) {

var result;

while (first_number != second_number)

if (first_number > second_number) {

var first_number = first_number - second_number;

} else if (first_number <= second_number) {

second_number -= first_number;

} else {}

console.log('result is:', first_number | second_number);

}(12, 24)

[email protected]

Page 8: JSHint: Learning JavaScript the Hard Way

First run

● maxerr = 50, hit that● Things started looking suspicious● Did the guys before me have any idea

what they were doing?● Do I have any idea about what I am

doing?

[email protected]

Page 9: JSHint: Learning JavaScript the Hard Way

eqeqeq

● Expected '===' and instead saw '=='.● Anyone coming from any other

language to JS will find this amazing● [ ] == 0 // true● [ ] === 0 // false

[email protected]

Page 10: JSHint: Learning JavaScript the Hard Way

eqeqeq (Cont.)

[email protected]

Page 11: JSHint: Learning JavaScript the Hard Way

eqeqeq (Cont.)

[email protected] 10

Page 12: JSHint: Learning JavaScript the Hard Way

eqeqeq (Cont.)

● The Strict Equals Operator (===): shorter, more straight-forward algorithm

● Better for someone accustomed to this behaviour

● Inexperienced devs might miss the point of == and experience some sleepless nights

[email protected] 11

Page 13: JSHint: Learning JavaScript the Hard Way

foo is already defined (-W004)

[email protected] 12

Page 14: JSHint: Learning JavaScript the Hard Way

Deeper the Rabbit Hole - latedef

[email protected] 13

Page 15: JSHint: Learning JavaScript the Hard Way

Is this real life?

● Function scope - funcscope, shadow● Hoisting● Closures● Missing var… but not in strict mode,

whatever that is

[email protected] 14

Page 16: JSHint: Learning JavaScript the Hard Way

Semicolons: FUD or truth?

[email protected] 15

Page 17: JSHint: Learning JavaScript the Hard Way

Semicolons: FUD or truth?

[email protected] 15

Page 18: JSHint: Learning JavaScript the Hard Way

asi/ lastsemic (Cont.)

● Zen of Python: “Explicit is better than implicit.”

● Controversial, but smart life decision for (junior?) developers

[email protected] 16

Page 19: JSHint: Learning JavaScript the Hard Way

Curly

[email protected] 17

Page 20: JSHint: Learning JavaScript the Hard Way

forin

[email protected] 18

Page 21: JSHint: Learning JavaScript the Hard Way

forin (Cont.)

● The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.

● So, it doesn’t work like Python…? (obv.)● typeof [] === ‘object’ - I am really confused

now.

[email protected] 19

Page 22: JSHint: Learning JavaScript the Hard Way

Code quality

● quotmark● immed● boss● newcap● expr● notypeof● freeze

● maxlen● unused● loopfunc● camelcase● es3/ esnext● noempty● nonew

[email protected] 20

Page 23: JSHint: Learning JavaScript the Hard Way

Function over style?

● maxparams - maybe use an args object?

● maxstatements - don’t make me scroll● maxdepth - nests, nests everywhere● maxcomplexity - if for if wtf if while if

else

[email protected]

Page 24: JSHint: Learning JavaScript the Hard Way

Conclusions

● Static code analysis: controversial but will help novices (and not only) write maintainable code

● Expose juniors to some new concepts○ latedef and -W004 will send you

through a long spiritual journey

[email protected] 22

Page 25: JSHint: Learning JavaScript the Hard Way

Conclusions (Cont.)

● Can help maintain some standards, especially among heterogeneous teams

● My project had a good outcome, some JS learned

● Still using JSHint

[email protected] 23

Page 26: JSHint: Learning JavaScript the Hard Way

Your questions,answered.

[email protected] 24

Page 27: JSHint: Learning JavaScript the Hard Way

Other tools

● JSLint - more explanatory, explicit○ jslinterrors.com

● ESLint - very configurable, check it out!● Google Closure Linter - strict typing● Flow - static type checker● JSure, JavaScript Lint

[email protected]

Page 28: JSHint: Learning JavaScript the Hard Way

How do I use it?

● Editor plugin● Grunt task: grunt-contrib-jshint● Git hook: pre-push, merge request● Continuous integration step

[email protected]