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
Penelope Phippen
July 21, 2014
Technology
3
200
Types and Refactoring
A talk about types and refactoring I gave at Brighton Ruby 2014.
Penelope Phippen
July 21, 2014
Tweet
Share
More Decks by Penelope Phippen
See All by Penelope Phippen
Introducing Rubyfmt
penelope_zone
0
590
How RSpec Works
penelope_zone
0
6.8k
Quick and easy browser testing using RSpec and Rails 5.1
penelope_zone
1
98
Teaching RSpec to play nice with Rails
penelope_zone
2
160
Little machines that eat strings
penelope_zone
1
120
What is processor (brighton ruby edition)
penelope_zone
0
130
What is processor?
penelope_zone
1
370
extremely defensive coding - rubyconf edition
penelope_zone
0
280
Agile, etc.
penelope_zone
2
240
Other Decks in Technology
See All in Technology
All About Sansan – for New Global Engineers
sansan33
PRO
1
1.3k
制約が導く迷わない設計 〜 信頼性と運用性を両立するマイナンバー管理システムの実践 〜
bwkw
3
920
MCPでつなぐElasticsearchとLLM - 深夜の障害対応を楽にしたい / Bridging Elasticsearch and LLMs with MCP
sashimimochi
0
160
生成AIを活用した音声文字起こしシステムの2つの構築パターンについて
miu_crescent
PRO
2
180
顧客の言葉を、そのまま信じない勇気
yamatai1212
1
350
AzureでのIaC - Bicep? Terraform? それ早く言ってよ会議
torumakabe
1
520
AI駆動開発を事業のコアに置く
tasukuonizawa
1
160
今日から始めるAmazon Bedrock AgentCore
har1101
4
400
OWASP Top 10:2025 リリースと 少しの日本語化にまつわる裏話
okdt
PRO
3
640
日本の85%が使う公共SaaSは、どう育ったのか
taketakekaho
1
150
Cosmos World Foundation Model Platform for Physical AI
takmin
0
830
SREのプラクティスを用いた3領域同時 マネジメントへの挑戦 〜SRE・情シス・セキュリティを統合した チーム運営術〜
coconala_engineer
2
630
Featured
See All Featured
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
180
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
640
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
77
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
730
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
Building an army of robots
kneath
306
46k
Navigating Weather and Climate Data
rabernat
0
100
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
120
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
170
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Leo the Paperboy
mayatellez
4
1.4k
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]