Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

!

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

It's a sign!

Slide 7

Slide 7 text

@Jack_Franklin

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

"Let's replicate rails"

Slide 12

Slide 12 text

MVC / MVVC / MCVCVMMCVCCC

Slide 13

Slide 13 text

Two way data binding and dirty checking

Slide 14

Slide 14 text

Object.observe

Slide 15

Slide 15 text

We can do better

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Changes in data

Slide 19

Slide 19 text

The application owns its state

Slide 20

Slide 20 text

Single source of truth

Slide 21

Slide 21 text

http://todo.com/filter/completed { todos: [{ ... }, { ... }], filter: 'completed' } Duplicate knowledge = out of sync quickly!

Slide 22

Slide 22 text

Most state lives in one place, with exceptions.

Slide 23

Slide 23 text

It Depends

Slide 24

Slide 24 text

Our UI becomes a representation of state at a given time

Slide 25

Slide 25 text

{ todos: [{ text: 'Do an Elm talk', done: true }, ... ] }

Slide 26

Slide 26 text

{ todos: [{ text: 'Do an Elm talk', done: false }, ... ] }

Slide 27

Slide 27 text

{ todos: [{ text: 'Do an Elm talk', done: true }, ... ] }

Slide 28

Slide 28 text

Efficient rendering is not a developer concern1 1 within reason, not always, be reasonable, thanks!

Slide 29

Slide 29 text

Explicitly define how the user can modify the state

Slide 30

Slide 30 text

And be able to trace it

Slide 31

Slide 31 text

angular.controller('MyCtrl', function($scope) { $scope.onClick = function() { $scope.x = $scope.x + 1; } function fetchDataAndIncrement() { fetch('api.com/data').then(function(data) { $scope.data = data; $scope.x = $scope.x + 1; }); } });

Slide 32

Slide 32 text

function addTodo() { return { type: 'USER_ADD_TODO', text: 'Buy Milk' } } These can be logged, stored, tracked, serialized and so on.

Slide 33

Slide 33 text

Have update functions that can handle actions and update the state update(action, state) => newState

Slide 34

Slide 34 text

const action = { type: 'USER_LOG_OUT'; }; const currentState = { user: { name: 'Jack' } }; const newState = update(action, currentState); newState => { user: undefined };

Slide 35

Slide 35 text

update encapsulates most business logic

Slide 36

Slide 36 text

Your logic and your UI are separated

Slide 37

Slide 37 text

Data flow

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Recommended Reading ➡ Unidirectional User Interface Architectures by André Staltz

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Explictly deal with all user interactions and state changes function update(action, state) { switch (action.type) { case 'NEW_USER': return Object.assign({}, state, { user: { name: action.name } }); } }

Slide 42

Slide 42 text

Even better! type Msg = NewUser String | LogOut

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Elm: a language to solve these problems.

Slide 45

Slide 45 text

Not the perfect language (yet?!)

Slide 46

Slide 46 text

Not the perfect solution to all our problems

Slide 47

Slide 47 text

→ Functional → Typed → Compiled

Slide 48

Slide 48 text

→ Expressive, clear code → Self documenting → Robust

Slide 49

Slide 49 text

Learning curve ahead!

Slide 50

Slide 50 text

Expressive, clear code (add 1 2) List.map (\x -> x + 2) [1, 2, 3, 4] List.map ((+) 2) [1, 2, 3, 4]

Slide 51

Slide 51 text

Pipes incrementWeight (incrementHeight (incrementAge (makePerson "jack"))) makePerson "jack" |> incrementAge |> incrementHeight |> incrementWeight

Slide 52

Slide 52 text

! makePerson "jack" |> incrementAge |> incrementHeight |> incrementWeight |> incrementWeight |> incrementWeight |> incrementWeight |> incrementWeight |> incrementWeight |> incrementWeight |> incrementWeight |> incrementWeight |> incrementWeight |> incrementWeight

Slide 53

Slide 53 text

incrementAge person = { person | age = person.age + 1 } add x y = x + y addTwo = add 2

Slide 54

Slide 54 text

Self documenting

Slide 55

Slide 55 text

Types add : Int -> Int -> Int add x y = x + y isEven : Int -> Bool isEven x = n % 2 == 0

Slide 56

Slide 56 text

Union Types type Filter = ShowAll | ShowCompleted | ShowActive

Slide 57

Slide 57 text

type Filter = ShowAll | ShowCompleted | ShowActive showTodos : Filter -> List Todo -> List Todo showTodos filter todos = case filter of ShowAll -> todos ShowCompleted -> List.filter (\t -> t.complete) todos ShowActive -> List.filter (\t -> not t.complete) todos

