Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Domain typing in Ruby

170 views

Published on

If you follow DDD approach, you understand that the domain of an application is what really matters.
The better you express it, the more maintainable application you get. Common approaches for modelling of real business processes doesn’t work well on big scale. Single model of Customer with all possible states and attributes in one place quickly turns it into mess. Here is the place where Types Algebra from Functional programming comes helpful. And it can be implemented in Ruby in a decent way using dry-types from the great dry-rb toolkit.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Domain typing in Ruby

  1. 1. Domain Typing in Ruby
  2. 2. HELLO! I am Sergiy Kukunin Feel free to follow me at @sergey_kukunin 2
  3. 3. Plan Setting context of the problem A lot of ruby code (small snippets) Conclusions Questions and Answers 3
  4. 4. This talk contains functional programming 4
  5. 5. This talk contains domain-driven design 5
  6. 6. I was on RubyC 2017 6
  7. 7. Two values of software 7
  8. 8. “I now suggest that we confine ourselves to the design and implementation of intellectually manageable programs © Edsger W. Dijkstra, 1972 88
  9. 9. Intellectually manageable programs 9
  10. 10. Safety
  11. 11. “How to shoot yourself in the foot using Perl You shoot yourself in the foot, but nobody can understand how you did it. Six months later, neither can you. 1111
  12. 12. Safety improves intellectual manageability 12
  13. 13. Safety matters ▰Assembler is hard to manage ▰Goto statements are banned because of complexity ▰Eval is almost banned 13
  14. 14. Typing
  15. 15. Typing aims for better safety 15
  16. 16. I ain’t gonna make Java in Ruby 16
  17. 17. Disclaimer 17 Inspired by F# https://fsharpforfunandprofit.com/ddd/
  18. 18. Functional programming conquers ▰Avoid (global) mutable state ▰Composability over Inheritance ▰Service objects and data bags ▰Lambdas 18
  19. 19. How it looks in F# 19 module CardGame = type Suit = Club | Diamond | Spade | Heart type Rank = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace type Card = Suit * Rank type Hand = Card list type Deck = Card list type Player = {Name:string; Hand:Hand} type Game = {Deck:Deck; Players: Player list} type Deal = Deck -> (Deck * Card) type PickupCard = (Hand * Card) -> Hand
  20. 20. 20
  21. 21. 21
  22. 22. 22
  23. 23. 23
  24. 24. 24
  25. 25. 25
  26. 26. Types Algebra 26 In theory
  27. 27. Type is a set of possible values type Suit = Club | Diamond | Spade | Heart type Rank = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace type Card = Suit * Rank 27
  28. 28. 28
  29. 29. 29
  30. 30. 30
  31. 31. 31
  32. 32. 32
  33. 33. 33
  34. 34. 34
  35. 35. 35
  36. 36. In F# it’s simple 36
  37. 37. 37
  38. 38. Let’s go deeper 38
  39. 39. 39
  40. 40. “A contact must have at least one contact information” Implies ▰email address only, or ▰postal address only, or ▰both email and postal addresses 40
  41. 41. 41
  42. 42. Meanwhile in F# 42
  43. 43. “A contact must have at least one contact information” 43 Is it really what business wants?
  44. 44. 44
  45. 45. States and Transitions 45
  46. 46. States and transitions for shopping cart 46 EmptyCart ActiveCart PaidCart Pay Add ItemAdd Item Remove ItemRemove Item
  47. 47. 47
  48. 48. What’s about behaviour?
  49. 49. Object-oriented style 49
  50. 50. Functional style 50
  51. 51. 51
  52. 52. Why is that cool? Each state has own set of data All states are explicitly documented All transitions are explicitly documented It’s safe because it makes you think about any combination 52
  53. 53. Caveats Non-native support Indistinguishable types Requires mindshift Complex persistence aspect In-mature approach in Ruby 53
  54. 54. ● dry-types ● dry-struct ● dry-logic It’s possible thankfully to dry-rb http://dry-rb.org/
  55. 55. Further reading ▰Functional programming ▰Domain-driven design ▰Typing Algebra ▰dry-rb and rom-rb toolkits ▰F# as friendly functional language 55
  56. 56. 56 THANKS! Any questions? You can find me at @sergey_kukunin & sergey.kukunin@gmail.com

×