Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Weekly Iteration - Primitive Obsession
- ## What is it?
- ## What does it look like in Ruby?
- Over relying on hashes/arrays
- ```ruby
- [5, 30]
- ```
- ```ruby
- 1930
- ```
- ```ruby
- { name: "Joël", age: 42 }
- ```
- ## Why is it a bad idea?
- * Harder to read
- * Many domain concepts combined together
- * In Ruby, need to define methods somewhere
- * Allows representing invalid values
- * Hard to change
- ## What are the solutions?
- More objects
- ```ruby
- class Money
- def from_cents(cents)
- new(cents / 100, cents % 100)
- end
- def initialize(dollars, cents)
- @dollars = dollars
- @cents = cents
- end
- end
- ```
- ## What about functional languages like Elixir?
- We don't have objects so what's the alternative?
- ## Discuss custom types in Haskell/Elm
- Compilers reward you for avoiding primitives
- ```elm
- type Seach =
- { query : Maybe String
- , results : Maybe (List String)
- }
- ```
- ```elm
- type Search
- = NotStarted
- | TermSelected String
- | ResultsFetched String (List String)
- ```
- ## General ideas
- Try to program at a higher level of abstraction with domain concepts rather than
- language primitives.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement