Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Types and Refactoring
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Penelope Phippen
July 21, 2014
Technology
210
3
Share
Types and Refactoring
A talk about types and refactoring I gave at Brighton Ruby 2014.
Penelope Phippen
July 21, 2014
More Decks by Penelope Phippen
See All by Penelope Phippen
Introducing Rubyfmt
penelope_zone
0
610
How RSpec Works
penelope_zone
0
6.8k
Quick and easy browser testing using RSpec and Rails 5.1
penelope_zone
1
100
Teaching RSpec to play nice with Rails
penelope_zone
2
170
Little machines that eat strings
penelope_zone
1
130
What is processor (brighton ruby edition)
penelope_zone
0
140
What is processor?
penelope_zone
1
380
extremely defensive coding - rubyconf edition
penelope_zone
0
290
Agile, etc.
penelope_zone
2
250
Other Decks in Technology
See All in Technology
20260428_Product Management Summit_tadokoroyoshiro
tadokoro_yoshiro
14
15k
今年注目する!データ分析プラットフォームでのAIの活用
nayuts
0
170
エージェントスキルを作って自分のインプットに役立てよう
tsubakimoto_s
0
470
Good Enough Types: Heuristic Type Inference for Ruby
riseshia
1
320
データ定義の混乱と戦う 〜 管理会計と財務会計 〜
wonohe
0
150
AIが自律的に働く時代へ Amazon Quick で実現するAIエージェント紹介
koheiyoshikawa
0
140
M5Stack CoreS3とZephyr(RTOS)で Edge AIっぽいことしてみた
iotengineer22
0
370
VespaのParent Childを用いたフィードパフォーマンスの改善
taking
0
120
EMから幅を広げるために最近挑戦していること / Recent challenges I'm undertaking to expand my horizons beyond EM
hiro_torii
1
150
Google Cloud Next '26 の裏でこっそりリリースされたCloud Number Registry & Cloud Hub コスト分析 を試してみた
hikaru1001
0
110
Agents CLI と Gemini Enterprise Agent Platform で マルチエージェント開発が楽しくなる!
kaz1437
0
170
ハーネスエンジニアリングの概要と設計思想
sergicalsix
9
5.5k
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
360
Visualization
eitanlees
150
17k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The untapped power of vector embeddings
frankvandijk
2
1.7k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.2k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
The Invisible Side of Design
smashingmag
303
52k
Building AI with AI
inesmontani
PRO
1
920
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
160
Transcript
Types & Refactoring
a!/samphippen
Types
I think type systems are great!
I think dynamic languages are great!
I think thinking about types in dynamic languages is great!
RUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUBYYYYYYYYYYYYYY YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
No type systems here
NoMethodError: undefined method `foo' for nil:NilClass
No explicit type systems here
What is a type?
Concrete implementation of some data and behaviour
Class
1.class # => FixNum
(1.0).class # => Float
What is a typeclass?
A set of methods satisfied by many types
Interface
(1+1.0).class # => Float (1.0+1).class # => Float
But also things like
Positive Int + Positive Int # => Positive Int
some_collection.count
String Hash Array
Form a typeclass (or interface) under count with no args
Methods that return consistent interfaces are great
The problem
We’re not forced to return a single interface
If interface is inconsistent, code must check types further down
For example returning nil
nil is not type classed the same as your object!
pony = Pony.find_by(:key => value) if pony pony.neigh else puts
“pony was nil” end
The if is a subtle form of type checking.
Type checking is the antithesis of OO
Can we get a consistent interface?
Refactoring
Null object
class Pony def horse_power 0.5 end end
Pony.find_by( :key => value ) || NullPony.new
class NullPony def horse_power 0 end end
Decides default for horse_power at define time
May or may not be what you want
Maybe
Steal ideas from functional programming!
Decide defaults at run time
Maybe is a type class
#map(&blk) -> maybe #value_or(x) -> Object
class Just def initialize(value) @value = value end def map(&blk)
def value_or(x) Just.new(blk.call(@value)) @value end end end
class Nothing def map(&blk) self end def value_or(x) x end
end
Map until done with transforms
use maybe.value_or(“something”)
Recap
Methods that return nil are almost always inconsistently typed
Use a null object to replace a nil if you
know defaults at define time
Use maybe if you want to set defaults at run
time
RSpec RSpec ! ! RSpec 3
tinyurl.com/ sambr2014
Let’s have some questions a!/samphippen
[email protected]