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
extremely defensive coding - rubyconf edition
Search
Penelope Phippen
November 16, 2015
Technology
0
250
extremely defensive coding - rubyconf edition
Penelope Phippen
November 16, 2015
Tweet
Share
More Decks by Penelope Phippen
See All by Penelope Phippen
Introducing Rubyfmt
penelope_zone
0
530
How RSpec Works
penelope_zone
0
6.4k
Quick and easy browser testing using RSpec and Rails 5.1
penelope_zone
1
75
Teaching RSpec to play nice with Rails
penelope_zone
2
120
Little machines that eat strings
penelope_zone
1
80
What is processor (brighton ruby edition)
penelope_zone
0
94
What is processor?
penelope_zone
1
340
Agile, etc.
penelope_zone
2
210
Extremely Defensive Coding
penelope_zone
0
86
Other Decks in Technology
See All in Technology
小規模に始めるデータメッシュとデータガバナンスの実践
kimujun
3
590
物価高なラスベガスでの過ごし方
zakky
0
380
Amazon_CloudWatch_ログ異常検出_導入ガイド
tsujiba
4
1.6k
プロダクト成長に対応するプラットフォーム戦略:Authleteによる共通認証基盤の移行事例 / Building an authentication platform using Authlete and AWS
kakehashi
1
150
最速最小からはじめるデータプロダクト / Data Product MVP
amaotone
5
730
とあるユーザー企業におけるリスクベースで考えるセキュリティ業務のお話し
4su_para
3
320
pandasはPolarsに性能面で追いつき追い越せるのか
vaaaaanquish
4
4.5k
大規模データ基盤チームのオンプレTiDB運用への挑戦 / dpu-tidb
cyberagentdevelopers
PRO
1
110
使えそうで使われないCloudHSM
maikamibayashi
0
170
プロダクトチームへのSystem Risk Records導入・運用事例の紹介/Introduction and Case Studies on Implementing and Operating System Risk Records for Product Teams
taddy_919
1
170
Jr. Championsになって、強く連携しながらAWSをもっと使いたい!~AWSに対する期待と行動~
amixedcolor
0
190
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
290k
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
KATA
mclloyd
29
13k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
3
370
Music & Morning Musume
bryan
46
6.1k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Adopting Sorbet at Scale
ufuk
73
9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Transcript
Extremely Defensive Coding
a!/samphippen
Who is having fun?
Who is here for their first time?
I want to start with a story
“When is it OK to override the methods on object?”
I umm’d and ahh’d for a bit
I gave a half explanation and then asked them if
they understood
I eventually came to an answer to do with consistency
Do it when it makes your object more consistent with
Ruby, not less
e.g. an == on a data object
What makes a gem good?
Internals?
Internals?
Interface?
Interface? ✅
More convenient than if I did it myself
And it stays convenient
And it can be used by everyone on a team
And it works with a wide range of Ruby codebases
RSpec
RSpec Definitely always convenient
RSpec Definitely always convenient
User story
As an RSpec user
When I stub an object
I want the original method to be on that object
after the example
So that my objects aren’t broken by my test suite
As an RSpec user
When I stub an object
I want the original method to be on that object
after the example
So that my objects aren’t broken by my test suite
How does that work?
allow(cat).to receive(:meow)
Takes the meow method off cat
Saves it
Executes test
Puts the method back on the original object
How do you save a method?
None
Method object
Put it somewhere else
Put it back at end of test
The end
The end
Defensive coding
RSpec::Support .method_handle_for
Simply invoking the method method is not good enough
Some scenes may have been altered/accelerated for your viewing pleasure
Let’s have some questions !/samphippen
[email protected]
Users Lie
def method ‘get’ end
Users can redefine anything at any time in Ruby
and that’s fine
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Instance method object Method without a target object
Comes from a class/module not an instance
Grab the Kernel implementation of #method
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Users can and will redefine core methods at any time
Instead we use Kernel’s implementation
Nobody screws with Kernel
Ruby interpreters lie
Some objects do not have Kernel in their inheritance chain
None
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Rebinding module methods
c
c
RSpec does not support Rubinius
None
We tried, we really really tried
So anyway, dealing with module methods
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Ruby interpreters behave differently.
Let’s have some questions !/samphippen
[email protected]
Sometimes users don’t lie
None
None
None
None
`method': undefined method `foo' for class `Foo' (NameError)
This is a catch 22
Solution: trust but verify
None
None
None
Let’s put it all together
None
Wrapping up
This is why I love RSpec
Please file bugs
Your gem should be defensive
Users lie Redefinitions come from anywhere, expect them
Ruby interpreters lie Your code will run on non-MRI, old
MRI, etc
Sometimes users don’t lie Users are weird, trust but verify
What makes a gem good?
Internals?
That explanation made no sense
Internals?
Interface?
allow(cat).to receive(:meow)
You get the complexity of RSpec working with any object
for free
You probably didn’t even know it was there until I
just told you
Interface? ✅
Gems
Gems provide strong abstraction boundaries
Done badly they cause an even more extreme mess
Done correctly they hide huge complexity behind well defined barriers
Defending against users helps your gem be convenient
Write defensively, and your users will never know what’s inside
the box
Remember the story?
This talk is born out of Ruby’s power
We need our code to defend today, against the mistakes
of our tomorrow
Let’s have some questions !/samphippen
[email protected]
Let’s have some questions !/samphippen
[email protected]
Minitest Mock is 169 lines of code
I just showed you more code than that
For a tiny feature that isn’t a complete mocking library
and that’s fine
Please check out https://browserpath.co
None
Let’s have some questions a!/samphippen
[email protected]