Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # Weekly Iteration - Primitive Obsession
  2.  
  3. ## What is it?
  4.  
  5. ## What does it look like in Ruby?
  6.  
  7. Over relying on hashes/arrays
  8.  
  9. ```ruby
  10. [5, 30]
  11. ```
  12.  
  13. ```ruby
  14. 1930
  15. ```
  16.  
  17. ```ruby
  18. { name: "Joël", age: 42 }
  19. ```
  20.  
  21. ## Why is it a bad idea?
  22.  
  23. * Harder to read
  24. * Many domain concepts combined together
  25. * In Ruby, need to define methods somewhere
  26. * Allows representing invalid values
  27. * Hard to change
  28.  
  29. ## What are the solutions?
  30.  
  31. More objects
  32.  
  33. ```ruby
  34. class Money
  35. def from_cents(cents)
  36. new(cents / 100, cents % 100)
  37. end
  38.  
  39. def initialize(dollars, cents)
  40. @dollars = dollars
  41. @cents = cents
  42. end
  43. end
  44. ```
  45.  
  46. ## What about functional languages like Elixir?
  47.  
  48. We don't have objects so what's the alternative?
  49.  
  50. ## Discuss custom types in Haskell/Elm
  51.  
  52. Compilers reward you for avoiding primitives
  53.  
  54. ```elm
  55. type Seach =
  56. { query : Maybe String
  57. , results : Maybe (List String)
  58. }
  59. ```
  60.  
  61. ```elm
  62. type Search
  63. = NotStarted
  64. | TermSelected String
  65. | ResultsFetched String (List String)
  66. ```
  67.  
  68. ## General ideas
  69.  
  70. Try to program at a higher level of abstraction with domain concepts rather than
  71. language primitives.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement