SlideShare une entreprise Scribd logo
1  sur  185
Télécharger pour lire hors ligne
WHEN FP
meets DDD
Good Things Happen
Cyrille Martraire - @cyriux -
http://25.media.tumblr.com/tumblr_lus2bi5FAU1qhnnqjo1_500.jpg
Passionate
developer
PARIS
Since 1999
@cyriux
Cyrille Martraire
Paris Software
Craftsmanship Community
http://www.meetup.com/paris-software-craftsmanship/
Arolla.fr
WARNING
The	 following	 show	 features	 
no	 spectacular	 stunt,	 no	 live	 
coding,	 only	 trivial	 Java	 code.	 
You	 can	 re-create	 or	 re-enact	 
at	 home	 with	 no	 danger.
A matter
of Taste
http://rosshirt.blogspot.fr/
The Code Gourmet
(dedicate to @ziobrando)
"My first encounter
with FP concepts
was from DDD"
a bit personal
DDD+FP
= ?
DDD?
justenoughtheory
http://www.virtual-genius.com/blog/post/Domain-Driven-Design-Immersion-Course-e280
Focus on the
domain!
Seniors Developers
http://www.thisisio.ie/blog/article/149/hiring_senior_developer
I. Putting the
model to work
II. Building blocks
(Tactical DDD)
III.Refactoring
toward deeper
insight
IV. Strategic DDD
Adopted 2005
Still in love
What do you think
when you hear
about FP?
http://jeremykun.com/2013/02/08/why-there-is-no-hitchhikers-guide-to-mathematics-for-programmers/
http://www.jaider.net/archives/609-intro-to-functional-programming/
PURE
No State
No War
What do DDD & FP
have in common?
Value Objects................................................................................................. 19
Learn one
and get the other
one for FREE!
DDD
FP
(OO)
DDD
FP
(OO)
NICE
STYLEof code
Let’s try that
sam
plepractice
(Prag) Dave Thomas
http://codekata.pragprog.com/
Code
Kata FTW!
Day
Max Temperature
Min Temperature
Unit (C/F)
Please compute
Daily min-max spreads
Daily min-max mids
Monthly average
...
GO
Read file
Compute spread
& console output
DONE.
NEXT
DONE.
UH-OH!
Duplication
Duplication
Transaction
Script 1
Transaction
Script 2
Transaction
Script 1
Transaction
Script 2≈
Data
access
Data
access
Presentation Presentation
Domain Domain
Transaction
Script 1
Transaction
Script 2
Data
access
Data
access
Presentation Presentation
Domain Domain
Transaction
Script 1
Transaction
Script 2
≈
Data
access
Data
access
Presentation Presentation
Domain Domain
Transaction
Script 1
Transaction
Script 2
≈
=
Data
access
Data
access
Presentation Presentation
Domain Domain
Transaction
Script 1
Transaction
Script 2
≈
=
=
Data
access
Presentation
Domain Domain
Data
access
Presentation
Domain Domain≈
Data
access
Presentation
Domain
Data
access
Presentation
Domain
Refactoring of similar
Transaction Scripts -> layers!
Emergence of a
domain layer
Emergence of a
domain layer
DDD
Emergence of a
domain layer
DDD
Domain layer:
"Here we talk about
the domain only"
My hygiene standards in
domain layer
• Dependencies
– Legacy
– Middleware
– Frameworks
– Database stuff
– No logging
null
+ TDD, BDD
+ Clean Code
Load data
process data
output data
<<side-effect-free>>
Load data
<<side-effect>>
<<side-effect>>
process data
output data
<<side-effect-free>>
Load data
<<side-effect>>
<<side-effect>>
process data
output data
Favor side-effect-
free functions in
domain layer
Data
access
Presentation
Domain functions
records
list
side-effect-
free function
Very easy to test
input output
http://le.compendium.pagesperso-orange.fr/compendium_metrique.htm
expected = expression
Side-effect free service
<<ValueObje
ct
<<ValueObje
ct
<<ValueObject>>
<<ValueObje
ct
<<ValueObject>>
<<Service>>
<<SPI>><<Service>>
<<API>>
Values in Values out
NEXT
= 1/N	 ∑
Varying units
18°C
+ 65°F
= ?
Duplication
Duplication
Let’s factor it out
Extract a function?
We can do better
Extract a
TYPE
Temperature
value
unit
behavior!
"65°F"
No getter/
setter
Immutable
Enum
Immutable Goodness
bugs
keep old state
safe sharing
(concurrency)
Temperature
value
unit
behavior!
<<ValueObject>>
Value Object:
equals by fields
equality
FP: everything
is a value
Value Object
A DDD pattern
to import FP-ish
values in OO
languages
“Functional-First” style
“Functional-First” style
• Value Objects (Quantity, Range…) as
much as possible
90%
“Functional-First” style
Value Objects
(Ok, gut feeling, I did’nt measure)
Methods return
same type
toCelsius(): Temperature
toFarenheit(): Temperature
Temperature
Methods return
same type
toCelsius(): Temperature
toFarenheit(): Temperature
Temperature
avg = 1 ∑ti
n i
double sum = 0;
int count = 0;
for(Temperature t in temperatures){
sum+= t1.toCelsius().getValue();
count++;
}
return sum/count;
double sum = 0;
int count = 0;
for(Temperature t in temperatures){
sum+= t1.toCelsius().getValue();
count++;
}
return sum/count;
DEMETER
VIOLATION
avg = t1.add(t2)
.scale(1/2);
returns a new
instance
Change -> new instance
Methods manipulate
same type
toCelsius(): Temperature
toFarenheit(): Temperature
add(Temperature): Temperature
scale(double): Temperature
Temperature
Closure of
Operations
Closure of
Operations
18°C
+ 16°C
= 34°C
Temperature sum = max.add(min);
18°C
+ 16°C
= 34°C
Object
Arithmetics
Object
Arithmetics
avg = 1 ∑ti
n i
unit-safe
DONE.
...for(...){...}
...for(...){...}
Duplication
Duplication
Zipper?
We can even do
better
How do domain
experts THINK
about it?
How do domain
experts THINK
about it?
SAY
SKETCH
Go out and
listen to them!
Tell us
about your domain
blabla Time
Series bla bla...
Time Series?
Yes, let me show
you
- =
Max Min Spreads
OK
Introduce a type
TimeSeries
TimeSeries spread =
max.subtract(min);
<<ValueObject>>
TimeSeries
points
at(i)
add(TimeSeries):TimeSeries
subtract(TimeSeries):TimeSe
Value Object
again
Closure of
Operations
again
add(TimeSeries):TimeSeries
subtract(TimeSeries):TimeSeries
Declarative style
TimeSeries spread
= max.subtract(min);
Temperature sum
= max.add(min);
So far
Domain Layer
Better style
emerging from
refactoring with a
focus on the omain
Wrote code with:
Wrote code with:
no naked primitive
Wrote code with:
no naked primitive
no naked collection
Object Calisthenics?
Signal to Noise ratio
http://www.flickr.com/photos/28471130@N07/2666802097
Signal to Noise Ratio
• SNR ≥ 80 %
• Signal (VO): CashFlow.multiply(),
CashFlowSequence.add(), .negate(),
truncate(), Money.add(), .times
(), .opposite(), Fixing,
FinancialProduct, BankHolidays,
ReferenceData
• Noise: CashFlowBuilder,
CompositeEngine, ProductFactory,
BankHolidaysDecorator
WeWant:
Traceability
of processing
No worry!
Just enrich our
types
Just enrich our
types
Just enrich our
types
label field
Monad-ish
No logging needed
Each value stores
its history
Draw on
established
formalisms
Object
Arithmetics
=
Maths structure
Magma/Monoid/
Vector Spaces
Identity Element
= NullObject
Identity Element
= NullObject
VatCalculation.NONE
Wikipedia is
your friend!
In Closing
Invest time:
Learn DDD, and get
free FP exposure
A paradox:
FP influence helps
craft better Object-
Oriented code!
DDD + FP
A love story
http://wadler.blogspot.fr/2008/04/functional-programming-is-beautiful.html
Taste-Driven
Development
TDD
Questions?
You can also ask me later!
@cyriux
Merci
Merci

