SlideShare a Scribd company logo
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
Managing JavaScript Complexity
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
Managing JavaScript Complexity
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!
Managing JavaScript Complexity
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.
Managing JavaScript Complexity
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)
Managing JavaScript Complexity
Managing JavaScript Complexity
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

More Related Content

Similar to Managing JavaScript Complexity (20)

PDF
Idiot proofing your code
Jarrod Overson
 
PDF
Maintainability SFJS Sept 4 2014
Jarrod Overson
 
PDF
Refactoring JavaScript turning bad code into good code First Edition Burchard
simbajdzikie4
 
PDF
Good Coding Practices with JavaScript
🏁 Pierre-Henry Soria 💡
 
PDF
Managing JavaScript Complexity in Teams - Fluent
Jarrod Overson
 
PDF
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Fwdays
 
PPTX
Tech talk on code quality
Alexander Osin
 
PDF
How to write good quality code
Hayden Bleasel
 
PDF
20100309 01 - Maintenance and re-engineering (McCabe)
LeClubQualiteLogicielle
 
PDF
The top 5 JavaScript issues in all our codebases
Phil Nash
 
PDF
Managing and evolving JavaScript Code
Jean Carlo Emer
 
PDF
Your cyclomatic complexity is so 1.9,76
asciidisco
 
PPTX
Maintainable JavaScript 2012
Nicholas Zakas
 
PDF
Sharable of qualities of clean code
Eman Mohamed
 
PPTX
Cyclomatic complexity
Angelo Trozzo
 
PDF
"JS: the right way" by Mykyta Semenistyi
Binary Studio
 
PPTX
Clean Code
Rajesh Kumar
 
PDF
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
 
PDF
15 Experts on the Art of JavaScript Programming
FusionCharts
 
PPTX
HTML5 Developer Conference 2013: Javascript Insights
Ann Robson
 
Idiot proofing your code
Jarrod Overson
 
Maintainability SFJS Sept 4 2014
Jarrod Overson
 
Refactoring JavaScript turning bad code into good code First Edition Burchard
simbajdzikie4
 
Good Coding Practices with JavaScript
🏁 Pierre-Henry Soria 💡
 
Managing JavaScript Complexity in Teams - Fluent
Jarrod Overson
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Fwdays
 
Tech talk on code quality
Alexander Osin
 
How to write good quality code
Hayden Bleasel
 
20100309 01 - Maintenance and re-engineering (McCabe)
LeClubQualiteLogicielle
 
The top 5 JavaScript issues in all our codebases
Phil Nash
 
Managing and evolving JavaScript Code
Jean Carlo Emer
 
Your cyclomatic complexity is so 1.9,76
asciidisco
 
Maintainable JavaScript 2012
Nicholas Zakas
 
Sharable of qualities of clean code
Eman Mohamed
 
Cyclomatic complexity
Angelo Trozzo
 
"JS: the right way" by Mykyta Semenistyi
Binary Studio
 
Clean Code
Rajesh Kumar
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
 
15 Experts on the Art of JavaScript Programming
FusionCharts
 
HTML5 Developer Conference 2013: Javascript Insights
Ann Robson
 

More from Jarrod Overson (18)

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

Recently uploaded (20)

PDF
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PPTX
PCU Keynote at IEEE World Congress on Services 250710.pptx
Ramesh Jain
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
The Future of Artificial Intelligence (AI)
Mukul
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PCU Keynote at IEEE World Congress on Services 250710.pptx
Ramesh Jain
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
Ad

Managing JavaScript Complexity