+ All Categories
Home > Technology > Building Rich User Experiences Without JavaScript Spaghetti

Building Rich User Experiences Without JavaScript Spaghetti

Date post: 02-Nov-2014
Category:
Upload: jared-faris
View: 1,471 times
Download: 0 times
Share this document with a friend
Description:
Codemash 2013 slide deck
Popular Tags:
102
Building Rich User Experiences without JavaScript Spaghetti by Jared Faris @jaredthenerd jaredthenerd.com
Transcript
Page 1: Building Rich User Experiences Without JavaScript Spaghetti

Building Rich User Experienceswithout

JavaScript Spaghetti

by Jared Faris@jaredthenerd

jaredthenerd.com

Page 2: Building Rich User Experiences Without JavaScript Spaghetti

About me

Page 3: Building Rich User Experiences Without JavaScript Spaghetti
Page 4: Building Rich User Experiences Without JavaScript Spaghetti

Traditional Web Development

Web assets are created last to glue together things.

Stakeholders, architects and developers define the

“real” application.

The application is built model first (from the DB up).

Page 5: Building Rich User Experiences Without JavaScript Spaghetti

Traditional Web Development

The design is focused on making the CRUD pretty, not usable

The design of the UI/UX happens in Photoshop... if it happens at all.

Most of the application is just CRUD.

Page 6: Building Rich User Experiences Without JavaScript Spaghetti

We have a widgetstable

We need awidgetsscreen!

Page 7: Building Rich User Experiences Without JavaScript Spaghetti

Traditional Web Development

“Advanced” web developers write lots of unorganized bits of jQuery.

Most user interactions result in lots of POSTS back

to the server.

JavaScripts are something someone download from hotscripts.com.

Page 8: Building Rich User Experiences Without JavaScript Spaghetti

We have a widgetstable

We need awidgetsscreen!

Make sure youadd a date picker

And a lighbox!

Page 9: Building Rich User Experiences Without JavaScript Spaghetti

JavaScript: Not a “Real” Language

Patternless design.

Built as a glue to bind pieces together, not as a core component of the application.

Not thought through like server-side code.

Page 10: Building Rich User Experiences Without JavaScript Spaghetti

Not Real? Why?

A lot of development tools traditionally hid the JS behind configurable controls.

Libraries like jQuery make it really easy to pile on lots of little events.

JS used to be a tool for doing image rollovers.

Page 11: Building Rich User Experiences Without JavaScript Spaghetti

Is That A Problem?

In a workflow-based application it falls apart.

For form-based apps that was mostly fine.

Customized, reusable controls and a modular application design needs something more.

Page 12: Building Rich User Experiences Without JavaScript Spaghetti

Questions So Far?

Page 13: Building Rich User Experiences Without JavaScript Spaghetti

A Typical Product LifecycleSomewhat dramatized...

Page 14: Building Rich User Experiences Without JavaScript Spaghetti

Designer Developer

Page 15: Building Rich User Experiences Without JavaScript Spaghetti

We need this feature

Page 16: Building Rich User Experiences Without JavaScript Spaghetti

I got this

Page 17: Building Rich User Experiences Without JavaScript Spaghetti

?

Page 18: Building Rich User Experiences Without JavaScript Spaghetti

Tweaking time...

Page 19: Building Rich User Experiences Without JavaScript Spaghetti

I got anothergreat idea

Page 20: Building Rich User Experiences Without JavaScript Spaghetti

Now you tellme

Page 21: Building Rich User Experiences Without JavaScript Spaghetti

The developer bolts on some more code

Page 22: Building Rich User Experiences Without JavaScript Spaghetti

And anotherthing...

Page 23: Building Rich User Experiences Without JavaScript Spaghetti

grrr

Page 24: Building Rich User Experiences Without JavaScript Spaghetti

We don’t ‘really’

need this

Page 25: Building Rich User Experiences Without JavaScript Spaghetti

Uh, yeah wedo

Page 26: Building Rich User Experiences Without JavaScript Spaghetti
Page 27: Building Rich User Experiences Without JavaScript Spaghetti

The developer bolts on some more code

Page 28: Building Rich User Experiences Without JavaScript Spaghetti

Some time passes

‘Some time’ is defined as: Just long enough that the developer doesn’t remember

exactly how his original code works.

Page 29: Building Rich User Experiences Without JavaScript Spaghetti

I’ve got a new feature

Page 30: Building Rich User Experiences Without JavaScript Spaghetti

Angry developerscan really do this.IT managers be

warned.

Page 31: Building Rich User Experiences Without JavaScript Spaghetti

Protective Beret

Page 32: Building Rich User Experiences Without JavaScript Spaghetti

More messy code

Page 33: Building Rich User Experiences Without JavaScript Spaghetti

