Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Types and Refactoring
Search
Penelope Phippen
July 21, 2014
Technology
220
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
630
How RSpec Works
penelope_zone
0
6.9k
Quick and easy browser testing using RSpec and Rails 5.1
penelope_zone
1
120
Teaching RSpec to play nice with Rails
penelope_zone
2
190
Little machines that eat strings
penelope_zone
1
150
What is processor (brighton ruby edition)
penelope_zone
0
150
What is processor?
penelope_zone
1
390
extremely defensive coding - rubyconf edition
penelope_zone
0
300
Agile, etc.
penelope_zone
2
270
Other Decks in Technology
See All in Technology
SDDの運用にめげずに向き合った話
sansantech
PRO
1
140
3つのアプローチで目指す チームを強くするAI駆動開発
thkt
0
120
生成AIを使って「人が」考える技術 ― AI時代の人機共想と実践ノウハウ|UNITT AC2026
ishiirikie
0
560
しろおびから始める!ログ活用の第一歩
shumei_ito
0
550
Codex概要
ymiya55
0
460
AI時代のAPI開発を加速する品質ガードレール / API Quality Guardrails in the AI Era
yokawasa
0
160
ミイダス株式会社 テックチームのご紹介 / MIIDAS Tech Team
miidas
0
160
無理せず続ける、子育てエンジニアの学び
codmoninc
PRO
0
370
freeeらしさをAIとともに作る / Creating the freee Experience with AI
ymrl
0
210
生成AIと進める探索的データ分析(2026.10.03 第122回Tokyo.R勉強会)
tatamiya
2
1.4k
自分で立ててみるLLMサービス
y_sera15
0
140
AIは爆速なのに、私が詰まっていた話 ― 音声入力と鳴くマスコットでボトルネックを削る
yama3133
0
530
Featured
See All Featured
BBQ
matthewcrist
90
10k
Deep Space Network (abreviated)
tonyrice
0
360
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
270
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
My Coaching Mixtape
mlcsv
0
340
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
350
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
550
It's Worth the Effort
3n
188
29k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.7k
Rails Girls Zürich Keynote
gr2m
96
14k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
550
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]