SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
JSHint 
Learning JavaScript the Hard Way 
Adrian-Tudor Pănescu 
tudor.panescu@cynny.com
About me 
● ~5 years of Computer Science 
● Germany, Switzerland, Romania 
● NEC, CERN, Cynny Social Cloud 
● Web Applications Development 
jobs@cynny.com 2
JavaScript 
● Used first time around 2010 
● Recently moved from Python 
● Open Source: Invenio, Intro.js, silog 
jobs@cynny.com 3
First Real JavaScript Project 
● Inherited project with some (many) 
issues 
● Web Analytics/ Advertisements Web 
App 
● Shared client/ server code base 
jobs@cynny.com 4
Taming the Beast 
● First step: understand the code 
● pylint/ PEP8 → JSHint 
● Cleanup → Discovery ↔ Learning 
jobs@cynny.com 5
JSHint 
● Static code analysis 
● Highly-configurable 
● Open-source, used by: 
○ Mozilla 
○ Twitter 
○ Facebook 
○ Apache 
jobs@cynny.com 6
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) 
jobs@cynny.com 7
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? 
jobs@cynny.com 8
eqeqeq 
● Expected '===' and instead saw '=='. 
● Anyone coming from any other 
language to JS will find this amazing 
● [ ] == 0 // true 
● [ ] === 0 // false 
jobs@cynny.com 9
eqeqeq (Cont.) 
jobs@cynny.com 10
eqeqeq (Cont.) 
jobs@cynny.com 10
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 
jobs@cynny.com 11
foo is already defined (-W004) 
jobs@cynny.com 12
Deeper the Rabbit Hole - latedef 
jobs@cynny.com 13
Is this real life? 
● Function scope - funcscope, shadow 
● Hoisting 
● Closures 
● Missing var… but not in strict mode, 
whatever that is 
jobs@cynny.com 14
Semicolons: FUD or truth? 
jobs@cynny.com 15
Semicolons: FUD or truth? 
jobs@cynny.com 15
asi/ lastsemic (Cont.) 
● Zen of Python: “Explicit is better than 
implicit.” 
● Controversial, but smart life decision 
for (junior?) developers 
jobs@cynny.com 16
Curly 
jobs@cynny.com 17
forin 
jobs@cynny.com 18
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. 
jobs@cynny.com 19
Code quality 
● quotmark 
● immed 
● boss 
● newcap 
● expr 
● notypeof 
● freeze 
● maxlen 
● unused 
● loopfunc 
● camelcase 
● es3/ esnext 
● noempty 
● nonew 
jobs@cynny.com 20
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 
jobs@cynny.com 21
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 
jobs@cynny.com 22
Conclusions (Cont.) 
● Can help maintain some standards, 
especially among heterogeneous teams 
● My project had a good outcome, some 
JS learned 
● Still using JSHint 
jobs@cynny.com 23
Your questions, 
answered. 
jobs@cynny.com 24
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 
jobs@cynny.com 25
How do I use it? 
● Editor plugin 
● Grunt task: grunt-contrib-jshint 
● Git hook: pre-push, merge request 
● Continuous integration step 
jobs@cynny.com 26

Weitere ähnliche Inhalte

Ähnlich wie Learning JavaScript Code Quality with JSHint

Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScriptOffirmo
 
Using Guided Missiles in Drive-bys: Automatic Browser Fingerprinting and Expl...
Using Guided Missiles in Drive-bys: Automatic Browser Fingerprinting and Expl...Using Guided Missiles in Drive-bys: Automatic Browser Fingerprinting and Expl...
Using Guided Missiles in Drive-bys: Automatic Browser Fingerprinting and Expl...egypt
 
Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplateStanislav Petrov
 
The Future is Here: ECMAScript 6 in the Wild
The Future is Here: ECMAScript 6 in the WildThe Future is Here: ECMAScript 6 in the Wild
The Future is Here: ECMAScript 6 in the WildAdrian-Tudor Panescu
 
ooc - OSDC 2010 - Amos Wenger
ooc - OSDC 2010 - Amos Wengerooc - OSDC 2010 - Amos Wenger
ooc - OSDC 2010 - Amos WengerAmos Wenger
 
Universal job embedding in recommendation (public ver.)
Universal job embedding in recommendation (public ver.)Universal job embedding in recommendation (public ver.)
Universal job embedding in recommendation (public ver.)Marsan Ma
 
Pentester++
Pentester++Pentester++
Pentester++CTruncer
 