Contenu connexe

Similaire à I T.A.K.E. talk: "When DDD meets FP, good things happen"

Similaire à I T.A.K.E. talk: "When DDD meets FP, good things happen" (20)

Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
 
Ur Domain Haz Monoids
Ur Domain Haz MonoidsUr Domain Haz Monoids
Ur Domain Haz Monoids
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
Control all the Things with Node-JS
Control all the Things with Node-JSControl all the Things with Node-JS
Control all the Things with Node-JS
 
Apclass
ApclassApclass
Apclass
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Clean code
Clean codeClean code
Clean code
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
program#include iostreamusing namespace std;void calculatio.pdf
program#include iostreamusing namespace std;void calculatio.pdfprogram#include iostreamusing namespace std;void calculatio.pdf
program#include iostreamusing namespace std;void calculatio.pdf
 
Being Expressive in Code
Being Expressive in CodeBeing Expressive in Code
Being Expressive in Code
 
AI&ML Conference 2019 - Deep Learning from zero to hero
AI&ML Conference 2019 - Deep Learning from zero to heroAI&ML Conference 2019 - Deep Learning from zero to hero
AI&ML Conference 2019 - Deep Learning from zero to hero
 
"Quantum" performance effects
"Quantum" performance effects"Quantum" performance effects
"Quantum" performance effects
 
Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)Google App Engine in 40 minutes (the absolute essentials)
Google App Engine in 40 minutes (the absolute essentials)
 
Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020
 
L04 Software Design Examples
L04 Software Design ExamplesL04 Software Design Examples
L04 Software Design Examples
 
Lecture # 1 - Introduction Revision - 1 OOPS.pptx
Lecture # 1 - Introduction  Revision - 1 OOPS.pptxLecture # 1 - Introduction  Revision - 1 OOPS.pptx
Lecture # 1 - Introduction Revision - 1 OOPS.pptx
 
C# Loops
C# LoopsC# Loops
C# Loops
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
 

Plus de Cyrille Martraire

Plus de Cyrille Martraire (17)

Domain modeling for Digital Transformations (FlowCon Paris 2019 edition)
Domain modeling for Digital Transformations (FlowCon Paris 2019 edition)Domain modeling for Digital Transformations (FlowCon Paris 2019 edition)
Domain modeling for Digital Transformations (FlowCon Paris 2019 edition)
 
Sunny Tech 2019 - Craft Forever
Sunny Tech 2019 - Craft ForeverSunny Tech 2019 - Craft Forever
Sunny Tech 2019 - Craft Forever
 
Hexagonal at Scale, with DDD and microservices! - Voxxed Days microservices 2...
Hexagonal at Scale, with DDD and microservices! - Voxxed Days microservices 2...Hexagonal at Scale, with DDD and microservices! - Voxxed Days microservices 2...
Hexagonal at Scale, with DDD and microservices! - Voxxed Days microservices 2...
 
Bounded Context - DDD Europe Foundation Track
Bounded Context - DDD Europe Foundation TrackBounded Context - DDD Europe Foundation Track
Bounded Context - DDD Europe Foundation Track
 
Domain Modeling towards First Principles
Domain Modeling towards First PrinciplesDomain Modeling towards First Principles
Domain Modeling towards First Principles
 
DDD beyond the infamous repository pattern - GeeCon Prague 2018
DDD beyond the infamous repository pattern - GeeCon Prague 2018DDD beyond the infamous repository pattern - GeeCon Prague 2018
DDD beyond the infamous repository pattern - GeeCon Prague 2018
 
DDD for real
DDD for realDDD for real
DDD for real
 
Les effets inattendus du passage en Features Teams à grande échelle -ScrumDay...
Les effets inattendus du passage en Features Teams à grande échelle -ScrumDay...Les effets inattendus du passage en Features Teams à grande échelle -ScrumDay...
Les effets inattendus du passage en Features Teams à grande échelle -ScrumDay...
 
Refactor your Specs - 2017 Edition
Refactor your Specs - 2017 EditionRefactor your Specs - 2017 Edition
Refactor your Specs - 2017 Edition
 
Interviewing Domain Experts - Heuristics From the Trenches (DDD Europe 2016 M...
Interviewing Domain Experts - Heuristics From the Trenches (DDD Europe 2016 M...Interviewing Domain Experts - Heuristics From the Trenches (DDD Europe 2016 M...
Interviewing Domain Experts - Heuristics From the Trenches (DDD Europe 2016 M...
 
DDD patterns that were not in the book
DDD patterns that were not in the bookDDD patterns that were not in the book
DDD patterns that were not in the book
 
Living Documentation (NCrafts Paris 2015, DDDx London 2015, BDX.io 2015, Code...
Living Documentation (NCrafts Paris 2015, DDDx London 2015, BDX.io 2015, Code...Living Documentation (NCrafts Paris 2015, DDDx London 2015, BDX.io 2015, Code...
Living Documentation (NCrafts Paris 2015, DDDx London 2015, BDX.io 2015, Code...
 
Legacy Code: Evolve or Rewrite?
Legacy Code: Evolve or Rewrite?Legacy Code: Evolve or Rewrite?
Legacy Code: Evolve or Rewrite?
 
Refactor your specs! Øredev 2013
Refactor your specs! Øredev 2013Refactor your specs! Øredev 2013
Refactor your specs! Øredev 2013
 
DDD session BrownBagLunch (FR)
DDD session BrownBagLunch (FR)DDD session BrownBagLunch (FR)
DDD session BrownBagLunch (FR)
 
Domain-Driven Design in legacy application
Domain-Driven Design in legacy applicationDomain-Driven Design in legacy application
Domain-Driven Design in legacy application
 
Tour d'horizon de Domain-Driven Design Avril 2012 autour d'un retour d'expéri...
Tour d'horizon de Domain-Driven Design Avril 2012 autour d'un retour d'expéri...Tour d'horizon de Domain-Driven Design Avril 2012 autour d'un retour d'expéri...
Tour d'horizon de Domain-Driven Design Avril 2012 autour d'un retour d'expéri...
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Dernier (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 

I T.A.K.E. talk: "When DDD meets FP, good things happen"