56

Managing JavaScript Complexity in Teams - Fluent

Embed Size (px)

DESCRIPTION

Slides for a talk given at Fluent 2014

Citation preview

Page 1: Managing JavaScript Complexity in Teams - Fluent
Page 2: Managing JavaScript Complexity in Teams - Fluent

JARROD OVERSON

MANAGING

JAVASCRIPT@jsoversonCOMPLEXITY

Page 3: Managing JavaScript Complexity in Teams - Fluent

WHO ISRIOT

Page 4: Managing JavaScript Complexity in Teams - Fluent

WE MAKE A GAME

Page 5: Managing JavaScript Complexity in Teams - Fluent
Page 6: Managing JavaScript Complexity in Teams - Fluent

A GAME THAT GOT HUGE

Page 7: Managing JavaScript Complexity in Teams - Fluent
Page 8: Managing JavaScript Complexity in Teams - Fluent

BUT I JUST MAKE WEB

STUFF

Page 9: Managing JavaScript Complexity in Teams - Fluent

THE WEB IS AWESOME

BUT, WE NEED TO TALK.

Page 10: Managing JavaScript Complexity in Teams - Fluent

PEAK OF INFLATED EXPECTATIONS

TROUGH OF DISILLUSIONMENT

HTML5

MOBILE WEB APPS

PLATEAU OF REALITY

The Web Platform

Page 11: Managing JavaScript Complexity in Teams - Fluent

2008 2009 2010 2011 2012 2013 2014 NOW WE CAN MOVE ON

Page 12: Managing JavaScript Complexity in Teams - Fluent

STEP TWO

STEP ONE

STEP THREE

HONESTY

Page 13: Managing JavaScript Complexity in Teams - Fluent

DYNAMIC LANGUAGES ARE MESSY.1.

COMING TO TERMS

Combine the normal pitfalls with !• Immature IDEs and tooling • Wildly variable module styles • Best practices that vary monthly • Similar yet vastly different ecosystems

Page 14: Managing JavaScript Complexity in Teams - Fluent

THE TALENT POOL IS NUTS.2.

COMING TO TERMS

Web Platform Engineers

jQuery Experts

Made a menu fade in once

async wat?

Page 15: Managing JavaScript Complexity in Teams - Fluent

PROGRESS IS STAGGERING.3.

COMING TO TERMS

It’s hard to keep up. !Everyone has an opinion. !The future doesn’t get here all at once.

Page 16: Managing JavaScript Complexity in Teams - Fluent

REFACTORING JAVASCRIPT IS NOT EASY.4.

COMING TO TERMS

Callback hell is more than just deep nesting. !IDEs can’t help much yet. !Flexibility is more important here than anywhere.

Page 17: Managing JavaScript Complexity in Teams - Fluent

THE WEB IS HARD!5.

COMING TO TERMS

Web applications are not solved. !Even the giants pivot and backtrack. !So many solutions just don’t exist yet.

Page 18: Managing JavaScript Complexity in Teams - Fluent

WHY ARE WE EVEN HERE?

Page 19: Managing JavaScript Complexity in Teams - Fluent

JAVASCRIPTWON

Page 20: Managing JavaScript Complexity in Teams - Fluent

AND THIS ISN’T EVEN ITS FINAL FORM

Page 21: Managing JavaScript Complexity in Teams - Fluent

STEP ONE

STEP THREE

ACCEPTANCESTEP TWO

Page 22: Managing JavaScript Complexity in Teams - Fluent

RESPECT YOUR JAVASCRIPT

Page 23: Managing JavaScript Complexity in Teams - Fluent

‣ Indentation style ‣ Line length ‣ Quote styles ‣ Naming conventions ‣ Curly brace placement ‣ Directory structure ‣ Everything

GET EVERYONE TOGETHER

ENFORCE

‣ Use community tools ‣ Grunt ‣ Gulp ‣ JSHint ‣ etc

‣ Warnings === errors ‣ Make it hard to be

wrong

DOCUMENT

‣ Treat docs as code ‣ Make it

‣ easy to find ‣ easy to read ‣ easy to update ‣ easy to discuss

‣ Use github!

AGREE

Page 24: Managing JavaScript Complexity in Teams - Fluent

RESPECT THE COMMUNITY

Page 25: Managing JavaScript Complexity in Teams - Fluent

RESPECT THE COMMUNITY

>90% use last comma

http://sideeffect.kr/popularconvention/#javascript

>80% indent with spaces

>55% use single quotes

https://github.com/Seravo/js-winning-style

https://github.com/rwaldron/idiomatic.js/

Research public style guides

Page 26: Managing JavaScript Complexity in Teams - Fluent

VIOLATIONS BEGET VIOLATIONS

ALLOW 1 & ALLOW 1,000

Page 27: Managing JavaScript Complexity in Teams - Fluent

KNOW YOUR OPTIONS

JSHINT

ESLINT

JSCS

Community-driven JSLint fork. High configurability.

JSHint alternative. High configurability.

Code style checker. Good complement to JSHint.

WHAT ABOUT JSLINT AND CLOSURE LINTER?

Page 28: Managing JavaScript Complexity in Teams - Fluent

