SlideShare a Scribd company logo
1 of 67
Download to read offline
Retrofitting a legacy Om
SPA to use a functional
architecture
Manuel Rivero
@trikitrok
About me
● I’m Manuel Rivero (@trikitrok).
● I’m a member of Codesai.
● I collaborate with BCN Software
Craftsmanship, Clojure
Developers BCN and
Aprendices (@deAprendices)
communities.
● One of my clients is
Green Power
Monitor where we
develop a SPA using
ClojureScript and
Om.
Regardless of what we discover, we
understand and truly believe that
everyone did the best job they could,
given what they knew at the time,
their skills and abilities, the
resources available, and the
situation at hand.
Context
SPA for monitoring and managing
renewable energy portfolios using
ClojureScript and Om.
Several years of development
A prototype became
the product
No testing culture
Initial architecture blurred by
employee turnover, technical debt,
lack of documentation and design
flaws
Two times legacy
Lost mental models
Programming as Theory Building, Peter Naur
No tests
The Big Problem
Business logic and effectful code
not clearly separated
Conflating pure and effectful code
destroys the advantages of
functional programming
Imperative Programming
everywhere
Some code samples
● Stateful logic
● Stateful view
Problems
● Action at a distance
○ Difficult to understand
● Difficult to test
Hard to Evolve Safely
Working against the architecture/design
A Pit of Failure
A possible solution:
effects and coeffects
Effects and Coeffects
● Effects: describe your program’s
side-effects (what it does to the world)
● Coeffects: track your program’s
side-causes (what it requires from the world)
A design based on effects and coeffects
1. Extracting all needed data from “the world”
A design based on effects and coeffects
1. Extracting all needed data from “the world”
2. Using pure functions to compute effects
A design based on effects and coeffects
1. Extracting all needed data from “the world”
2. Using pure functions to compute effects
3. Performing the side effects described by effects
Clear isolation of effectful code
and business logic
Business logic becomes pure
Advantages
● Local reasoning
○ Easier to understand
● Easier to test
Pure Functional Core, Imperative Shell
Declarative Effects
pattern
Declarative Effects pattern
● Business logic is comprised by pure functions
● They receive coeffects and return effects
● Injecting the values coeffects track and interpreting
effects to actually perform side-effects are done by
sth else (the language run-time, a framework, etc)
re-frame
Unidirectional Data Flow Architecture
from re-frame wonderful docs
Event Driven Data Flow
re-frame architecture
Imagine a re-frame app as a reduce
State Management
● At any point in time, app state results from
reducing over all events dispatched in the app
up until that time.
● The combining function for the reduce is the
set of registered event handlers.
Pure functional Core
Pure Functional Core, Imperative Shell
re-frame subscriptions
● Query functions
● Extract data from the app state and
provide it to view functions in the right
format
Simpler model & Dumb Views
● Avoid having to keep derived state in
the model (app state).
● Dumb down the views, that end up
being simple “data in, screen out”
functions
Improve Performance
● De-duplicating computations
● Reducing re-renderings
A Pit of Success
A Pit of Success
“A well-designed system makes it
easy to do the right things and
annoying (but not impossible) to do
the wrong things.” Jeff Atwood
Why not use re-frame
then?
A rewrite
was
not an option
Strangler application pattern
Progressively evolve to a re-frame like
architecture, while being able to keep
adding new features all the time
Strangler application pattern
● All new code would use the new
architecture
● Only port bit by bit those legacy parts
that needed to change
If you're in a hole, stop digging!
Ok, but why not use re-frame?
Om to reagent: too huge a change
● Heavy investment on Om
● Long coexistence of Om and reagent
● Rewriting components or
● Complicated wrapping to reuse
components
re-om is born
Our real goal: having the advantages
of re-frame’s architecture in our SPA
re-om was a less
risky and progressive way to get it
We wrote an event-driven framework
using effects and coeffects
and called it re-om as a joke
Using re-om event handlers, effects & coeffects sample code
We added subscriptions that worked
like re-frame’s ones but with Om
components
Using re-om subscriptions
Evolving the SPA
● re-om is not invasive
● Ideal for strangler application pattern
re-om
Open-sourcing
re-om & reffectory
● We extracted from re-om the code that was
independent of any view technology.
● It might be used as the base for creating
frameworks similar to re-om for other view
technologies, like for example rum, or used
directly in Clojure projects.
reffectory
● Delegates to reffectory for the event-driven
system withs effects and coeffects.
● Adds subscriptions that work with Om
components.
re-om
Conclusions
Thanks to re-om we could start
carving islands of pure functional
code inside our SPA’s imperative
soup
This introduced some sanity in our
code base and made its development
more sustainable
● GreenPowerMonitor for open-sourcing re-om and
reffectory
● André Stylianos and Joel Sánchez, and the rest of our
colleagues at GreenPowerMonitor
● re-frame project, a really wonderful way of writing SPAs, on
which re-om is heavily inspired
● Francesc for introducing me to ClojureScript and re-frame
Acknowledgements
Thanks!!
Manuel Rivero @trikitrok

More Related Content

Similar to Retrofitting a legacy SPA to use a functional architecture

How to Migrate Applications Off a Mainframe
How to Migrate Applications Off a MainframeHow to Migrate Applications Off a Mainframe
How to Migrate Applications Off a Mainframe
VMware Tanzu
 
NLP for videos: Understanding customers' feelings in videos - Albert Lewandow...
NLP for videos: Understanding customers' feelings in videos - Albert Lewandow...NLP for videos: Understanding customers' feelings in videos - Albert Lewandow...
NLP for videos: Understanding customers' feelings in videos - Albert Lewandow...
GetInData
 

Similar to Retrofitting a legacy SPA to use a functional architecture (20)

Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
Effects, coeffects & subscriptions: a pit of success for SPAs Socracan18
 
Effects, Coeffects & Subscriptions: a pit of success for SPAs
Effects, Coeffects & Subscriptions: a pit of success for SPAsEffects, Coeffects & Subscriptions: a pit of success for SPAs
Effects, Coeffects & Subscriptions: a pit of success for SPAs
 
How to Migrate Applications Off a Mainframe
How to Migrate Applications Off a MainframeHow to Migrate Applications Off a Mainframe
How to Migrate Applications Off a Mainframe
 
unit-1 agile development.pptx
unit-1 agile development.pptxunit-1 agile development.pptx
unit-1 agile development.pptx
 
Amit_Resume
Amit_ResumeAmit_Resume
Amit_Resume
 
Resume
ResumeResume
Resume
 
Managing software projects & teams effectively
Managing software projects & teams effectivelyManaging software projects & teams effectively
Managing software projects & teams effectively
 
Agile & SCRUM basics
Agile & SCRUM basicsAgile & SCRUM basics
Agile & SCRUM basics
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
CV_DebarpanMukherjee
CV_DebarpanMukherjeeCV_DebarpanMukherjee
CV_DebarpanMukherjee
 
ML Platform Q1 Meetup: Airbnb's End-to-End Machine Learning Infrastructure
ML Platform Q1 Meetup: Airbnb's End-to-End Machine Learning InfrastructureML Platform Q1 Meetup: Airbnb's End-to-End Machine Learning Infrastructure
ML Platform Q1 Meetup: Airbnb's End-to-End Machine Learning Infrastructure
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...
 
AppDynamics User Group
AppDynamics User GroupAppDynamics User Group
AppDynamics User Group
 
Jamshid_Odoo_Profile
Jamshid_Odoo_ProfileJamshid_Odoo_Profile
Jamshid_Odoo_Profile
 
NLP for videos: Understanding customers' feelings in videos - Albert Lewandow...
NLP for videos: Understanding customers' feelings in videos - Albert Lewandow...NLP for videos: Understanding customers' feelings in videos - Albert Lewandow...
NLP for videos: Understanding customers' feelings in videos - Albert Lewandow...
 
Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
Bsc. Csit Final Year Internship Presentation
Bsc. Csit Final Year Internship PresentationBsc. Csit Final Year Internship Presentation
Bsc. Csit Final Year Internship Presentation
 
ravi kumar s
ravi kumar sravi kumar s
ravi kumar s
 
The ZDLC Brief
The ZDLC BriefThe ZDLC Brief
The ZDLC Brief
 
Predictable reactive state management for enterprise apps using NGRX/platform
Predictable reactive state management for enterprise apps using NGRX/platformPredictable reactive state management for enterprise apps using NGRX/platform
Predictable reactive state management for enterprise apps using NGRX/platform
 

Recently uploaded

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
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Retrofitting a legacy SPA to use a functional architecture