Nodejs web service for starters
Nodejs web service for startersNodejs web service for starters
Nodejs web service for startersBruce Li
 
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...dcieslak
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataDaniel Bristot de Oliveira
 
Asynchronous job queues with python-rq
Asynchronous job queues with python-rqAsynchronous job queues with python-rq
Asynchronous job queues with python-rqAshish Acharya
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark... Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...Databricks
 
Black Ops Testing Workshop from Agile Testing Days 2014
Black Ops Testing Workshop from Agile Testing Days 2014Black Ops Testing Workshop from Agile Testing Days 2014
Black Ops Testing Workshop from Agile Testing Days 2014Alan Richardson
 
.NET compiler platform codename Roslyn
.NET compiler platform codename Roslyn.NET compiler platform codename Roslyn
.NET compiler platform codename RoslynPiotr Benetkiewicz
 

Ähnlich wie Learning JavaScript Code Quality with JSHint (20)

Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScript
 
Using Guided Missiles in Drive-bys: Automatic Browser Fingerprinting and Expl...
Using Guided Missiles in Drive-bys: Automatic Browser Fingerprinting and Expl...Using Guided Missiles in Drive-bys: Automatic Browser Fingerprinting and Expl...
Using Guided Missiles in Drive-bys: Automatic Browser Fingerprinting and Expl...
 
Gamedev-grade debugging
Gamedev-grade debuggingGamedev-grade debugging
Gamedev-grade debugging
 
Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplate
 
The Future is Here: ECMAScript 6 in the Wild
The Future is Here: ECMAScript 6 in the WildThe Future is Here: ECMAScript 6 in the Wild
The Future is Here: ECMAScript 6 in the Wild
 
Ecma6 in the wild
Ecma6 in the wildEcma6 in the wild
Ecma6 in the wild
 
Ecma6 in the wild
Ecma6 in the wildEcma6 in the wild
Ecma6 in the wild
 
Java vs. C/C++
Java vs. C/C++Java vs. C/C++
Java vs. C/C++
 
ooc - OSDC 2010 - Amos Wenger
ooc - OSDC 2010 - Amos Wengerooc - OSDC 2010 - Amos Wenger
ooc - OSDC 2010 - Amos Wenger
 
Universal job embedding in recommendation (public ver.)
Universal job embedding in recommendation (public ver.)Universal job embedding in recommendation (public ver.)
Universal job embedding in recommendation (public ver.)
 
Pentester++
Pentester++Pentester++
Pentester++
 
Nodejs web service for starters
Nodejs web service for startersNodejs web service for starters
Nodejs web service for starters
 
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
RandomTest - Random Software Integration Tests That Just Work for C/C++, Java...
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
 
Asynchronous job queues with python-rq
Asynchronous job queues with python-rqAsynchronous job queues with python-rq
Asynchronous job queues with python-rq
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark... Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 
Cloud accounting software uk
Cloud accounting software ukCloud accounting software uk
Cloud accounting software uk
 
Black Ops Testing Workshop from Agile Testing Days 2014
Black Ops Testing Workshop from Agile Testing Days 2014Black Ops Testing Workshop from Agile Testing Days 2014
Black Ops Testing Workshop from Agile Testing Days 2014
 
.NET compiler platform codename Roslyn
.NET compiler platform codename Roslyn.NET compiler platform codename Roslyn
.NET compiler platform codename Roslyn
 

Mehr von Adrian-Tudor Panescu

TAO: Facebook's Distributed Data Store for the Social Graph
TAO: Facebook's Distributed Data Store for the Social GraphTAO: Facebook's Distributed Data Store for the Social Graph
TAO: Facebook's Distributed Data Store for the Social GraphAdrian-Tudor Panescu
 
Parallel Prime Number Generation Using The Sieve of Eratosthenes
Parallel Prime Number Generation Using The Sieve of EratosthenesParallel Prime Number Generation Using The Sieve of Eratosthenes
Parallel Prime Number Generation Using The Sieve of EratosthenesAdrian-Tudor Panescu
 
Companion – An intelligent agent architecture [RO]
Companion – An intelligent agent architecture [RO]Companion – An intelligent agent architecture [RO]
Companion – An intelligent agent architecture [RO]Adrian-Tudor Panescu
 
Modelling a system using Petri Nets [RO]
Modelling a system using Petri Nets [RO]Modelling a system using Petri Nets [RO]
Modelling a system using Petri Nets [RO]Adrian-Tudor Panescu
 