KNOW YOUR OPTION’S OPTIONS

Page 29: Managing JavaScript Complexity in Teams - Fluent

BE AGGRESSIVE. YOUR FEELINGS WILL GET HURT.

Page 30: Managing JavaScript Complexity in Teams - Fluent

SMART DEVIATION IS OK AND EXPECTED

!!function fn(param) { /*jshint eqeqeq:false*/ ! if (param == 42) return; !}

Page 31: Managing JavaScript Complexity in Teams - Fluent

SET NUMERIC LIMITS

"maxparams" : 4 "maxdepth" : 4 "maxstatements" : 20 "maxlen" : 100 "maxcomplexity" : 7

Page 32: Managing JavaScript Complexity in Teams - Fluent

WHAT ISCOMPLEXITY?CYCLOMATIC

Page 33: Managing JavaScript Complexity in Teams - Fluent

TECHNICALLY

CYCLOMATIC COMPLEXITY IS THE NUMBER OF PATHS

THROUGH YOUR CODE

Page 34: Managing JavaScript Complexity in Teams - Fluent

PRACTICALLY

CYCLOMATIC COMPLEXITY IS HOW HARD

YOUR CODE IS TO TEST

Page 35: Managing JavaScript Complexity in Teams - Fluent

COMPLEXITY : 1

!function main(a) { !}

Page 36: Managing JavaScript Complexity in Teams - Fluent

COMPLEXITY : 2

function main(a) { if (a > 5) { } }

Page 37: Managing JavaScript Complexity in Teams - Fluent

COMPLEXITY : ?

function main(a) { if (a > 5) { ! } else { ! } }

Page 38: Managing JavaScript Complexity in Teams - Fluent

COMPLEXITY : 3

function main(a) { if (a > 10) { ! } else if(a > 5) { ! } }

Page 39: Managing JavaScript Complexity in Teams - Fluent

COMPLEXITY : ?

function main(a) { if (a > 5) { if (a > 10) { ! } } }

Page 40: Managing JavaScript Complexity in Teams - Fluent

COMPLEXITY : 7function main(a) { if (a) { } else if (a) { } ! if (other) { } ! for (var i = 0; i < a; i++) { if (i % 2) { } else if (i % 3) { } } }

Page 41: Managing JavaScript Complexity in Teams - Fluent

I KNOW WHAT YOU’RE THINKINGI’M

GOING TO MAKE THE MOST AMAZING .JSHINTRC

EVER

Page 42: Managing JavaScript Complexity in Teams - Fluent

BUT IT’S NOT THAT EASY

Page 43: Managing JavaScript Complexity in Teams - Fluent

STEP TWO

STEP ONE

PERSEVERANCE

STEP THREE

Page 44: Managing JavaScript Complexity in Teams - Fluent

VISUALIZE YOUR GOAL

Page 45: Managing JavaScript Complexity in Teams - Fluent

VISUALIZE YOUR CODE

PLATOYour friendly, neighborhood

philosopher

PLATOJS.ORG

Page 46: Managing JavaScript Complexity in Teams - Fluent

YOUR GOAL

Page 47: Managing JavaScript Complexity in Teams - Fluent

THE PATH

FILES PASSING IDEAL SETTINGS

Page 48: Managing JavaScript Complexity in Teams - Fluent

THE PATH

FILES IN NEED OF LARGER REFACTOR

Page 49: Managing JavaScript Complexity in Teams - Fluent

THE PATH

QUICK WINS

Page 50: Managing JavaScript Complexity in Teams - Fluent

OTHER METRICS

HALSTEAD METRICS

CODE COVERAGE

MAINTAINABILITY

LINES OF CODE

Page 51: Managing JavaScript Complexity in Teams - Fluent

MAINTAINABILITY?fn(averageEffort, averageComplexity, averageLines);

fn(difficulty, volume)fn(length, vocabulary)

fn(uniqueOperators, totalOperands, uniqueOperands)

fn(uniqueOperators, uniqueOperands)

fn(totalOperators, totalOperands)

Page 52: Managing JavaScript Complexity in Teams - Fluent

MAURICE HALSTEADHALSTEAD METRICS

PHIL BOOTH ARIYA HIDAYATTHOMAS MCCABECYCLOMATIC COMPLEXITY COMPLEXITY ANALYSIS JS STATIC ANALYSIS

WHO TO BLAME

Page 53: Managing JavaScript Complexity in Teams - Fluent

THE UNEXAMINED CODE IS NOT WORTH RELEASING

”- SOCRATES

Page 54: Managing JavaScript Complexity in Teams - Fluent

CODE IS NOT JUST LOGIC.CODE IS AN API.

TREAT IT LIKE ONE.

Page 55: Managing JavaScript Complexity in Teams - Fluent

Recap

PERSEVERANCE

The web has unique value, it’s not a cheap alternative to native apps.

Embrace the web and JavaScript as your platform.

Create new tools. Automate & visualize everything.

ACCEPTANCE

HONESTY

Page 56: Managing JavaScript Complexity in Teams - Fluent

JARROD OVERSON

MANAGING

JAVASCRIPT@jsoversonCOMPLEXITY

Office hours @ 2:10pm