Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Debugging: The Science of Deduction

Debugging: The Science of Deduction

Tips for debugging code in Ruby.

Danielle Sucher

November 14, 2013
Tweet

More Decks by Danielle Sucher

Other Decks in Programming

Transcript

  1. "People write programs without any expectation that they will be

    right the first time." - David Lorge Parnas Friday, November 15, 13
  2. /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
  3. /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
  4. The Streetlight Effect searching where the keys fell vs searching

    where the light is Friday, November 15, 13
  5. Miller’s Law The average person can only hold 7 ±

    2 items in working memory Friday, November 15, 13
  6. It’s okay to think of the rest of the system

    as a black box Friday, November 15, 13
  7. Break apart methods so you can test smaller chunks of

    logic separately Friday, November 15, 13
  8. Comment out parts of your code so you can focus

    on one piece at a time Friday, November 15, 13
  9. Refactoring and adding tests (or even just fixing typos) keeps

    you focused and improves your understanding Friday, November 15, 13
  10. "...what are the facts, and what is the truth that

    the facts bear out?" - Bertrand Russell Friday, November 15, 13
  11. Maybe you think you’re getting nil, but you’re getting ‘’

    or [‘’] instead Friday, November 15, 13
  12. Possible Ordering Problems Methods with side effects Rails autoload order

    Database left in a dirty state Friday, November 15, 13
  13. Wave your hands around in the air placing pieces of

    your system in imaginary 3d space Friday, November 15, 13
  14. Question your assumptions. (Write a list or even an informal

    proof, if necessary.) Friday, November 15, 13
  15. “The only truly accurate comment I've seen was just cursing,

    in Swedish.” - Scott Vokes (@silentbicycle) Friday, November 15, 13
  16. “Without a debugger, you basically have to go to the

    next step; understand what the program does.” - Linus Torvalds Friday, November 15, 13
  17. “Ideas are tested by experiment. That is the core of

    science.” - Zombie Richard Feynman (via xkcd) Friday, November 15, 13
  18. git bisect binary search through your commits to figure out

    when the problem started! Friday, November 15, 13
  19. Running git bisect manually $ git bisect start $ git

    bisect good SHA $ git bisect bad HEAD Friday, November 15, 13
  20. Running git bisect automatically $ git bisect start HEAD HEAD~10

    $ git bisect run rspec spec/ Friday, November 15, 13
  21. The person who wrote this code may have been an

    idiot. Or they may have been a mad genius. Friday, November 15, 13
  22. Look at the original context $ git blame -L(range) filename

    $ git show SHA Friday, November 15, 13
  23. But what about whitespace changes? $ git blame -w filename

    (ignores whitespace) Friday, November 15, 13
  24. 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
  25. $ git log -p Includes patches, not just commit messages.

    You can search with / search_string Friday, November 15, 13
  26. $ git log -p -S “test string” Lists only commits

    where that string was added or deleted (with patches). Friday, November 15, 13
  27. 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
  28. 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