The last bug

Oh wait, one more

Page 34: Building Rich User Experiences Without JavaScript Spaghetti

Finally

Page 35: Building Rich User Experiences Without JavaScript Spaghetti

The next day...

Page 36: Building Rich User Experiences Without JavaScript Spaghetti
Page 37: Building Rich User Experiences Without JavaScript Spaghetti

Two weeks pass.

Page 38: Building Rich User Experiences Without JavaScript Spaghetti

I’ve got a new feature Gahh!

Page 39: Building Rich User Experiences Without JavaScript Spaghetti
Page 40: Building Rich User Experiences Without JavaScript Spaghetti

No developers were harmed in the makingof this dramatic reenactment.

Page 41: Building Rich User Experiences Without JavaScript Spaghetti

Poor design patterns + crappy code

= JavaScript spaghetti

Page 42: Building Rich User Experiences Without JavaScript Spaghetti

Why does this happen?

Page 43: Building Rich User Experiences Without JavaScript Spaghetti

Some Reasons

• JavaScript isn’t real code• We don’t treat client side things as real features• We can’t easily test it• We don’t like writing it• It behaves differently in different browsers*

* Or at least it used to

Page 44: Building Rich User Experiences Without JavaScript Spaghetti

This really all boils down to one thing.

We developers suck.

Page 45: Building Rich User Experiences Without JavaScript Spaghetti

Three JavaScript Principles

Push events, not state

Write small, discrete bits of code

Decouple everything

Page 46: Building Rich User Experiences Without JavaScript Spaghetti

Decouple Everything

Apply your OO best practices here too.

Remove dependencies between objects.

Start thinking about UI pieces as individual JS objects.

Page 47: Building Rich User Experiences Without JavaScript Spaghetti

Small Chunks Of Code

Even if you don’t test everything, learning how towrite testable code means learning

how to write better code

Put the rest of the stuff in classes that you can test.

Separate DOM dependent stuff into a single layer.

Page 48: Building Rich User Experiences Without JavaScript Spaghetti

Push Events, Not State

Inform other controls that “X happened to Y”, not “Y is in X state”

Let controls worry about their own state.

Know about the Law of Demeter.

Page 49: Building Rich User Experiences Without JavaScript Spaghetti

Writing Better JavaScript

Find the tools that make your life easier.

Use the design patterns you already know.

Leverage language features whether JS has them or not.

Page 50: Building Rich User Experiences Without JavaScript Spaghetti

Language Features

Keep state inside of the objects and expose methods.

Objects have state and behavior.

JavaScript loves its objects. Create them to representpage elements.

Page 51: Building Rich User Experiences Without JavaScript Spaghetti

Language “Features”

Consider using namespaces.

Page 52: Building Rich User Experiences Without JavaScript Spaghetti

JavaScript Namespacing

Page 53: Building Rich User Experiences Without JavaScript Spaghetti

Language “Features”

Use inheritance or faux subclassing.

Consider using namespaces.

Page 54: Building Rich User Experiences Without JavaScript Spaghetti

JavaScript Prototyping

Page 55: Building Rich User Experiences Without JavaScript Spaghetti

Coffeescript Classes

Page 56: Building Rich User Experiences Without JavaScript Spaghetti

Language “Features”

Pass JSON around asynchronously.

Use inheritance or faux subclassing.

Consider using namespaces.

Page 57: Building Rich User Experiences Without JavaScript Spaghetti

Design Patterns

Page 58: Building Rich User Experiences Without JavaScript Spaghetti

Mediator Pattern

"The essence of the Mediator Pattern is to "Define an object that encapsulates how a set of objects interact. Mediator promotes

loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently."

-Design Patterns: Elements of Reusable Object-Oriented Software

Page 59: Building Rich User Experiences Without JavaScript Spaghetti

NavControlMediator

itemSelected()

Events from some other object

unselectAll()

Page 60: Building Rich User Experiences Without JavaScript Spaghetti

Observer Pattern

"Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and

updated automatically."

-Design Patterns: Elements of Reusable Object-Oriented Software

Think jQuery $(‘.something’).click()

Page 61: Building Rich User Experiences Without JavaScript Spaghetti

NavControlMediator

itemSelected()

Events from some other object

unselectAll()

viewModel

Page 62: Building Rich User Experiences Without JavaScript Spaghetti

Tools & Frameworks

Page 63: Building Rich User Experiences Without JavaScript Spaghetti

Knockout

Magic.

Setup a template with some markup binding it.

Setup a JavaScript object with some KO code.

Page 64: Building Rich User Experiences Without JavaScript Spaghetti
Page 65: Building Rich User Experiences Without JavaScript Spaghetti
Page 66: Building Rich User Experiences Without JavaScript Spaghetti

