Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

121
Transforming Front-end Disaster Code™ Into a Maintainable Masterpiece @dangribbin

description

A story about what happens when you don't design your front end for the future, and how it make it better when things get messy.

Transcript of Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Page 1: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Transforming Front-end Disaster Code™ Into a Maintainable Masterpiece

@dangribbin

Page 2: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Hi, I’m Dan Gribbin

Software Engineer brand networks

Web Development Teacher rochester brainery

Page 3: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

front end js + css + html

Page 4: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

design is critical to the development process

Page 5: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

.psd

Page 6: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

design (a plan)

Page 7: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Page 8: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

best available solution | constraints

Page 9: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

get requirements → write code → ship?

Page 10: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

get requirements → write code → ship?

no.

Page 11: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

plan system→ then write code

yep

Page 12: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Page 13: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

disaster!

Page 14: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

alpine

Page 15: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

alpine*internal name - not actual thing name

Page 16: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

initial launch

- tight deadlines- limited front-end resources

Page 17: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

- good - fast - cheap

(pick two)

Page 18: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

our plan lacked detail it needed for the future of the front-end

Page 19: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Technical Debtpoor engineering complicates future work

Page 20: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

software entropy

complexity will increase unless an effort is made against it

Page 21: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

we set our front end up for failure

Page 22: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

alpine launch → SDLC

2 week sprintsrequirements → code →release

Page 23: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

technicalDebt++;

sprint : function() {

},

Page 24: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

performance

Page 25: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

page load times

15 seconds

