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
570
How RSpec Works
penelope_zone
0
6.7k
Quick and easy browser testing using RSpec and Rails 5.1
penelope_zone
1
86
Teaching RSpec to play nice with Rails
penelope_zone
2
150
Little machines that eat strings
penelope_zone
1
110
What is processor (brighton ruby edition)
penelope_zone
0
120
What is processor?
penelope_zone
1
360
extremely defensive coding - rubyconf edition
penelope_zone
0
270
Agile, etc.
penelope_zone
2
230
Other Decks in Technology
See All in Technology
Claude Codeを駆使した初めてのiOSアプリ開発 ~ゼロから3週間でグローバルハッカソンで入賞するまで~
oikon48
10
5.3k
Digitization部 紹介資料
sansan33
PRO
1
5.6k
現場データから見える、開発生産性の変化コード生成AI導入・運用のリアル〜 / Changes in Development Productivity and Operational Challenges Following the Introduction of Code Generation AI
nttcom
1
430
Microsoft 365 の認証と承認を理解する / Understanding Microsoft 365 Authentication and Authorization
karamem0
0
110
Biz職でもDifyでできる! 「触らないAIワークフロー」を実現する方法
igarashikana
3
1.3k
Databricks AI/BI Genie の「値ディクショナリー」をAmazonの奥地(S3)まで見に行く
kameitomohiro
1
380
Wasmの気になる最新情報
askua
0
180
Implementing and Evaluating a High-Level Language with WasmGC and the Wasm Component Model: Scala’s Case
tanishiking
0
170
AI時代、“平均値”ではいられない
uhyo
8
2.1k
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
12
81k
事業開発におけるDify活用事例
kentarofujii
4
1.2k
個人でデジタル庁の デザインシステムをVue.jsで 作っている話
nishiharatsubasa
1
550
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
353
21k
KATA
mclloyd
PRO
32
15k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
990
A Modern Web Designer's Workflow
chriscoyier
697
190k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
30
2.9k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Designing for humans not robots
tammielis
254
26k
Building Better People: How to give real-time feedback that sticks.
wjessup
369
20k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
54k
A Tale of Four Properties
chriscoyier
161
23k
Fireside Chat
paigeccino
40
3.7k
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]