Slide 58

Slide 58 text

Union Types ! They can be checked by the compiler (typos are spotted) ! Compiler ensures all are dealt with in case ... of ! Easy to change / add a new one: add it and fix each compiler error!

Slide 59

Slide 59 text

Type aliases type alias Person = { name : String , age : Int } incrementAge : Person -> Person incrementAge person = { person | person.age = person.age + 1 }

Slide 60

Slide 60 text

! Clearer code, typed in your domain specific objects. ! Compiler can guarantee you're meeting the type requirements. ! No more 'undefined is not a function' !

Slide 61

Slide 61 text

Robust

Slide 62

Slide 62 text

Immutability brings guarantees var person = { name: 'Jack', age: 24 }; incrementAge(person); // has this mutated? // does it return a new person?

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Sweet, sweet Elm let person = { name = "Jack", age = 24 } in incrementAge person ! person is untouched ! incrementAge has to return a new person ! goodbye mutation bugs

Slide 65

Slide 65 text

Pure Functions Elm functions are always pure. let sum = (a, b) => a + b; //PURE sum(2, 2) // => ALWAYS 4 let otherSum = (a, b) => window.foo + a + b; //IMPURE otherSum(2, 2) // => who knows, dependent on window.foo

Slide 66

Slide 66 text

Dealing with nothing

Slide 67

Slide 67 text

I call it my billion-dollar mistake.

Slide 68

Slide 68 text

It was the invention of the null reference in 1965.

Slide 69

Slide 69 text

This has led to innumerable errors, vulnerabilities, and system crashes -- Tony Hoare

Slide 70

Slide 70 text

You might say it's a...

Slide 71

Slide 71 text

...hoare-able mistake!

Slide 72

Slide 72 text

(You may leave)

Slide 73

Slide 73 text

No null: how do you represent a value being present or empty?

Slide 74

Slide 74 text

render() { return

{ this.props.apiData.username }

; }

Slide 75

Slide 75 text

Maybe

Slide 76

Slide 76 text

Maybe type Maybe a = Just a | Nothing It's either Just some value, or Nothing.

Slide 77

Slide 77 text

As a value, I am: → Just the integer 5 → or, I am Nothing I have the type Maybe Int

Slide 78

Slide 78 text

Get the first thing from the list, and double it2 let list = [1, 2, 3] in (List.head list) * 2 But what if list = [] ? 2 this code is not valid Elm, becuase List.head returns Maybe

Slide 79

Slide 79 text

Slide 80

Slide 80 text

Maybe type alias Model = { user : Maybe User } view : Model -> Html Msg view model = case model.user of Nothing -> div [] [ text "No user!" ] Just user -> div [] [ text ("Logged in as " ++ user.name) ]

Slide 81

Slide 81 text

You must handle all cases of missing / pending data

Slide 82

Slide 82 text

Task A module for async actions that might fail (HTTP). Task errType successType Task String User - if it fails, fail with a String - if it succeeds, succeed with a User

Slide 83

Slide 83 text

Commands an async thing that Elm should run for you (HTTP requests)

Slide 84

Slide 84 text

Learning curve!

Slide 85

Slide 85 text

The Elm compiler make sure that 100% of your code is thoroughly checked against corner cases and error cases. This everywhereness becomes a guarantee. And it is only because of this guarantee that Elm programs have virtually no runtime errors. -- Everywhereness as a Foundation, André Staltz

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Building actual apps in Elm

Slide 89

Slide 89 text

The Elm Architecture

Slide 90

Slide 90 text

The three parts: model : Model view : Model -> Html Msg update : Msg -> Model -> Model

Slide 91

Slide 91 text

What changed?

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Building a counter app

Slide 94

Slide 94 text

First, define your Model type alias Model = Int initialModel : Model initialModel = 0

Slide 95

Slide 95 text

Secondly, define your Msgs type Msg = Increment | Decrement

Slide 96

Slide 96 text

Thirdly, define your update: update : Msg -> Model -> Model update msg model = case msg of Increment -> model + 1 Decrement -> model - 1

Slide 97

Slide 97 text

Fourthly, define your view: view : Model -> Html Msg view model = div [] [ button [ onClick Decrement ] [ text "-" ] , div [] [ text (toString model) ] , button [ onClick Increment ] [ text "+" ] ]

Slide 98

Slide 98 text

Finally, hook it all up! main = Html.App.beginnerProgram { model = initialModel , view = view , update = update }

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

