O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Ur Domain Haz Monoids DDDx NYC 2014

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Ur Domain Haz Monoids
Ur Domain Haz Monoids
Carregando em…3
×

Confira estes a seguir

1 de 176 Anúncio

Ur Domain Haz Monoids DDDx NYC 2014

Baixar para ler offline

You probably can't imagine that Monoids (not monads) are so simple maths creatures that you can understand them in just a few minutes.

You probably can't imagine that Monoids (not monads) are so simple maths creatures that you can understand them in just a few minutes.

But you probably don't imagine either that they can help you craft elegant and powerful domain models that scale very well.

Through various examples, we will have a closer look at monoids used for domain modeling in a style that mixes the best of DDD and FP. Even in languages like Java or C#, this talk will influence your coding style forever!

'More entertaining and educational explanation of Monoids I've heard' - Martin Thompson, DDD exchange London 2014.

See more at http://skillsmatter.com/conferences/1880-ddd-exchange-nyc-2014#program

You probably can't imagine that Monoids (not monads) are so simple maths creatures that you can understand them in just a few minutes.

You probably can't imagine that Monoids (not monads) are so simple maths creatures that you can understand them in just a few minutes.

But you probably don't imagine either that they can help you craft elegant and powerful domain models that scale very well.

Through various examples, we will have a closer look at monoids used for domain modeling in a style that mixes the best of DDD and FP. Even in languages like Java or C#, this talk will influence your coding style forever!

'More entertaining and educational explanation of Monoids I've heard' - Martin Thompson, DDD exchange London 2014.

See more at http://skillsmatter.com/conferences/1880-ddd-exchange-nyc-2014#program

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Semelhante a Ur Domain Haz Monoids DDDx NYC 2014 (20)

Mais de Cyrille Martraire (18)

Anúncio
Anúncio

