SlideShare una empresa de Scribd logo
1 de 79
Descargar para leer sin conexión
Managing and Visualizing

JavaScript Complexity
Jarrod Overson
Consultant @ Gossamer.io
jarrodoverson.com
Yes, we’ll talk about

“Code Complexity”
and

“Code Quality”
Check your bias at the door
We’ll deal with it later
1. Why is this important now
2. Static Analysis & Linting
3. Visualizing Complexity
The obvious…
JavaScript is Dynamic
all the obvious pitfalls compounded by
Immature tooling and IDEs
Wildly variable module styles
Best practices vary as language evolves
Server & Client similar yet so different
The Talent Pool is ridic
Web Platform
Engineers

jQuery experts
Closures?
The less obvious…
Progress is staggering
It’s hard to keep up
The next tech might not be usable yet
When it is, you want to actually
be able to use it
Refactoring isn’t easy
Callback hell is more than
just deep nesting
!

IDEs can’t help much, yet
!

But flexibility is more important
on the web than anywhere else
And the hard to admit…
The Web is hard
Web Applications are not solved
Even the giants pivot and backtrack
So many solutions still don’t exist
So why are we here?
Why bother?
JS coasted to the lead
on neutral
And this isn’t even

its final form
We can see where it’s headed
and we’re betting on JS
1. Why is this important now
2. Static Analysis & Linting
3. Visualizing Complexity
Respect your JavaScript
and codify that respect.
All code should look the same.
Style
Naming
Punctuation
Indentation
Comments
Case
Get everyone together
1. Agree
2. Document
3. Enforce
https://github.com/rwaldron/idiomatic.js/
https://github.com/Seravo/js-winning-style
https://github.com/airbnb/javascript
Coding conventions based on
Github analysis
1. >90% use last comma
2. >80% use space indents
3. >55% use single quotes
http://sideeffect.kr/popularconvention/#javascript
Lax enforcement begets violations.

Warnings need to fail builds.
These is as important as failed tests.
Know your options
JSLint
	 	

Crockford-style linter, low configuration

Closure Linter
	 	

Google-style linter, low configuration

JSHint ✔
	 	

Community driven JSLint fork, moderately configurable

ESLint ✔
	 	

Pluggable styles, highly configurable
Know your options’ options
{

}

"maxerr"
"bitwise"
"camelcase"
"curly"
"eqeqeq"
"forin"
"immed"
"indent"
"latedef"
"newcap"
"noarg"
"noempty"
"nonew"
"plusplus"
"quotmark"
//...

:
:
:
:
:
:
:
:
:
:
:
:
:
:
:

50,
true,
false,
true,
true,
true,
false,
4,
false,
false,
true,
true,
false,
false,
false
Be aggressive.
Default to overly strict.
Smart deviation is
OK and expected.
!
!

function fn(param) {
/*jshint eqeqeq:false*/
!

if (param == 42) return;
!

}
Set complexity limits
"maxparams"
"maxdepth"
"maxstatements"
"maxcomplexity"
"maxlen"

:
:
:
:
:

4,
4,
20,
7,
100
What is

Cyclomatic Complexity
a.k.a.

Conditional Complexity
?
Cyclomatic Complexity
is not
magic
nerd hokum
something you should ignore
Cyclomatic Complexity
is
the number of paths
through a block of code
(technically)
Cyclomatic Complexity
is
how hard your code
is to test.
(practically)
Complexity : 1
!

function main(a) {
!

}
Complexity : 2
function main(a) {
if (a > 5) {
}
}
Complexity : ? still 2
function main(a) {
if (a > 5) {
!

} else {
!

}
}
Complexity : ? now 3
function main(a) {
if (a > 10) {
!

} else if(a > 5) {
!

}
}
Complexity : ? still 3
function main(a) {
if (a > 10) {
!

} else if(a > 5) {
!

} else {
!

}

}
Complexity : ? also 3
function main(a) {
if (a > 5) {
if (a > 10) {
!

}
}
}
Complexity : 7
function main(a) {
if (a) {
} else if (a) {
}
!

if (other) { }
!

}

for (var i = 0; i < a; i++) {
if (i % 2) {
} else if (i % 3) {
}
}
Don’t get hung up on numbers
!

function main() {
/*jshint maxcomplexity:12*/
!

}

//...

!

* note : jshint calculates complexity differently than
complexity-report (plato, grunt-complexity)
Cyclomatic Complexity
is an early warning but isn’t everything.
OMG! I’m going to
make the best .jshintrc
It’s ok.
Have an ideal set of options,
and a current set that passes now.

Visualize your goal.
1. Why is this important now
2. Static Analysis & Linting
3. Visualizing Complexity
Plato.
One cool guy.

github.com/es-analysis/plato
Visualize your progress.
Target hot spots and track progress.

Promote files when ready.
When a file clears, promote it
to your ideal jshintrc.
Files passing “ideal” settings
Files to target next
Someday…
Challenge Accepted.

You
But wait! There’s MORE!
Code is a liability.
Your job is to provide value with
as little code as possible.
How many lines of code does
your main project have right now?
If you don’t know, within 10%,
then you’re ignoring it.
Treat SLOC like credit card debt.

Don’t add to it without knowing the balance.
Maintainability Index?

Awesome,
JavaScript is a real
platform now!

You’re
drunk

You’re both right.
Maintainability : 100

// empty file

Well we can buy that.
Maintainability : 95
var foo = 42;

Seems harsh, but ok.
Maintainability : 83
var foo = 42;
!

var bar = Math.log(foo++);

Holy crap, we’re dropping fast…
Maintainability : 92
var foo = 42;
!

function calc(x) {
return Math.log(x++);
}
!

var bar = calc(foo);
Ok, that does seem better…
Toolable via grunt-complexity

https://github.com/vigetlabs/grunt-complexity
What are we really working with here?
var vocabulary = unqOperators + unqOperands;
var length = totOperators + totOperands;
var difficulty = (unqOperators / 2) *
(totOperands / unqOperands);
var volume = length * Math.log2(vocabulary);
var effort = difficulty * volume;
!

But don’t look at

var maintainabilityIndex = Math.max(
0,
(
171 +
-3.42 * Math.log(aveEffort) +
!
-0.23 * (aveComplexity) +
-16.2 * Math.log(aveLOC)
Smarter people are responsible
) * 100 / 171
);

me for questions
1976 Thomas McCabe - Cyclomatic Complexity
1977 Maurice Halstead - Halstead Metrics
1991 Oman/Hagemeister - Maintainability Index

Phil Booth
JavaScript Implementation

Ariya Hidayat
Source Analysis (Esprima)
Oh, come on.
These numbers are for
introspection and exploration
These calculations have been praised and
criticised, promoted and shot down.
(and Halstead died before
being able to defend them)
The point is
“ The unexamined code
is not worth releasing ”
- Socrates
Code
is not just logic
Code
is the api between
imagination
and reality
Inconsistent, complex

Code
is an inconsistent, complex

API
Tool
how you code
Hack
how you code
Visualize
how you code
Visualize
Everything
Jarrod Overson
@jsoverson
jarrod@jsoverson.com
jsoverson.com
jsoverson.com/google+
jsoverson.com/linkedin
jsoverson.com/github
jsoverson.com/twitter

Más contenido relacionado

La actualidad más candente

Documents of which registration is compulsory
Documents of which registration is compulsoryDocuments of which registration is compulsory
Documents of which registration is compulsory
Yasir Hayat
 
State of Maharashtra Vs Manesh Madhusudan Kotiyan
State of Maharashtra Vs Manesh Madhusudan KotiyanState of Maharashtra Vs Manesh Madhusudan Kotiyan
State of Maharashtra Vs Manesh Madhusudan Kotiyan
Anubhuti Shreya
 
Administrative law questions and answers2
Administrative law questions and answers2Administrative law questions and answers2
Administrative law questions and answers2
Baker Kosmac-Okwir
 

La actualidad más candente (20)

Documents of which registration is compulsory
Documents of which registration is compulsoryDocuments of which registration is compulsory
Documents of which registration is compulsory
 
Nullity of marriage
Nullity of marriageNullity of marriage
Nullity of marriage
 
Framing of suit
Framing of suitFraming of suit
Framing of suit
 
State of Maharashtra Vs Manesh Madhusudan Kotiyan
State of Maharashtra Vs Manesh Madhusudan KotiyanState of Maharashtra Vs Manesh Madhusudan Kotiyan
State of Maharashtra Vs Manesh Madhusudan Kotiyan
 
Inter country adoption
Inter country adoptionInter country adoption
Inter country adoption
 
The special relief act, 1877
The special relief act, 1877The special relief act, 1877
The special relief act, 1877
 
Cross examination
Cross examinationCross examination
Cross examination
 
Indian Evidence Act
Indian Evidence ActIndian Evidence Act
Indian Evidence Act
 
Administrative law questions and answers2
Administrative law questions and answers2Administrative law questions and answers2
Administrative law questions and answers2
 
Indian limitation act 1963
Indian limitation act 1963Indian limitation act 1963
Indian limitation act 1963
 
Exclusion of Foreign Law.pptx
Exclusion of Foreign Law.pptxExclusion of Foreign Law.pptx
Exclusion of Foreign Law.pptx
 
Cpc
CpcCpc
Cpc
 
Cpc learning module 9 law of limitation
Cpc learning module 9 law of limitationCpc learning module 9 law of limitation
Cpc learning module 9 law of limitation
 
Confession an overview
Confession an overviewConfession an overview
Confession an overview
 
Code of civil procedure 1908 incidental proceedings
Code of civil procedure 1908 incidental proceedingsCode of civil procedure 1908 incidental proceedings
Code of civil procedure 1908 incidental proceedings
 
The limitation act, 1908
The limitation act, 1908The limitation act, 1908
The limitation act, 1908
 
Tort introduction
Tort introduction Tort introduction
Tort introduction
 
Limitation act
Limitation actLimitation act
Limitation act
 
How to make plaint,Presented by :Shah Arafat Ayatullah
How to make plaint,Presented by :Shah Arafat AyatullahHow to make plaint,Presented by :Shah Arafat Ayatullah
How to make plaint,Presented by :Shah Arafat Ayatullah
 
Austins theory of analytical positivism
Austins theory of analytical positivismAustins theory of analytical positivism
Austins theory of analytical positivism
 

Destacado (7)

React, Redux and es6/7
React, Redux and es6/7React, Redux and es6/7
React, Redux and es6/7
 
React Redux React Native
React Redux React NativeReact Redux React Native
React Redux React Native
 
Understanding, measuring and improving code quality in JavaScript
Understanding, measuring and improving code quality in JavaScriptUnderstanding, measuring and improving code quality in JavaScript
Understanding, measuring and improving code quality in JavaScript
 
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
버전관리를 들어본적 없는 사람들을 위한 DVCS - Git
 
Software Metrics
Software MetricsSoftware Metrics
Software Metrics
 
Github 으로 학교 팀 프로젝트 하기
Github 으로 학교 팀 프로젝트 하기Github 으로 학교 팀 프로젝트 하기
Github 으로 학교 팀 프로젝트 하기
 
5 Presentation design trends 2017
5 Presentation design trends 20175 Presentation design trends 2017
5 Presentation design trends 2017
 

Similar a Managing JavaScript Complexity

Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
elliando dias
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
Ajax Experience 2009
 

Similar a Managing JavaScript Complexity (20)

All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
Search for Vulnerabilities Using Static Code Analysis
Search for Vulnerabilities Using Static Code AnalysisSearch for Vulnerabilities Using Static Code Analysis
Search for Vulnerabilities Using Static Code Analysis
 
Real World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case StudyReal World Single Page App - A Knockout Case Study
Real World Single Page App - A Knockout Case Study
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
writing self-modifying code and utilizing advanced assembly techniques
writing self-modifying code and utilizing advanced assembly techniqueswriting self-modifying code and utilizing advanced assembly techniques
writing self-modifying code and utilizing advanced assembly techniques
 
Huge web apps web expo 2013
Huge web apps web expo 2013Huge web apps web expo 2013
Huge web apps web expo 2013
 
Browsers
BrowsersBrowsers
Browsers
 
NodeJS: the good parts? A skeptic’s view (jmaghreb, jmaghreb2013)
NodeJS: the good parts? A skeptic’s view (jmaghreb, jmaghreb2013)NodeJS: the good parts? A skeptic’s view (jmaghreb, jmaghreb2013)
NodeJS: the good parts? A skeptic’s view (jmaghreb, jmaghreb2013)
 
Managing JavaScript Complexity in Teams - Fluent
Managing JavaScript Complexity in Teams - FluentManaging JavaScript Complexity in Teams - Fluent
Managing JavaScript Complexity in Teams - Fluent
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
All of javascript
All of javascriptAll of javascript
All of javascript
 
10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript Tips10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript Tips
 
Test driven development for infrastructure as-a-code, the future trend_Gianfr...
Test driven development for infrastructure as-a-code, the future trend_Gianfr...Test driven development for infrastructure as-a-code, the future trend_Gianfr...
Test driven development for infrastructure as-a-code, the future trend_Gianfr...
 
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)
 
Planning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teamsPlanning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teams
 
