Video and slides synchronized, mp3 and slide download available at URL http://bit.ly/14qqeTd.
Logan Linn explores the design and implications of an architecture built around immutable data structures using ClojureScript and Om, a ClojureScript interface to Facebook's React. Filmed at qconsf.com.
Logan Linn is a software engineer at Prismatic, a social news curation and content discovery app for web and mobile.
2. InfoQ.com: News & Community Site
• 750,000 unique visitors/month
• Published in 4 languages (English, Chinese, Japanese and Brazilian
Portuguese)
• Post content from our QCon conferences
• News 15-20 / week
• Articles 3-4 / week
• Presentations (videos) 12-15 / week
• Interviews 2-3 / week
• Books 1 / month
Watch the video with slide
synchronization on InfoQ.com!
http://www.infoq.com/presentations
/immutable-frontend-clojurescript
3. Purpose of QCon
- to empower software development by facilitating the spread of
knowledge and innovation
Strategy
- practitioner-driven conference designed for YOU: influencers of
change and innovation in your teams
- speakers and topics driving the evolution and innovation
- connecting and catalyzing the influencers and innovators
Highlights
- attended by more than 12,000 delegates since 2007
- held in 9 cities worldwide
Presented at QCon San Francisco
www.qconsf.com
6. IMMUTABLE FRONTEND
ClojureScript gives us immutability and more
Immutability simplifies data-flow
React allows us to render predictably with pure functions
4
8. OUTLINE
Challenges of a Building a Frontend
Immutability
ClojureScript
An Immutable Frontend
6
9. CHALLENGES OF BUILDING A FRONTEND
Interactive UIs have a human factor
Asynchronous programming
Complexity comes from every angle
Software complexity is a compounding debt
7
13. EVENTS AND DATA-BINDING
Most frameworks today structured around Models
Models publish changes via global events
Views subscribe to changes and update
Data bindings let you be declarative
11
22. A MUTABLE WORLD
20
x = Domain.List.from([‘x'])
y = x.unshift('y')
z = x.unshift('z')
print(z.second()) // 'x' or 'y'?
print(x) // ['x'] or ['z', 'y', 'x']?
23. AN IMMUTABLE WORLD…
21
x = Domain.List.from([‘x'])
y = x.unshift('y')
z = x.unshift('z')
print(z.second()) // 'x', final answer!
print(x) // ['x'], fasho!
27. IMMUTABILITY ON THE FRONTEND
Simplicity & Clarity
Predictability
Less defensive programming, i.e.
_.cloneDeep(obj)
25
28. IMMUTABILITY ON THE FRONTEND
Simplicity & Clarity
Predictability
Less defensive programming, i.e.
_.cloneDeep(obj)
Constant time dirty checking
26
29. IMMUTABILITY & PERFORMANCE
Persistent data structures
Structural sharing
Memory efficiency
Conjoin to collection in O(1)
Update hash-map in O(log32 n) vs O(n)
27
ab
c
31. CLOJURE & CLOJURESCRIPT
Dynamic, Functional, Lisp
Clojure
Compiles to JVM bytecode
7 years old
ClojureScript
Compiles to JavaScript
3 years old
29
32. WHY WE LIKE CLOJURESCRIPT
Clarity & Consistency
Strong core library over clean abstractions
Macros
Share code with rest of code-base
30
"It is better to have 100
functions operate on one
data structure than 10
functions on 10 data
structures."
—Alan Perlis, 1982
33. MUTATION REQUIRES OPT-IN
Immutable data by default
State modeled with reference to immutable value
Special functions to mutate reference & dereference value
Easy to identify side-effects
31
51. APP STATE
Reference to an immutable value
Updating state changes reference
Business logic as pure functions
Compose multiple operations & apply at once
49
App
State
52. REACT
UI rendering library from Facebook
“Not templating. Not MVC. It’s like a declarative jQuery” -Pete Hunt
Manipulates DOM & handles events (thats all)
Trending, and rightfully so!
50
React
D
O
M
54. REACT
Basic building block is a component with render() method
Data in, “virtual DOM” out
When data changes, render() is re-run
Performs diff between vDOM and actual DOM
Pushes out minimal set of changes to keep in sync
52
React
D
O
M
55. REACT & CLOJURESCRIPT
Shared design principles
pure and composable functions
simplicity through predictability
Actually useful API
Easy to compose from ClojureScript
Libraries like Om, Reagent, Quiescent
53
58. WRAP-UP
Immutability & referential transparency have many benefits
Testing & Reasoning
Application architecture
Invest in languages & tools that prioritize simplicity
Clojure & ClojureScript are great!
React is great!
56