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
Debugging: The Science of Deduction
Search
Danielle Sucher
November 14, 2013
Programming
7
1.2k
Debugging: The Science of Deduction
Tips for debugging code in Ruby.
Danielle Sucher
November 14, 2013
Tweet
Share
More Decks by Danielle Sucher
See All by Danielle Sucher
Debugging: The Science of Deduction (2014)
daniellesucher
1
270
Negotiation for Hackers
daniellesucher
5
770
Other Decks in Programming
See All in Programming
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
受け取る人から提供する人になるということ
little_rubyist
0
230
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
610
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Bash Introduction
62gerente
608
210k
Designing for Performance
lara
604
68k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
A Modern Web Designer's Workflow
chriscoyier
693
190k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Building Your Own Lightsaber
phodgson
103
6.1k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Transcript
THE SCIENCE OF DEDUCTION
[email protected]
• @DanielleSucher DEBUGGING Friday, November
15, 13
"People write programs without any expectation that they will be
right the first time." - David Lorge Parnas Friday, November 15, 13
Friday, November 15, 13
Let’s start simple. Friday, November 15, 13
When you get an error message Friday, November 15, 13
“Oh, I think I know what went wrong..!” Friday, November
15, 13
NO. Friday, November 15, 13
READ THE ERROR MESSAGE Friday, November 15, 13
/Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/attribute_assigner.rb:14:in `tap' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/attribute_assigner.rb:14:in `object' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/evaluation.rb:12:in `object' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/
lib/factory_girl/strategy/build.rb:9:in `result' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/factory.rb:42:in `run' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/factory_runner.rb:23:in `block in run' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/activesupport-4.0.1/ lib/active_support/notifications.rb:161:in `instrument' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/factory_runner.rb:22:in `run' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method' /Users/stan/projects/rails4/spec/models/project_spec.rb:21:in `block (4 levels) in <top (required)>' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/rspec-core-2.14.5/ lib/rspec/core/memoized_helpers.rb:199:in `block (2 levels) in let' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/rspec-core-2.14.5/ lib/rspec/core/memoized_helpers.rb:199:in `fetch' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/rspec-core-2.14.5/ lib/rspec/core/memoized_helpers.rb:199:in `block in let' Friday, November 15, 13
How do you find the important stuff? Friday, November 15,
13
Start at the top. Friday, November 15, 13
Skim for lines that refer to your project’s codebase Friday,
November 15, 13
/Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/attribute_assigner.rb:14:in `tap' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/attribute_assigner.rb:14:in `object' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/evaluation.rb:12:in `object' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/
lib/factory_girl/strategy/build.rb:9:in `result' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/factory.rb:42:in `run' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/factory_runner.rb:23:in `block in run' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/activesupport-4.0.1/ lib/active_support/notifications.rb:161:in `instrument' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/factory_runner.rb:22:in `run' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/factory_girl-4.2.0/ lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method' /Users/stan/projects/rails4/spec/models/project_spec.rb:21:in `block (4 levels) in <top (required)>' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/rspec-core-2.14.5/ lib/rspec/core/memoized_helpers.rb:199:in `block (2 levels) in let' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/rspec-core-2.14.5/ lib/rspec/core/memoized_helpers.rb:199:in `fetch' /Users/stan/.rvm/gems/ruby-1.9.3-p448@rails4/gems/rspec-core-2.14.5/ lib/rspec/core/memoized_helpers.rb:199:in `block in let' /Users/stan/projects/rails4/spec/models/project_spec.rb:24:in `block Friday, November 15, 13
Read the line number Friday, November 15, 13
google the error (no, really, you’re not smarter than google)
Friday, November 15, 13
Unexpected Behavior Friday, November 15, 13
“I don’t guess. I observe.” Friday, November 15, 13
“And once I’ve observed...” Friday, November 15, 13
“I deduce.” Friday, November 15, 13
Formulate a hypothesis. Try to disprove it. Repeat as needed.
Friday, November 15, 13
Where do you start looking? Friday, November 15, 13
The Streetlight Effect searching where the keys fell vs searching
where the light is Friday, November 15, 13
Start looking where the light is Friday, November 15, 13
First test the theories you can disprove the fastest Friday,
November 15, 13
To test your hypotheses, you need to reproduce the bug
Friday, November 15, 13
If you can’t reproduce the bug, what’s different about your
environment? Friday, November 15, 13
Watch somebody else reproduce the problem. Friday, November 15, 13
Sometimes it’s all in the timing Friday, November 15, 13
Even partial patterns can give you useful data Friday, November
15, 13
the 500 mile bug http://www.ibiblio.org/harris/ 500milemail.html Friday, November 15, 13
Try to break things down into smaller, self-contained, pieces. Friday,
November 15, 13
Don’t even try to understand everything Friday, November 15, 13
Friday, November 15, 13
Miller’s Law The average person can only hold 7 ±
2 items in working memory Friday, November 15, 13
It’s okay to think of the rest of the system
as a black box Friday, November 15, 13
Break apart methods so you can test smaller chunks of
logic separately Friday, November 15, 13
Comment out parts of your code so you can focus
on one piece at a time Friday, November 15, 13
Refactoring and adding tests (or even just fixing typos) keeps
you focused and improves your understanding Friday, November 15, 13
With really gnarly code write tests to document existing behavior
Friday, November 15, 13
"...what are the facts, and what is the truth that
the facts bear out?" - Bertrand Russell Friday, November 15, 13
Is the method you’re looking at even being called at
all? Friday, November 15, 13
def might_be_called puts “IN MIGHT_BE_CALLED” puts caller # ... end
Friday, November 15, 13
“What they don’t confess to is almost always more interesting.”
Friday, November 15, 13
What are the actual inputs your method is receiving? Friday,
November 15, 13
Maybe you think you’re getting nil, but you’re getting ‘’
or [‘’] instead Friday, November 15, 13
Is there a rescue in the way, swallowing useful errors?
Friday, November 15, 13
What is the state of the universe? Friday, November 15,
13
Follow the money logs $ tail -f log/whatevs.log Friday, November
15, 13
Does doing things in a different order lead to different
results? Friday, November 15, 13
Possible Ordering Problems Methods with side effects Rails autoload order
Database left in a dirty state Friday, November 15, 13
Clean out your environment (if at all possible) Friday, November
15, 13
Make the problem more concrete Friday, November 15, 13
Sketch things out Friday, November 15, 13
Wave your hands around in the air placing pieces of
your system in imaginary 3d space Friday, November 15, 13
“I need to go to my mind palace.” Friday, November
15, 13
Question your assumptions. (Write a list or even an informal
proof, if necessary.) Friday, November 15, 13
Rubber duck debugging Friday, November 15, 13
Trust no one. Friday, November 15, 13
Friday, November 15, 13
“The only truly accurate comment I've seen was just cursing,
in Swedish.” - Scott Vokes (@silentbicycle) Friday, November 15, 13
“Without a debugger, you basically have to go to the
next step; understand what the program does.” - Linus Torvalds Friday, November 15, 13
print statement debugging puts “*” * 20 puts “foo: #{foo.inspect}”
puts “*” * 20 Friday, November 15, 13
Where is this method defined? m = Foo.new.method(:do_stuff) m.source_location =>
[filename, line_number] Friday, November 15, 13
“Ideas are tested by experiment. That is the core of
science.” - Zombie Richard Feynman (via xkcd) Friday, November 15, 13
Experiment with small pieces of the code in IRB or
Pry Friday, November 15, 13
Focus on what’s changed since the problem started Friday, November
15, 13
A few Git tricks Friday, November 15, 13
test on a different branch or a different commit Friday,
November 15, 13
git bisect binary search through your commits to figure out
when the problem started! Friday, November 15, 13
Running git bisect manually $ git bisect start $ git
bisect good SHA $ git bisect bad HEAD Friday, November 15, 13
Running git bisect automatically $ git bisect start HEAD HEAD~10
$ git bisect run rspec spec/ Friday, November 15, 13
git blame (not actually for blaming people!) Friday, November 15,
13
The person who wrote this code may have been an
idiot. Or they may have been a mad genius. Friday, November 15, 13
Look at the original context $ git blame -L(range) filename
$ git show SHA Friday, November 15, 13
But what about whitespace changes? $ git blame -w filename
(ignores whitespace) Friday, November 15, 13
But what about lines that were just copied or moved?
$ git blame -wCCC filename -w ignores whitespace -C detects moved/copied lines -CCC is like -C, but tries harder Friday, November 15, 13
$ git log -p Includes patches, not just commit messages.
You can search with / search_string Friday, November 15, 13
$ git log -p -S “test string” Lists only commits
where that string was added or deleted (with patches). Friday, November 15, 13
Debuggers Friday, November 15, 13
byebug (Ruby 2.0.x) debugger (Ruby 1.9.x) ruby-debug (Ruby 1.8.x) Friday,
November 15, 13
Pry https://github.com/pry/pry https://github.com/pry/pry/ wiki/Available-plugins Friday, November 15, 13
ls - list methods on an object cd - into
objects wtf? - backtrace show-doc - prints docs show-method - prints source whereami - no really Pry Friday, November 15, 13
binding.pry if @whatevs Friday, November 15, 13
Pry-remote https://github.com/mon-ouie/ pry-remote Friday, November 15, 13
binding.pry_remote $ pry-remote Friday, November 15, 13
Better Errors https://github.com/charliesome/ better_errors Friday, November 15, 13
Friday, November 15, 13
WHEN THINGS GET REALLY WEIRD Friday, November 15, 13
This shouldn’t make a difference, but let’s try it just
in case... Friday, November 15, 13
Friday, November 15, 13
It doesn’t count if you’re right by accident Friday, November
15, 13
“That’s clever. Is it clever?” “...why is it clever?” Friday,
November 15, 13
If you don’t understand the cause of the problem, it’s
not fixed, because you can’t predict when it might come up again. Friday, November 15, 13
Don’t be afraid to read the source Friday, November 15,
13
$ bundle open gem_name Friday, November 15, 13
occasionally the compiler (Rarely! But it happens.) Friday, November 15,
13
Friday, November 15, 13
Friday, November 15, 13
“You know my methods, Watson.” Friday, November 15, 13
THE SCIENCE OF DEDUCTION
[email protected]
• @DanielleSucher DEBUGGING thank you!
Friday, November 15, 13