Ur Domain Haz Monoids DDDx NYC 2014

  1. 1. Ur Domain Haz Monoids? DDDx NYC 14 cyrille martraire aka @cyriux
  2. 2. Can you tell the difference?
  3. 3. Monoid
  4. 4. Glass of beer
  5. 5. This talk is the answer
  6. 6. DDD
  7. 7. FP
  8. 8. (00)
  9. 9. A matter of Taste http://rosshirt.blogspot.fr/ The Code Gourmet (dedicated to @ziobrando)
  10. 10. Passionate developer PARIS Since 1999 ! @cyriux Cyrille Martraire
  11. 11. Paris Software Craftsmanship Community http://www.meetup.com/paris-software-craftsmanship/
  12. 12. TDD BDD DDD Legacy
  13. 13. 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.
  14. 14. a bit personal
  15. 15. Adopted 2005 Still in love
  16. 16. ...
  17. 17. What do DDD & FP have in common?
  18. 18. "My first encounter with FP concepts was from DDD"
  19. 19. FP = ?
  20. 20. http://www.jaider.net/archives/609-intro-to-functional-programming/ PURE
  21. 21. No State No War
  22. 22. So what do DDD & FP have in common?
  23. 23. Value Objects................................................................................................. 19
  24. 24. Learn one and get the other one for FREE!
  25. 25. DDD FP (OO)
  26. 26. NICE STYLE of code
  27. 27. DDD+FP = ?
  28. 28. Example PLZ?
  29. 29. MONOIDZ!
  30. 30. @cyriux
  31. 31. Closure Associativity Neutral Element
  32. 32. only 3 numbers in programming
  33. 33. 0, 1, MANY
  34. 34. Monoid: encapsulate diversity inside
  35. 35. Neutral Element 0, 1, MANY Element Operation
  36. 36. Encapsulate special cases → simple again
  37. 37. Battle against complexity
  38. 38. × × × …
  39. 39. × × × …
  40. 40. × × × …
  41. 41. Applied often: → scalable process → can grow to very complex complexity
  42. 42. Scalable in complexity
  43. 43. OO FTW! For a given interface I find myself doing often 0, 1, Many
  44. 44. NullObject Composite 0, 1, MANY Implementation(s)
  45. 45. Identity Element = NullObject VatCalculation.NONE
  46. 46. Example PLZ!
  47. 47. Numbers int+int=int (3+5)+2=3+(5+2) 0
  48. 48. Lists (.)+(.,.)=(.,.,.) (a︎)+(b︎,c)=(a︎, b)+(c) ()
  49. 49. Strings "hello"+"world" "cy"+"ri"+"lle" ""
  50. 50. look simplistic; the key to very complex behavior
  51. 51. The key to infinite scalability! (space)
  52. 52. The key to infinite incremental computing! (time)
  53. 53. Hadoop x Storm
  54. 54. Composeability
  55. 55. Monoids ☛ reduce
  56. 56. Monoids ☛ domain
  57. 57. Monoids ☛ domain
  58. 58. Monoids: typical FP
  59. 59. FP: Everything is a value
  60. 60. PROOF Therefore: Monoids are values!
  61. 61. VALUE OBJECTS
  62. 62. Immutable Equals by value
  63. 63. Value Object A DDD pattern to import FP-ish values in OO languages
  64. 64. Not Anemic
  65. 65. 18 m + 16 m = 34 m
  66. 66. Change -> new instance returns a new instance
  67. 67. SIDE-EFFECT -free FUNCTIONS
  68. 68. Immutability & Side-effect-free functions!
  69. 69. “Functional-First” style
  70. 70. “Functional-First” style 90% Value Objects (Ok, gut feeling, I did’nt measure)
  71. 71. Money
  72. 72. (25, EUR) + (30, EUR) = (55, EUR)
  73. 73. (25, EUR) + (30, USD) = exception
  74. 74. Money <<ValueObject>> amount currency add(Money): Money
  75. 75. Cashflows (Payments)
  76. 76. (25, EUR, today) + (30, EUR, today) = (55, EUR, today)
  77. 77. (25, EUR, today) + (30, EUR, next day) = exception
  78. 78. CashFlow <<ValueObject>> amount currency date add(CashFlow):CashFlow
  79. 79. CLOSURE of OPERATION
  80. 80. Cashflows Sequences
  81. 81. (30, EUR, 21/03) (10, EUR, 20/03) (25, EUR, 21/03) (12, EUR, 22/03) (10, EUR, 20/03) (55, EUR, 21/03) (12, EUR, 22/03) + =
  82. 82. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  83. 83. Object Arithmetics
  84. 84. This is how domain experts THINK about it SAY SKETCH
  85. 85. Ranges
  86. 86. [1, 3] Union* [2, 4] = [1, 4] * for a bounding box-kind definition of union
  87. 87. [1, 3] Union [2, 4] = [1, 4] ][
  88. 88. Predicates
  89. 89. Filter AND Filter = Filter
  90. 90. Filter AND Filter = Filter Always True
  91. 91. Filter OR Filter = Filter
  92. 92. Filter OR Filter = Filter Always False
  93. 93. Grants
  94. 94. Read, Write, Execute ! "most secure wins" ! r + w = r w + x = w
  95. 95. Configuration Maps
  96. 96. Default Desk-specific Color BLUE Enable True Timeout 30 + = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX
  97. 97. Default Desk-specific Color BLUE Enable True Timeout 30 + = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX overwrite logical OR min
  98. 98. Default Desk-specific Color BLUE Enable True Timeout 30 + Values are monoids too! = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX overwrite logical OR min
  99. 99. Color BLUE Enable True Timeout 30 + Neutral Element = Color NONE Enable False Timeout +∞ Desk ALL Color BLUE Enable True Timeout 30
  100. 100. Color BLUE Enable True Timeout 30 + Neutral Element = Color BLUE Enable True Timeout 30
  101. 101. Color BLUE Enable True Timeout 30 + Value Objects may be *BIG* object trees! (DOM) = Color BLUE Enable True Timeout 30
  102. 102. Non-Linear Stuff (average, std dev, K-clustering, barycenters...)
  103. 103. Average + Average = WRONG
  104. 104. Average + Average Not Composeable!
  105. 105. avg = sum / count ! avg = sum / count !
  106. 106. avg = sum / count ! avg = sum / count ! +
  107. 107. avg = sum / count ! avg = sum / count ! + +
  108. 108. avg = sum / count + = + = ! avg = sum / count ! avg = sum / count
  109. 109. (sum, count) ! + = (sum, count) ! (sum, count) Average
  110. 110. (sum, sum2, count) + = ! (sum, sum2, count) ! (sum, sum2, count) Std deviation
  111. 111. Can model as a monoid even non-linear stuff!
  112. 112. MOAR MATHS PLZ!
  113. 113. Monoid several times... MailingList toString(): String union(MailingList): MailingList intersection(MailingList): MailingList nobody(): MailingList everybody(): MailingList
  114. 114. Space Vectors
  115. 115. average temperature! = t1.add(t2)! .scale(1/2)
  116. 116. Change -> new instance returns a new instance
  117. 117. Space Vector Temperature toCelsius(): Temperature toFarenheit(): Temperature add(Temperature): Temperature scale(double): Temperature
  118. 118. Why is it useful?
  119. 119. (30, EUR, 21/03) (10, EUR, 20/03) (25, EUR, 21/03) (12, EUR, 22/03) (10, EUR, 20/03) (55, EUR, 21/03) (12, EUR, 22/03) + =
  120. 120. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  121. 121. This is how domain experts THINK about it SAY SKETCH
  122. 122. DECLARATIVE STYLE
  123. 123. Much less code Much less bugs
  124. 124. // without monoids PaymentsFees(...) PaymentsFeesWithOptions(...) PaymentsFeesWithInsuranceAndOptions(...) PaymentsFeesWithInsurance(...) NoFeesButInsurance(...) ... ! // with monoids fees(...) : Payments options(...) : Payments insurance(...) : Payments ! Payments.add(Payments) : Payments
  125. 125. // without monoids PaymentsFees(...) PaymentsFeesWithOptions(...) PaymentsFeesWithInsuranceAndOptions(...) PaymentsFeesWithInsurance(...) NoFeesButInsurance(...) ...
  126. 126. // with monoids fees(...) : Payments options(...) : Payments insurance(...) : Payments ! Payments.add(Payments) : Payments
  127. 127. Very easy to test side-effect-free operation input output
  128. 128. Much less stuff to learn
  129. 129. 1 interface to rule them all
  130. 130. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  131. 131. ESTABLISHED FORMALISMS
  132. 132. Monoid/Vector Spaces/Cyclic Group
  133. 133. Literature Documented
  134. 134. @annotations MailingList @Monoid(neutral="emptyList") intersection(MailingList): MailingList ! emptyList(): MailingList
  135. 135. LIVING DOCUMENTATION
  136. 136. Write code that tells the business domain stories
  137. 137. generic naming MailingList @Monoid(neutral="emptyList") intersection(MailingList): MailingList ! emptyList(): MailingList
  138. 138. Domain-Specific naming MailingList @Monoid(neutral="nobody") overlapping(MailingList): MailingList ! nobody(): MailingList
  139. 139. SELF-EXPLAINING VALUES
  140. 140. We Want: Traceability of processing
  141. 141. No worry!
  142. 142. Just enrich our types
  143. 143. Just enrich our types
  144. 144. Just enrich our types ! label field
  145. 145. Monad-ish No logging needed Each value stores its history
  146. 146. http://stuartcook.files.wordpress.com/2010/11/happy-monkey.jpg
  147. 147. In Closing
  148. 148. Composeability Composeability Composeability Composeability Composeability Composeability
  149. 149. Invest time: Learn DDD, and get free FP exposure
  150. 150. A paradox: FP influence helps craft better Object- Oriented code!
  151. 151. So simple most people have no appreciation of that!
  152. 152. Monoids are good: Eat Them!
  153. 153. LOOK 4 Ur DOMAIN MONOIDZ!
  154. 154. Also learn other maths structures
  155. 155. Wikipedia is your friend!
  156. 156. DDD+FP =
  157. 157. http://wadler.blogspot.fr/2008/04/functional-programming-is-beautiful.html
  158. 158. TDD Taste-Driven Development @cyriux
  159. 159. Questions? Did you try similar things too? Let’s discuss! @cyriux
  160. 160. Follow me @cyriux ! Slides: slideshare.net/cyriux Blog: cyrille.martraire.com ! In Paris? Join !
  161. 161. Merci
  162. 162. Abstractions in the small that compose at large

×