! We left the view until last. ! Explained all our logic before the UI. ! Notice how easy update would be to test.

Slide 101

Slide 101 text

Side Effects

Slide 102

Slide 102 text

Explicitly model side effects. Hand off to Elm, it will hand back later. Keeps functions pure, and async easier to reason about.

Slide 103

Slide 103 text

Commands

Slide 104

Slide 104 text

Whenever you need to perform some background work, you have to give Elm a command. Elm will go off, perform the command, and call your update function once it's done.

Slide 105

Slide 105 text

The Elm Architecture, Part 2

Slide 106

Slide 106 text

model : Model view : Model -> Html Msg update : Msg -> Model -> (Model, Cmd Msg)

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

Fetching someone's GitHub data.

Slide 109

Slide 109 text

Firstly, define the model: type alias GithubPerson = { name : String , company : String } type alias Model = { username : String , githubPerson : Maybe GithubPerson }

Slide 110

Slide 110 text

Secondly, define your Msgs type Msg = NewGithubData GithubPerson | FetchGithubData | FetchError Http.Error

Slide 111

Slide 111 text

Thirdly, define your update: update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of NewGithubData person -> ( { model | githubPerson = Just person }, Cmd.none ) FetchGithubData -> ( model, fetchGithubData model.username ) ...

Slide 112

Slide 112 text

NewGithubData person -> ( { model | githubPerson = Just person }, Cmd.none ) -- Cmd.none === do nothing

Slide 113

Slide 113 text

FetchGithubData -> ( model, fetchGithubData model.username ) --- fetchGithubData returns a command --- which Elm will run for us

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

Fourthly, define your view: view : Model -> Html Msg view model = case model.githubPerson of Just person -> div [] [ text (person.name ++ ", " ++ person.company) ] Nothing -> div [] [ button [ onClick FetchGithubData ] [ text "Load!" ] ]

Slide 116

Slide 116 text

Fifthly (new step), define your init: initialModel : Model initialModel = { username = "jackfranklin" , githubPerson = Nothing } init : ( Model, Cmd Msg ) init = ( initialModel, Cmd.none )

Slide 117

Slide 117 text

Finally, hook it all together! main = Html.App.program { init = init , view = view , update = update , subscriptions = \_ -> Sub.none }

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

Deep breath!

Slide 129

Slide 129 text

That feels like a lot of code to do something so simple -- All of you.

Slide 130

Slide 130 text

Boilerplate vs Explicitness

Slide 131

Slide 131 text

Benefits increase as application grows Which is often where JavaScript starts to struggle.

Slide 132

Slide 132 text

jackfranklin/elm-for-js-developers-talk

Slide 133

Slide 133 text

Components...ish?

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

The Elm Ecosystem

Slide 136

Slide 136 text

elm reactor

Slide 137

Slide 137 text

elm package

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

There's so much more I haven't covered.

Slide 140

Slide 140 text

So, why / when should you use Elm?

Slide 141

Slide 141 text

You're fed up of undefined function errors that take up loads of time

Slide 142

Slide 142 text

You want to develop with the confidence of Types and a clever compiler to back you up

Slide 143

Slide 143 text

You're happy to "ride the wave" and deal with a language still growing and settling down

Slide 144

Slide 144 text

You're happy to build more packages than depend on existing solutions which may not exist in Elm

Slide 145

Slide 145 text

But what if this talk has put me off Elm?

Slide 146

Slide 146 text

Elm does take time to learn, so please don't give up after 30 minutes of slides! guide.elm-lang.org

Slide 147

Slide 147 text

Elm the language brings many concepts that are language agnostic

Slide 148

Slide 148 text

The Elm Architecture

Slide 149

Slide 149 text

Explicitness across your application

Slide 150

Slide 150 text

Types

Slide 151

Slide 151 text

Immutability / Functional Programming

Slide 152

Slide 152 text

Defining your application step by step 1. Define your model. 2. Define your actions. 3. Define your update function. 4. Define your view. 5. Repeat.

Slide 153

Slide 153 text

Will everyone be writing Elm in 1/2/5 years?

Slide 154

Slide 154 text

It Depends

Slide 155

Slide 155 text

But it's great fun!

Slide 156

Slide 156 text

→ guide.elm-lang.org → elm-lang.org/docs → elm-lang.org/community → github.com/jackfranklin/elm-for-js-developers- talk → speakerdeck.com/jackfranklin

Slide 157

Slide 157 text

@Jack_Franklin javascriptplayground.com