Douglas Crockford Presentation Goodparts
Douglas Crockford Presentation GoodpartsDouglas Crockford Presentation Goodparts
Douglas Crockford Presentation Goodparts
 
Developer Job in Practice
Developer Job in PracticeDeveloper Job in Practice
Developer Job in Practice
 
SFScon 21 - Davide Montesin - Typescript vs. Java
SFScon 21 - Davide Montesin - Typescript vs. JavaSFScon 21 - Davide Montesin - Typescript vs. Java
SFScon 21 - Davide Montesin - Typescript vs. Java
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 

Más de Jarrod Overson

Más de Jarrod Overson (20)

Practical WebAssembly with Apex, wasmRS, and nanobus
Practical WebAssembly with Apex, wasmRS, and nanobusPractical WebAssembly with Apex, wasmRS, and nanobus
Practical WebAssembly with Apex, wasmRS, and nanobus
 
AppSecCali - How Credential Stuffing is Evolving
AppSecCali - How Credential Stuffing is EvolvingAppSecCali - How Credential Stuffing is Evolving
AppSecCali - How Credential Stuffing is Evolving
 
How Credential Stuffing is Evolving - PasswordsCon 2019
How Credential Stuffing is Evolving - PasswordsCon 2019How Credential Stuffing is Evolving - PasswordsCon 2019
How Credential Stuffing is Evolving - PasswordsCon 2019
 
JSconf JP - Analysis of an exploited npm package. Event-stream's role in a su...
JSconf JP - Analysis of an exploited npm package. Event-stream's role in a su...JSconf JP - Analysis of an exploited npm package. Event-stream's role in a su...
JSconf JP - Analysis of an exploited npm package. Event-stream's role in a su...
 
Analysis of an OSS supply chain attack - How did 8 millions developers downlo...
Analysis of an OSS supply chain attack - How did 8 millions developers downlo...Analysis of an OSS supply chain attack - How did 8 millions developers downlo...
Analysis of an OSS supply chain attack - How did 8 millions developers downlo...
 
Deepfakes - How they work and what it means for the future
Deepfakes - How they work and what it means for the futureDeepfakes - How they work and what it means for the future
Deepfakes - How they work and what it means for the future
 
The State of Credential Stuffing and the Future of Account Takeovers.
The State of Credential Stuffing and the Future of Account Takeovers.The State of Credential Stuffing and the Future of Account Takeovers.
The State of Credential Stuffing and the Future of Account Takeovers.
 
How to Reverse Engineer Web Applications
How to Reverse Engineer Web ApplicationsHow to Reverse Engineer Web Applications
How to Reverse Engineer Web Applications
 
The life of breached data and the attack lifecycle
The life of breached data and the attack lifecycleThe life of breached data and the attack lifecycle
The life of breached data and the attack lifecycle
 
The Life of Breached Data & The Dark Side of Security
The Life of Breached Data & The Dark Side of SecurityThe Life of Breached Data & The Dark Side of Security
The Life of Breached Data & The Dark Side of Security
 
Shape Security @ WaffleJS October 16
Shape Security @ WaffleJS October 16Shape Security @ WaffleJS October 16
Shape Security @ WaffleJS October 16
 
Graphics Programming for Web Developers
Graphics Programming for Web DevelopersGraphics Programming for Web Developers
Graphics Programming for Web Developers
 
The Dark Side of Security
The Dark Side of SecurityThe Dark Side of Security
The Dark Side of Security
 
JavaScript and the AST
JavaScript and the ASTJavaScript and the AST
JavaScript and the AST
 
ES2015 workflows
ES2015 workflowsES2015 workflows
ES2015 workflows
 
Maintainability SFJS Sept 4 2014
Maintainability SFJS Sept 4 2014 Maintainability SFJS Sept 4 2014
Maintainability SFJS Sept 4 2014
 
Idiot proofing your code
Idiot proofing your codeIdiot proofing your code
Idiot proofing your code
 
Riot on the web - Kenote @ QCon Sao Paulo 2014
Riot on the web - Kenote @ QCon Sao Paulo 2014Riot on the web - Kenote @ QCon Sao Paulo 2014
Riot on the web - Kenote @ QCon Sao Paulo 2014
 
Real World Web components
Real World Web componentsReal World Web components
Real World Web components
 
Continuous Delivery for the Web Platform
Continuous Delivery for the Web PlatformContinuous Delivery for the Web Platform
Continuous Delivery for the Web Platform
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
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...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 

Managing JavaScript Complexity