SlideShare une entreprise Scribd logo
1  sur  60
THE IMMUTABLE FRONTEND IN
CLOJURESCRIPT
Logan Linn (@loganlinn)
QCon SF 2014
1
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
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
PRISMATIC
Personalized, interest-based newsfeeds
Crawlers, Machine Learning, Clients
We’re very functional
99.9% Clojure backend
ClojureScript frontend
We <3 open-source
2
3
IMMUTABLE FRONTEND
ClojureScript gives us immutability and more
Immutability simplifies data-flow
React allows us to render predictably with pure functions
4
IMMUTABLE FRONTEND
5
ReactApp
State
User
Events
API
Data
Component
Tree
D
O
M
OUTLINE
Challenges of a Building a Frontend
Immutability
ClojureScript
An Immutable Frontend
6
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
INCIDENTAL COMPLEXITY
Managing state
8
INCIDENTAL COMPLEXITY
Managing state
Mutating data
9
MODEL-VIEW-*
Domain vs Presentational data
Keeping data and DOM in sync
MVC, MVP, MVVM, etc.
10
animate
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
ControllerModel View
12
EVENT AND DATA-BINDING
Encourages mutation
Data-flow becomes opaque, potentially hazardous
Makes it easier, but not simpler
13
MVC DATA-FLOW
14
MVC DATA-FLOW
15
ControllerModel View
MVC DATA-FLOW
16
ControllerModel View
DOM
User
Events
What if we prioritized a simple data-flow?
17
SINGLE-DIRECTION DATA FLOW
Pure
Render
18
DOMApp
State
User
Events
IMMUTABILITY
19
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']?
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!
RENDERING WITH PURE FUNCTIONS
22
ƒ(S1) = D1
ƒ(S2) = D2
ƒ(S1) = D1
t
IMMUTABILITY ON THE FRONTEND
Simplicity & Clarity
23
IMMUTABILITY ON THE FRONTEND
Simplicity & Clarity
Predictability
24
IMMUTABILITY ON THE FRONTEND
Simplicity & Clarity
Predictability
Less defensive programming, i.e.
_.cloneDeep(obj)
25
IMMUTABILITY ON THE FRONTEND
Simplicity & Clarity
Predictability
Less defensive programming, i.e.
_.cloneDeep(obj)
Constant time dirty checking
26
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
CLOJURE(SCRIPT)
28
CLOJURE & CLOJURESCRIPT
Dynamic, Functional, Lisp
Clojure
Compiles to JVM bytecode
7 years old
ClojureScript
Compiles to JavaScript
3 years old
29
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
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
(def state-ref (atom {}))
(deref state-ref) ;; => {}
(reset! state-ref {:a 1})
@state-ref ;; => {:a 1}
(defn increment-a [state]
(update-in state [:a] inc))
(increment-a @state-ref) ;; => {:a 2}
@state-ref ;; => {:a 1}
(swap! state-ref increment-a)
@state-ref ;; => {:a 2}
32
SEPARATION OF CONCERNS
Time
Relative moments when events occur
State
A value at a point in time
Identity
Entity associated with state over time
33
AN IMMUTABLE ARCHITECTURE
34
AN IMMUTABLE ARCHITECTURE
35
ReactApp
State
User
Events
HTTP
Data
Component
Tree
D
O
M
AN IMMUTABLE ARCHITECTURE
state
36
AN IMMUTABLE ARCHITECTURE
component
component
component
state
component
37
AN IMMUTABLE ARCHITECTURE
state
component
component component
component
38
AN IMMUTABLE ARCHITECTURE
state
component component
component
component
39
AN IMMUTABLE ARCHITECTURE
state
component component
40
AN IMMUTABLE ARCHITECTURE
state
component
41
AN IMMUTABLE ARCHITECTURE
state
component
42
component
tree
actions
AN IMMUTABLE ARCHITECTURE
43
state
component
tree
AN IMMUTABLE ARCHITECTURE
44
state
component
tree
AN IMMUTABLE ARCHITECTURE
45
component
tree
AN IMMUTABLE ARCHITECTURE
46
S1
component
tree
S2
AN IMMUTABLE ARCHITECTURE
47
ReactApp
State
User
Events
HTTP
Data
Component
Tree
D
O
M
AN IMMUTABLE ARCHITECTURE
Immutable application state
Business logic written as pure functions
Declarative rendering
48
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
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
REACT
var HelloMessage = React.createClass({
render: function() {
return DOM.div({}, "Hello " + this.props.name);
}
});
React.render(HelloMessage({name: "QCon"}), mountNode);
React.render(HelloMessage({name: "QCon SF"}), mountNode);
51
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
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
DOM2
REACT & CLOJURESCRIPT
54
S1
D1
S2
D2
React
DOM1
SIMPLE OM EXAMPLE
55
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
THANKS!
@loganlinn
@prismatic
57
Watch the video with slide synchronization on
InfoQ.com!
http://www.infoq.com/presentations/immutable-
frontend-clojurescript

Contenu connexe

Plus de C4Media

Plus de C4Media (20)

Next Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileNext Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy Mobile
 
Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020
 
Understand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsUnderstand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java Applications
 
Kafka Needs No Keeper
Kafka Needs No KeeperKafka Needs No Keeper
Kafka Needs No Keeper
 
High Performing Teams Act Like Owners
High Performing Teams Act Like OwnersHigh Performing Teams Act Like Owners
High Performing Teams Act Like Owners
 
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaDoes Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
 
Service Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideService Meshes- The Ultimate Guide
Service Meshes- The Ultimate Guide
 
Shifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDShifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CD
 
CI/CD for Machine Learning
CI/CD for Machine LearningCI/CD for Machine Learning
CI/CD for Machine Learning
 
Fault Tolerance at Speed
Fault Tolerance at SpeedFault Tolerance at Speed
Fault Tolerance at Speed
 
Architectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsArchitectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep Systems
 
ML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.js
 
Build Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerBuild Your Own WebAssembly Compiler
Build Your Own WebAssembly Compiler
 
User & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleUser & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix Scale
 
Scaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeScaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's Edge
 
Make Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereMake Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home Everywhere
 
The Talk You've Been Await-ing For
The Talk You've Been Await-ing ForThe Talk You've Been Await-ing For
The Talk You've Been Await-ing For
 
Future of Data Engineering
Future of Data EngineeringFuture of Data Engineering
Future of Data Engineering
 
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreAutomated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
 
Navigating Complexity: High-performance Delivery and Discovery Teams
Navigating Complexity: High-performance Delivery and Discovery TeamsNavigating Complexity: High-performance Delivery and Discovery Teams
Navigating Complexity: High-performance Delivery and Discovery Teams
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

The Immutable Front-end in ClojureScript