Slide 1

Slide 1 text

I’M AFRAID OF JAVASCRIPT

Slide 2

Slide 2 text

AHOY FRIEND!

Slide 3

Slide 3 text

@searls

Slide 4

Slide 4 text

every developer FEAR shares a secret

Slide 5

Slide 5 text

...my code sucks? WHAT IF

Slide 6

Slide 6 text

BUT WE COPE POORLY

Slide 7

Slide 7 text

stupid things we do: we pair less often we share less often we change less often

Slide 8

Slide 8 text

we defer too often stupid things we do: we defend too often we deflect too often

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

say it with me: “My Code Sucks”

Slide 11

Slide 11 text

say it with me: “My Code Sucks” sometimes

Slide 12

Slide 12 text

say it with me: “My Code Sucks” maybe

Slide 13

Slide 13 text

say it with me: “My Code Sucks” if you say so

Slide 14

Slide 14 text

SO FIGHT THE FEAR

Slide 15

Slide 15 text

and fail PROUDLY

Slide 16

Slide 16 text

JavaScript! as we talk

Slide 17

Slide 17 text

just throw away classes & interfaces strong typing most keywords ORACLE (or MS) JAVASCRIPT IS BASICALLY JAVA (OR C#)

Slide 18

Slide 18 text

JAVASCRIPT IS BASICALLY JAVA (OR C#) and then add 1st class functions object literals tons of gotchas healthy open source libs

Slide 19

Slide 19 text

CoffeeScript! as we talk

Slide 20

Slide 20 text

just throw away mixins & modules method_missing def and end easy debugging COFFEESCRIPT IS BASICALLY RUBY (OR PYTHON)

Slide 21

Slide 21 text

and then add list comprehension object literals existential operator huge install base COFFEESCRIPT IS BASICALLY RUBY (OR PYTHON)

Slide 22

Slide 22 text

YOUR PAIR

Slide 23

Slide 23 text

YOUR PAIR GRABS A CARD

Slide 24

Slide 24 text

Slide 25

Slide 25 text

so you say, “let’s use JavaScript!”

Slide 26

Slide 26 text

but your pair’s all like

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

she says, “Start with the simplest thing that could possibly work!”

Slide 29

Slide 29 text

!== simplest easiest

Slide 30

Slide 30 text

!== simplest most familiar

Slide 31

Slide 31 text

!== simplest calling a server to sort an array & render HTML for you

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

so simple!‚

Slide 37

Slide 37 text

WHAT GOOD ARE YOUR SERVERS?

Slide 38

Slide 38 text

security persisting data analytics model behavior HTML templating routing users proxying UI events things servers are great at

Slide 39

Slide 39 text

security persisting data analytics proxying things servers are great at

Slide 40

Slide 40 text

model behavior HTML templating routing users UI events it’s simpler when the client handles

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

DON’T PANIC

Slide 43

Slide 43 text

YOU’VE GOT THIS

Slide 44

Slide 44 text

model behavior HTML templating routing users UI events

Slide 45

Slide 45 text

jasmine

Slide 46

Slide 46 text

describe()

Slide 47

Slide 47 text

it()

Slide 48

Slide 48 text

expect().toBe()

Slide 49

Slide 49 text

expect().not.toBe()

Slide 50

Slide 50 text

beforeEach()

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

run it yourself! http://is.gd/logic1 describe('CallList', function() { });

Slide 55

Slide 55 text

run it yourself! http://is.gd/logic1 describe('CallList', function() { describe("#moveUp", function() { }); });

Slide 56

Slide 56 text

run it yourself! http://is.gd/logic1 describe('CallList', function() { describe("#moveUp", function() { context("moving up C", function() { }); }); });

Slide 57

Slide 57 text

run it yourself! http://is.gd/logic1 describe('CallList', function() { describe("#moveUp", function() { context("moving up C", function() { it("places it above B", function() { }); }); }); });

Slide 58

Slide 58 text

run it yourself! http://is.gd/logic1 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() { it("places it above B", function() { }); }); }); });

Slide 59

Slide 59 text

run it yourself! http://is.gd/logic1 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() { beforeEach(function() { subject.moveUp('C'); }); it("places it above B", function() { }); }); }); });

Slide 60

Slide 60 text

run it yourself! http://is.gd/logic1 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() { beforeEach(function() { subject.moveUp('C'); }); it("places it above B", function() { expect(subject.get('items')).toEqual(['A','C','B']); }); }); }); });

Slide 61

Slide 61 text

run it yourself! http://is.gd/logic1

Slide 62

Slide 62 text

run it yourself! http://is.gd/logic1 window.CallList = Backbone.Model.extend({ });

Slide 63

Slide 63 text

run it yourself! http://is.gd/logic1 window.CallList = Backbone.Model.extend({ moveUp: function(item) { } });

Slide 64

Slide 64 text

run it yourself! http://is.gd/logic1 window.CallList = Backbone.Model.extend({ moveUp: function(item) { this.set({items: ['A','C','B']}); } });

Slide 65

Slide 65 text

run it yourself! http://is.gd/logic1

Slide 66

Slide 66 text

run it yourself! http://is.gd/logic2 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() {...}); context("moving up B", function() { }); }); });

Slide 67

Slide 67 text

run it yourself! http://is.gd/logic2 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() {...}); context("moving up B", function() { it("places B above A", function() { }); }); }); });

Slide 68

Slide 68 text

run it yourself! http://is.gd/logic2 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() {...}); context("moving up B", function() { beforeEach(function() { subject.moveUp('B'); }); it("places B above A", function() { }); }); }); });

Slide 69

Slide 69 text

run it yourself! http://is.gd/logic2 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() {...}); context("moving up B", function() { beforeEach(function() { subject.moveUp('B'); }); it("places B above A", function() { expect(subject.get('items')).toEqual(['B','A','C']); }); }); }); });

Slide 70

Slide 70 text

run it yourself! http://is.gd/logic2

Slide 71

Slide 71 text

run it yourself! http://is.gd/logic2 window.CallList = Backbone.Model.extend({ moveUp: function(item) { var items = this.get('items'); } });

Slide 72

Slide 72 text

run it yourself! http://is.gd/logic2 window.CallList = Backbone.Model.extend({ moveUp: function(item) { var items = this.get('items'); var index = _(items).indexOf(item) - 1; } });

Slide 73

Slide 73 text

run it yourself! http://is.gd/logic2 window.CallList = Backbone.Model.extend({ moveUp: function(item) { var items = this.get('items'); var index = _(items).indexOf(item) - 1; items.splice(index,2,item,items[index]); } });

Slide 74

Slide 74 text

run it yourself! http://is.gd/logic2

Slide 75

Slide 75 text

run it yourself! http://is.gd/logic3 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() {...}); context("moving up B", function() {...}); context("moving up A", function() { }); }); });

Slide 76

Slide 76 text

run it yourself! http://is.gd/logic3 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() {...}); context("moving up B", function() {...}); context("moving up A", function() { it("leaves the items as they were", function() { }); }); }); });

Slide 77

Slide 77 text

run it yourself! http://is.gd/logic3 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() {...}); context("moving up B", function() {...}); context("moving up A", function() { beforeEach(function() { subject.moveUp('A'); }); it("leaves the items as they were", function() { }); }); }); });

Slide 78

Slide 78 text

run it yourself! http://is.gd/logic3 describe('CallList', function() { var subject; beforeEach(function() { subject = new CallList({items: ['A','B','C']}); }); describe("#moveUp", function() { context("moving up C", function() {...}); context("moving up B", function() {...}); context("moving up A", function() { beforeEach(function() { subject.moveUp('A'); }); it("leaves the items as they were", function() { expect(subject.get('items')).toEqual(['A','B','C']); }); }); }); });

Slide 79

Slide 79 text

run it yourself! http://is.gd/logic3

Slide 80

Slide 80 text

run it yourself! http://is.gd/logic3

Slide 81

Slide 81 text

run it yourself! http://is.gd/logic3 window.CallList = Backbone.Model.extend({ moveUp: function(item) { var items = this.get('items'); var index = _(items).indexOf(item) - 1; items.splice(index,2,item,items[index]); } });

Slide 82

Slide 82 text

run it yourself! http://is.gd/logic3 window.CallList = Backbone.Model.extend({ moveUp: function(item) { var items = this.get('items'); var index = _(items).indexOf(item) - 1; if(index >= 0) { items.splice(index,2,item,items[index]); } } });

Slide 83

Slide 83 text

run it yourself! http://is.gd/logic3

Slide 84

Slide 84 text

model behavior HTML templating routing users UI events

Slide 85

Slide 85 text

backbone

Slide 86

Slide 86 text

view model 00 00 UI Event Occurs! “click .move-up”

Slide 87

Slide 87 text

view model 00 00 this.model.moveUp(‘C’)

Slide 88

Slide 88 text

view model 00 00 this.set({ items:[‘A’,’C’,’B’] });

Slide 89

Slide 89 text

view model 00 00 Model Event Fired! “change:items”

Slide 90

Slide 90 text

view model 00 00 change:items bound! this.render()

Slide 91

Slide 91 text

view model 00 00 01

Slide 92

Slide 92 text

and now you know backbone!

Slide 93

Slide 93 text

run it yourself! http://is.gd/view1 describe "CallListView", ->

Slide 94

Slide 94 text

HOLD THE ☎

Slide 95

Slide 95 text

CoffeeScript

Slide 96

Slide 96 text

function(){}

Slide 97

Slide 97 text

->

Slide 98

Slide 98 text

() {} ;

Slide 99

Slide 99 text

optional

Slide 100

Slide 100 text

this.foo

Slide 101

Slide 101 text

@foo

Slide 102

Slide 102 text

employee.group.department.company.yuck

Slide 103

Slide 103 text

employee.group.department.company.yuck TypeError: 'undefined' is not an object

Slide 104

Slide 104 text

?. enter the existential operator

Slide 105

Slide 105 text

employee?.group?.department?.company?.yuck

Slide 106

Slide 106 text

employee?.group?.department?.company?.yuck now, merely “undefined”

Slide 107

Slide 107 text

employee?.group?.department?.company?.yuck now, merely “undefined” It’s Demeter-tastic!

Slide 108

Slide 108 text

run it yourself! http://is.gd/view1 describe "CallListView", -> describe "events", ->

Slide 109

Slide 109 text

run it yourself! http://is.gd/view1 describe "CallListView", -> describe "events", -> it "binds to up-arrow clicks", ->

Slide 110

Slide 110 text

run it yourself! http://is.gd/view1 describe "CallListView", -> beforeEach -> @subject = new CallListView describe "events", -> it "binds to up-arrow clicks", -> expect(@subject.events).toEqual "click .up-arrow": 'moveUp'

Slide 111

Slide 111 text

run it yourself! http://is.gd/view1

Slide 112

Slide 112 text

run it yourself! http://is.gd/view1 class CallListView extends Backbone.View

Slide 113

Slide 113 text

run it yourself! http://is.gd/view1 class CallListView extends Backbone.View events: "click .up-arrow": "moveUp"

Slide 114

Slide 114 text

run it yourself! http://is.gd/view1

Slide 115

Slide 115 text

run it yourself! http://is.gd/view1

Slide 116

Slide 116 text

run it yourself! http://is.gd/view1 class CallListView extends Backbone.View events: "click .up-arrow": "moveUp" moveUp: ->

Slide 117

Slide 117 text

run it yourself! http://is.gd/view1

Slide 118

Slide 118 text

jasmine spies

Slide 119

Slide 119 text

spyOn(object,‘method’)

Slide 120

Slide 120 text

spyOn(object,‘method’) jasmine.createSpy(‘method’)

Slide 121

Slide 121 text

spyOn(object,‘method’) jasmine.createSpy(‘method’) jasmine.createSpyObj(‘Name’,[‘method1’,‘method2’])

Slide 122

Slide 122 text

stubbing

Slide 123

Slide 123 text

spyOn(panda,‘isHappy’)

Slide 124

Slide 124 text

spyOn(panda,‘isHappy’) panda.isHappy.andReturn(true)

Slide 125

Slide 125 text

spyOn(panda,‘isHappy’) panda.isHappy.andReturn(true) panda.isHappy()

Slide 126

Slide 126 text

spyOn(panda,‘isHappy’) panda.isHappy.andReturn(true) panda.isHappy() returns true

Slide 127

Slide 127 text

verifying

Slide 128

Slide 128 text

spyOn(panda,‘feed’)

Slide 129

Slide 129 text

spyOn(panda,‘feed’) panda.feed()

Slide 130

Slide 130 text

spyOn(panda,‘feed’) expect(panda.feed).toHaveBeenCalled() panda.feed()

Slide 131

Slide 131 text

run it yourself! http://is.gd/view2 describe "CallListView", -> beforeEach -> @subject = new CallListView describe "events", -> it "binds to up-arrow clicks", -> expect(@subject.events).toEqual "click .up-arrow": 'moveUp' describe "#moveUp", -> it "tells the model to move up the arrow's text", ->

Slide 132

Slide 132 text

run it yourself! http://is.gd/view2 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> it "binds to up-arrow clicks", -> expect(@subject.events).toEqual "click .up-arrow": 'moveUp' describe "#moveUp", -> it "tells the model to move up the arrow's text", ->

Slide 133

Slide 133 text

run it yourself! http://is.gd/view2 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> it "binds to up-arrow clicks", -> expect(@subject.events).toEqual "click .up-arrow": 'moveUp' describe "#moveUp", -> beforeEach -> $upArrow = $(@subject.el).inject('up-arrow').text('C') it "tells the model to move up the arrow's text", ->

Slide 134

Slide 134 text

run it yourself! http://is.gd/view2 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> it "binds to up-arrow clicks", -> expect(@subject.events).toEqual "click .up-arrow": 'moveUp' describe "#moveUp", -> beforeEach -> $upArrow = $(@subject.el).inject('up-arrow').text('C') it "tells the model to move up the arrow's text", -> jasmine-fixture https://github.com/searls/jasmine-fixture

Slide 135

Slide 135 text

run it yourself! http://is.gd/view2 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> it "binds to up-arrow clicks", -> expect(@subject.events).toEqual "click .up-arrow": 'moveUp' describe "#moveUp", -> beforeEach -> $upArrow = $(@subject.el).inject('up-arrow').text('C') @subject.moveUp target: $upArrow[0] it "tells the model to move up the arrow's text", ->

Slide 136

Slide 136 text

run it yourself! http://is.gd/view2 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> it "binds to up-arrow clicks", -> expect(@subject.events).toEqual "click .up-arrow": 'moveUp' describe "#moveUp", -> beforeEach -> $upArrow = $(@subject.el).inject('up-arrow').text('C') @subject.moveUp target: $upArrow[0] it "tells the model to move up the arrow's text", -> expect(@model.moveUp).toHaveBeenCalledWith 'C'

Slide 137

Slide 137 text

run it yourself! http://is.gd/view2

Slide 138

Slide 138 text

run it yourself! http://is.gd/view2 class CallListView extends Backbone.View events: "click .up-arrow": "moveUp" moveUp: (e) -> @model.moveUp $(e.target).text()

Slide 139

Slide 139 text

run it yourself! http://is.gd/view2

Slide 140

Slide 140 text

HTML templating model behavior routing users UI events

Slide 141

Slide 141 text

run it yourself! http://is.gd/template1 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> ... describe "#moveUp", -> ... describe "#render", -> it "renders the template with the model's JSON", ->

Slide 142

Slide 142 text

run it yourself! http://is.gd/template1 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> ... describe "#moveUp", -> ... describe "#render", -> it "renders the template with the model's JSON", -> expect($(@subject.el)).toHaveHtml('HTML for bar')

Slide 143

Slide 143 text

run it yourself! http://is.gd/template1 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> ... describe "#moveUp", -> ... describe "#render", -> it "renders the template with the model's JSON", -> expect($(@subject.el)).toHaveHtml('HTML for bar') jasmine-jquery https://github.com/velesin/jasmine-jquery

Slide 144

Slide 144 text

run it yourself! http://is.gd/template1 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp']) @subject = new CallListView model: @model describe "events", -> ... describe "#moveUp", -> ... describe "#render", -> beforeEach -> @subject.render() it "renders the template with the model's JSON", -> expect($(@subject.el)).toHaveHtml('HTML for bar')

Slide 145

Slide 145 text

run it yourself! http://is.gd/template1 describe "CallListView", -> beforeEach -> @model = jasmine.createSpyObj('CallList',['bind','moveUp','toJSON']) @subject = new CallListView model: @model describe "events", -> ... describe "#moveUp", -> ... describe "#render", -> beforeEach -> @model.toJSON.andReturn foo: "bar" @subject.render() it "renders the template with the model's JSON", -> expect($(@subject.el)).toHaveHtml('HTML for bar')

Slide 146

Slide 146 text

run it yourself! http://is.gd/template1 describe "CallListView", -> beforeEach -> $template = inject('') $template.html('HTML for <%= foo %>') @model = jasmine.createSpyObj('CallList',['bind','moveUp','toJSON']) @subject = new CallListView model: @model describe "events", -> ... describe "#moveUp", -> ... describe "#render", -> beforeEach -> @model.toJSON.andReturn foo: "bar" @subject.render() it "renders the template with the model's JSON", -> expect($(@subject.el)).toHaveHtml('HTML for bar')

Slide 147

Slide 147 text

run it yourself! http://is.gd/template1

Slide 148

Slide 148 text

run it yourself! http://is.gd/template1 class CallListView extends Backbone.View events: "click .up-arrow": "moveUp" initialize: -> @template = _.template($('#call-list-template').html()) moveUp: (e) -> @model.moveUp $(e.target).text()

Slide 149

Slide 149 text

run it yourself! http://is.gd/template1 class CallListView extends Backbone.View events: "click .up-arrow": "moveUp" initialize: -> @template = _.template($('#call-list-template').html()) moveUp: (e) -> @model.moveUp $(e.target).text() render: -> $(@el).html(@template(@model.toJSON()))

Slide 150

Slide 150 text

run it yourself! http://is.gd/template1

Slide 151

Slide 151 text

routing users HTML templating model behavior UI events

Slide 152

Slide 152 text

router

Slide 153

Slide 153 text

class AppRouter extends Backbone.Router routes: “call-list”: “callList” callList: -> new CallListView(model: new CallList).render()

Slide 154

Slide 154 text

$ -> window.router = new AppRouter(); Backbone.history.start();

Slide 155

Slide 155 text

$ -> window.router = new AppRouter(); Backbone.history.start(); http://yourapp.dev/#call-list

Slide 156

Slide 156 text

$ -> window.router = new AppRouter(); Backbone.history.start(); http://yourapp.dev/#call-list router.navigate('call-list',true)

Slide 157

Slide 157 text

phew.

Slide 158

Slide 158 text

PARTING GIFTS!

Slide 159

Slide 159 text

jasmine helpers jQuery Spec’n https://github.com/velesin/jasmine-jquery HTML Inject’n https://github.com/searls/jasmine-fixture Conditional Stub’n https://github.com/searls/jasmine-stealth Given-When-Then’n https://github.com/searls/jasmine-given

Slide 160

Slide 160 text

code helpers Underscore.js http://documentcloud.github.com/underscore extend.js https://github.com/searls/extend.js Date.js http://www.datejs.com Underscore.string http://epeli.github.com/underscore.string/

Slide 161

Slide 161 text

Runner/CI Helpers jasmine-headless-webkit http://johnbintz.github.com/jasmine-headless-webkit/ jasmine-maven-plugin http://searls.github.com/jasmine-maven-plugin/ jasmine-gem https://github.com/pivotal/jasmine-gem jasmine-rails https://github.com/searls/jasmine-rails

Slide 162

Slide 162 text

github.com/searls +I’ll help!

Slide 163

Slide 163 text

@searls +I’ll help!

Slide 164

Slide 164 text

[email protected] +I’ll help!

Slide 165

Slide 165 text

•g l e e * for the color scheme •clafouti for the despair statue •SteveFE for the pouting girl •ifollowtherabbit for another color scheme •mokra for the jaw-dropped baby •Epicality for the third color scheme credits