: (

Page 26: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

poor engineering → poor user experience

Page 27: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

effects on our page load- render-blocking scripts - loading all resources on all pages - lots of code, no minification

Page 28: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

maintainability

Page 29: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Time and ease of… - fixing a bug - developing a new feature

Page 30: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

impact?

Page 31: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

↑ feature work dev time ↓ velocity

Page 32: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

costs of an unmaintainable codebase to you and your stakeholders - time - money - frustration

Page 33: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

influences on maintainability

JS - globals - intermixed functionality

Page 34: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

SASS/CSS- long, overly specific selectors - confusing naming conventions

Page 35: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

- Styles not logically separated- Hitting IE9 selector limit

Page 36: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

moraleentropic products affect the teams that work on them

Page 37: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

our software problems don’t just belong to one person

Page 38: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

your disaster is your team’s disaster too.

Page 39: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

disasters affect morale (relationships), motivation, and productivity

Page 40: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

initial fixes

Page 41: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

initial fixes … mostly unsuccessful : /

Page 42: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

we needed a do-over

Page 43: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

1UP

Page 44: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

design!

Page 45: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

neptune

Page 46: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

much of our JavaScript wasn’t terrible, just terribly organized

Page 47: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

reorganization was top priority

Page 48: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

separation, actually

Page 49: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

MVC

Page 50: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Some MVC frameworksember, backbone, angular, knockout, dojo, YUI, CanJS, Maria, Polymer, React, cujo, Montage, Ext, Sammy, Stapes, Epitome, soma, DUEL, Kendo UI, PureMVC, Olives, Plastron, Dijon, rAppid.js, Deft, Aria, Exoskeleton, Atma, Ractive, ComponentJS

Page 51: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

MVC frameworksember, backbone, angular, knockout, dojo, YUI, CanJS, Maria, Polymer, React, cujo, Montage, Ext, Sammy, Stapes, Epitome, soma, DUEL, Kendo UI, PureMVC, Olives, Plastron, Dijon, rAppid.js, Deft, Aria, Exoskeleton, Atma, Ractive, ComponentJS

was one of these right for us?

Page 52: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

constraints time + experience

Page 53: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

AMD + RequireJSdefine([’controllers/article-controller.js’], function(ArticleController) { ArticleController.init(); } );

Page 54: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

neptune’s front end stack

Page 55: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

resultscut page load time by 80% cut JS per-page by 70% cut CSS per-page by 75%

Page 56: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

-huge maintainability gains -framework for future work -testable code

Page 57: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

where did we suck?

Page 58: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

estimation.seriously. ouch.

Page 59: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Page 60: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

awesome team-building experience

Page 61: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Things you can do right now to clean up your front end

Page 62: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Cleaning up means…-optimizing for performance -promoting maintainability -ensuring reliability

Page 63: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

optimize for performance

Page 64: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

developer tools are your best friends

Page 65: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Page 66: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Page 67: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

using an image sprite? you should be! !

one request → all your icons

Page 68: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Page 69: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

no <script>’s in your <head>

<head> <title>Bad Ideas, Inc.</title> <script src=“main.js”></script> </head>

Page 70: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Instead…… <script src=“main.js”></script> </body>

Page 71: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

use a build toolget some help optimizing

Page 72: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

In the beginning, was make

Page 73: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

then, this guy stuck his nose in

Page 74: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

a challenger appears?

Page 75: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

which one should you use?

Page 76: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

whichever one makes you happy.

Page 77: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

minimize and defer resource loading !

Page 78: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

track down unused CSSgrunt-uncss gulp-uncss

Page 79: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

minify & combinegrunt-contrib-concat grunt-contrib-uglify !

gulp-uglify gulp-concat

Page 80: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

optimize imagesgrunt-contrib-imagemin gulp-imagemin !

or, use an app like ImageOptim for manual operations

Page 81: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

cache jQuery selections in your code

var galleryItems = $(‘.gallery-item’)

http://training.bocoup.com/screencasts/the-importance-of-caching-jquery-selections/

Page 82: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

promoting maintainability

Page 83: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

a hallmark of a maintainable codebase is organization

Page 84: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

short term fixes

Page 85: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

using less/sass?be careful of the nesting trap.

Check your styles

Page 86: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

instead of !

li { display: block; } !

li img { width: 50px; }

Page 87: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

you can write !

li { display:block; !

img { width: 50px; } }

Page 88: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

which is awesome! but be careful.

Page 89: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

the following Disaster SASS™ is real code pulled from alpineviewer discretion is advised

Page 90: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

body.single { .outer-wrapper { #content{ #content_column{ ul { li{ a{ img { width: 50px !important; } } } } } } } }

Page 91: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

which compiles to…body.single .outer-wrapper #content #content_column ul li a img { width: 50px !important; }

Page 92: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

to override that, we’d need a selector with a higher specificity and importance

Page 93: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

see how this might get out of hand quickly?

Page 94: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

what if all your styles were so messy? ours were.

Page 95: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

.gallery img { width: 50px; }

how about this instead?

readable, reusable, maintainable

Page 96: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

also, don’t use !important

if you need !important, you have bigger problems to solve

Page 97: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

Collaborate on a standards document

Page 98: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

helpful for getting the entire team thinking about quality and consistency

Page 99: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

involvement increases buy in

collaboration can be an effective and positive teamwork experience

Page 100: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

long term goals

Page 101: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

organization through separation(and avoiding their opposites)

Page 102: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

modularize

JavaScript - AMD / MVCCSS - SASS/LESS

Page 103: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

modular code will help you create extensible features

Page 104: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

enforce separation of: -style -presentation -behavior

Page 105: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

use class names for on-the-fly style adjustments

Page 106: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

.addClass(‘error’)

.css(‘border’, ‘red’)

instead of

Page 107: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

✓ reusable ✓ consistent \o/ ✓ maintainable

Page 108: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

keeping JavaScript out of HTML means only having to look in one filetype

Page 109: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

onclick=“doThing()”

el.on(‘click’, doThing)

instead of

Page 110: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

navigating stakeholder relations

Page 111: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

clear, understandable communicationhow is what you’re saying relevant to business goals?

Page 112: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

the quicker solution will often lead to more cost down the road

Page 113: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

negotiation

Page 114: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

great client relationships are possible!

Page 115: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

remember

this is effort against complexity

Page 116: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

making effort against complexity benefits:

- you- future you - your team - your stakeholders

Page 117: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece
Page 118: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

make future-you love past-you

Page 119: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

the perfect !

tool/framework/library !

does not exist

Page 120: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

plan & design software for the situation at hand

Page 121: Transforming Front-End Disaster Code™ Into A Maintainable Masterpiece

thanks!

slides here: @dangribbin