A KO Warning

It’s really easy to go overboard with KO events.

I prefer to use KO for the VM binding (observables and computeds) but rely on

jQuery for events.

jQuery’s .on() binding and a good understanding of ‘this’ makes for much

cleaner events.

Page 67: Building Rich User Experiences Without JavaScript Spaghetti

Backbone

Views help you control the visual rendering.

Routers help you organize page flow.

While KO is Model < > View magic, Backbone is structure.

Models help you keep track of state.

Page 68: Building Rich User Experiences Without JavaScript Spaghetti

Backbone Use Case

Page 69: Building Rich User Experiences Without JavaScript Spaghetti

Backbone vs Knockout?

Page 70: Building Rich User Experiences Without JavaScript Spaghetti

Pub/Sub + Fairy Dust = Service Bus

Pub/Sub is great to make sure events propagate.It starts to get brittle with lots of different controls.

Page 71: Building Rich User Experiences Without JavaScript Spaghetti

Way Too Much Pubbing and Subbing

Page 72: Building Rich User Experiences Without JavaScript Spaghetti

Service Bus

Your controls are then only coupled to a single thing.

Controls that want to communicate speak through it.

A service bus is another layer that sits outside controls.

Page 73: Building Rich User Experiences Without JavaScript Spaghetti

Postal.js

Page 74: Building Rich User Experiences Without JavaScript Spaghetti

Service Bus + Mediator

• Controls no longer need to know about others.• We can remove/replace controls individually.• We can add controls that listen to the same events without modifying the publisher.• We can re-use pieces more easily because they work in a standard way.

Page 75: Building Rich User Experiences Without JavaScript Spaghetti

NavControlMediator

itemSelected()

Events from some other object

unselectAll()

viewModel

ReportMediator

itemChanged()

unselectAll()

viewModel

Service Bus

Page 76: Building Rich User Experiences Without JavaScript Spaghetti

NavControlMediator

itemSelected()

Events from some other object

unselectAll()

viewModel

ReportMediator

itemChanged()

unselectAll()

viewModel

Service Bus

HistoryControl

Page 77: Building Rich User Experiences Without JavaScript Spaghetti

Service Bus

TeamControl

Gets team changed message, makes AJAX

call for this team’s data, rewrites team with template

No view model

Page 78: Building Rich User Experiences Without JavaScript Spaghetti

Service Bus

Page 79: Building Rich User Experiences Without JavaScript Spaghetti

Testing

Try to have layers of your application’s JS that don’t touch any HTML elements.

Store data in models inside individual controls and test that published messages change the state of those models correctly.

Page 80: Building Rich User Experiences Without JavaScript Spaghetti

Underscore

Page 81: Building Rich User Experiences Without JavaScript Spaghetti

Underscore

Page 82: Building Rich User Experiences Without JavaScript Spaghetti

Underscore w/ Coffeescript

Page 83: Building Rich User Experiences Without JavaScript Spaghetti

What Else?

I don’t have a third bullet point here

Browser Debuggers

Templates

Page 84: Building Rich User Experiences Without JavaScript Spaghetti

Examples

Page 85: Building Rich User Experiences Without JavaScript Spaghetti

Questions?

Page 86: Building Rich User Experiences Without JavaScript Spaghetti

A Typical Product LifecycleRound Two

Page 87: Building Rich User Experiences Without JavaScript Spaghetti

We need this feature

Page 88: Building Rich User Experiences Without JavaScript Spaghetti

I got a fewquestions

Page 89: Building Rich User Experiences Without JavaScript Spaghetti

?

Page 90: Building Rich User Experiences Without JavaScript Spaghetti

Tweaking time...

Page 91: Building Rich User Experiences Without JavaScript Spaghetti

I got anothergreat idea

Page 92: Building Rich User Experiences Without JavaScript Spaghetti

Ok, Cool

Page 93: Building Rich User Experiences Without JavaScript Spaghetti

And anotherthing...

Page 94: Building Rich User Experiences Without JavaScript Spaghetti

Done.

Page 95: Building Rich User Experiences Without JavaScript Spaghetti

Two weeks pass...

Page 96: Building Rich User Experiences Without JavaScript Spaghetti

I’ve got a new feature

Page 97: Building Rich User Experiences Without JavaScript Spaghetti

No worries.

Page 98: Building Rich User Experiences Without JavaScript Spaghetti

Wha? Ohhhk.

Page 99: Building Rich User Experiences Without JavaScript Spaghetti

A short time later...

Page 100: Building Rich User Experiences Without JavaScript Spaghetti
Page 101: Building Rich User Experiences Without JavaScript Spaghetti

Special thanks to

He did the frame art

Blame me foreverything else


Recommended