Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Practically Immutable
Search
sporto
March 28, 2015
Technology
200
0
Share
Practically Immutable
Immutable data in practice in JavaScript
sporto
March 28, 2015
More Decks by sporto
See All by sporto
React inside Elm
sporto
2
190
Elm
sporto
1
270
Redux: Flux Reduced
sporto
1
370
Webpack and React
sporto
4
400
Rails with Webpack
sporto
1
230
Lesson learnt building Single Page Application
sporto
0
140
Grunt
sporto
1
200
Safe Testing in Ruby
sporto
1
140
Go - A great language for building web applications
sporto
1
350
Other Decks in Technology
See All in Technology
MLOps導入のための組織作りの第一歩
akasan
0
300
終盤で崩壊させないAI駆動開発
j5ik2o
2
2.2k
AI時代にデータ基盤が持つべきCapabilityを考える + Snowflake Data Superheroやっていき宣言 / Considering the Capabilities Data Platforms Should Have in the AI Era + Declaration of Commitment as a Snowflake Data Superhero
civitaspo
0
110
インターネットの技術 / Internet technology
ks91
PRO
0
170
クラウドネイティブな開発 ~ 認知負荷に立ち向かうためのコンテナ活用
literalice
0
100
NOSTR, réseau social et espace de liberté décentralisé
rlifchitz
0
200
Rapid Start: Faster Internet Connections, with Ruby's Help
kazuho
2
150
サイボウズ 開発本部採用ピッチ / Cybozu Engineer Recruit
cybozuinsideout
PRO
10
78k
生成AI時代のエンジニア育成 変わる時代と変わらないコト
starfish719
0
8.9k
[OpsJAWS 40]リリースしたら終わり、じゃなかった。セキュリティ空白期間をAWS Security Agentで埋める
sh_fk2
3
210
最近の技術系の話題で気になったもの色々(IoT系以外も) / IoTLT 花見予定会(たぶんBBQ) @都立潮風公園バーベキュー広場
you
PRO
1
210
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.1k
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
340
Building the Perfect Custom Keyboard
takai
2
730
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
430
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
130
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.4k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
94
Skip the Path - Find Your Career Trail
mkilby
1
110
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
520
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
Are puppies a ranking factor?
jonoalderson
1
3.3k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Transcript
practically immutable ! Immutable data in JS @sebasporto
I build CRUD apps, mostly
What? • Something that cannot change • Once created stays
the same
var array1 = [1, 2].immutable var array2 = array1.push(3) !
array1 //=> [1, 2] array2 //=> [1, 2, 3]
WHY?
# 1 no side effects
var orders = [1, 2]; processOrders(orders); what is orders after
this?
Predictability Store Comp Comp Data Data wants to sort data
wants to transform data doesn't want its data to be modified externally
Enforces uni directional data flow React + Flux Store Comp
Action
# 2 Easier workflows
Undo add(item) { history.push(items) items = items.push(item) } undo() {
items = history.pop() }
Dirty objects Get record from store Clone record Edit record
Save Replace original Yes Discard No
Dirty objects Get record from store Edit record Save Replace
original Yes Discard No
HOW?
clojurescript immutable data for free but this talk is about
plain JS
• In JS strings and numbers are immutable (not the
case in every language) • Arrays, objects are not - so we need help here • JS and immutable data => opposing forces
immutable.js • Great for simple collections • [1, 2, ...]
['a', 'b' ...] • Caveats for collections with objects https://github.com/facebook/immutable-js
Immutable.js var record = {label: 'Sam'}; var list = Immutable.List([record]);
! console.log(list.get(0).label) // => 'Sam' http://jsbin.com/dilupu/1/embed?js,console record.label = 'Biff' ! console.log(list.get(0).label) // => 'Biff' Opps!
immutable.js To be fair: Immutable.js can do the right thing
if you learn it really well Immutable.js != Just works™
Seamless immutable • Fixes Immutable.js issues with deep objects https://github.com/rtfeldman/seamless-immutable
var record = {label: 'Sam'}; var list = Immutable([record]); !
console.log(list[0].label) // => 'Sam' ! record.label = 'Biff' ! console.log(list[0].label) // => 'Sam' http://jsbin.com/fayupu/1/edit?js,console Seamless immutable
imm https://github.com/sporto/imm • Build on top of seamless-immutable • Nice
CRUD API
imm list = list.add(record) list = list.replace(record) list = list.remove(id)
! record = list.get(id)
in Conclusion
Immutable data can make your application more robust
but play a lot with your chosen lib, understand
the caveats
Thanks