Mehr von Adrian-Tudor Panescu (7)

From Idead to Product in 48 Hours
From Idead to Product in 48 HoursFrom Idead to Product in 48 Hours
From Idead to Product in 48 Hours
 
TAO: Facebook's Distributed Data Store for the Social Graph
TAO: Facebook's Distributed Data Store for the Social GraphTAO: Facebook's Distributed Data Store for the Social Graph
TAO: Facebook's Distributed Data Store for the Social Graph
 
ECMAScript 2015 Tips & Traps
ECMAScript 2015 Tips & TrapsECMAScript 2015 Tips & Traps
ECMAScript 2015 Tips & Traps
 
Parallel Prime Number Generation Using The Sieve of Eratosthenes
Parallel Prime Number Generation Using The Sieve of EratosthenesParallel Prime Number Generation Using The Sieve of Eratosthenes
Parallel Prime Number Generation Using The Sieve of Eratosthenes
 
Companion – An intelligent agent architecture [RO]
Companion – An intelligent agent architecture [RO]Companion – An intelligent agent architecture [RO]
Companion – An intelligent agent architecture [RO]
 
Modelling a system using Petri Nets [RO]
Modelling a system using Petri Nets [RO]Modelling a system using Petri Nets [RO]
Modelling a system using Petri Nets [RO]
 
Visualizing screen overlaps
Visualizing screen overlapsVisualizing screen overlaps
Visualizing screen overlaps
 

Kürzlich hochgeladen

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Kürzlich hochgeladen (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Learning JavaScript Code Quality with JSHint

  • 1. JSHint Learning JavaScript the Hard Way Adrian-Tudor Pănescu tudor.panescu@cynny.com
  • 2. About me ● ~5 years of Computer Science ● Germany, Switzerland, Romania ● NEC, CERN, Cynny Social Cloud ● Web Applications Development jobs@cynny.com 2
  • 3. JavaScript ● Used first time around 2010 ● Recently moved from Python ● Open Source: Invenio, Intro.js, silog jobs@cynny.com 3
  • 4. First Real JavaScript Project ● Inherited project with some (many) issues ● Web Analytics/ Advertisements Web App ● Shared client/ server code base jobs@cynny.com 4
  • 5. Taming the Beast ● First step: understand the code ● pylint/ PEP8 → JSHint ● Cleanup → Discovery ↔ Learning jobs@cynny.com 5
  • 6. JSHint ● Static code analysis ● Highly-configurable ● Open-source, used by: ○ Mozilla ○ Twitter ○ Facebook ○ Apache jobs@cynny.com 6
  • 7. 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) jobs@cynny.com 7
  • 8. 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? jobs@cynny.com 8
  • 9. eqeqeq ● Expected '===' and instead saw '=='. ● Anyone coming from any other language to JS will find this amazing ● [ ] == 0 // true ● [ ] === 0 // false jobs@cynny.com 9
  • 12. 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 jobs@cynny.com 11
  • 13. foo is already defined (-W004) jobs@cynny.com 12
  • 14. Deeper the Rabbit Hole - latedef jobs@cynny.com 13
  • 15. Is this real life? ● Function scope - funcscope, shadow ● Hoisting ● Closures ● Missing var… but not in strict mode, whatever that is jobs@cynny.com 14
  • 16. Semicolons: FUD or truth? jobs@cynny.com 15
  • 17. Semicolons: FUD or truth? jobs@cynny.com 15
  • 18. asi/ lastsemic (Cont.) ● Zen of Python: “Explicit is better than implicit.” ● Controversial, but smart life decision for (junior?) developers jobs@cynny.com 16
  • 21. 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. jobs@cynny.com 19
  • 22. Code quality ● quotmark ● immed ● boss ● newcap ● expr ● notypeof ● freeze ● maxlen ● unused ● loopfunc ● camelcase ● es3/ esnext ● noempty ● nonew jobs@cynny.com 20
  • 23. 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 jobs@cynny.com 21
  • 24. 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 jobs@cynny.com 22
  • 25. Conclusions (Cont.) ● Can help maintain some standards, especially among heterogeneous teams ● My project had a good outcome, some JS learned ● Still using JSHint jobs@cynny.com 23
  • 26. Your questions, answered. jobs@cynny.com 24
  • 27. 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 jobs@cynny.com 25
  • 28. How do I use it? ● Editor plugin ● Grunt task: grunt-contrib-jshint ● Git hook: pre-push, merge request ● Continuous integration step jobs@cynny.com 26