Wow Code, Such Read! (NR Ruby UnConf)

Wow Code, Such Read! (NR Ruby UnConf)

Abstract:
When we learn to code, that usually means learning how to *write* code. However in practice, we spend a lot of time *reading* code instead! It’s the way we find answers to questions about how things work. Reading code efficiently is therefore a very valuable skill. This talk will cover how Ruby developers can improve this skill, with tips for comprehending and debugging code faster. We'll also take a look at how you’d go one level deeper and read Ruby’s own source code, even if you don’t know much about C!

This talk was presented at an internal Ruby unconf at New Relic.
Blog post: http://kwugirl.blogspot.com/2016/05/wow-code-such-read.html

40be222374e709cae7543dee233fe2e1?s=128

Katherine Wu

February 23, 2017
Tweet

Transcript

  1. @kwugirl Wow Code, Such Read! KWu Ruby Agent Engineer amaze

    understand so power
  2. @kwugirl the goal: get better at reading code

  3. be fearless in diving into code you don’t know Edmond

    Lau theeffectiveengineer.com
  4. @kwugirl

  5. @kwugirl 10,000 hours

  6. @kwugirl 10,000 hours 250 weeks

  7. @kwugirl 10,000 hours 250 weeks ~5 years!

  8. @kwugirl productive struggling

  9. @kwugirl 2. remember your question 3. enlist help 1. see

    the code
  10. @kwugirl the problem

  11. @kwugirl the problem connection.execute("EXPLAIN #{query}")

  12. @kwugirl the problem connection.execute("EXPLAIN #{query}") SELECT "blogs".* FROM "blogs" WHERE

    "blogs"."id" = $1 LIMIT 1 with
  13. @kwugirl the problem connection.execute("EXPLAIN #{query}") SELECT "blogs".* FROM "blogs" WHERE

    "blogs"."id" = $1 LIMIT 1 with
  14. @kwugirl { :sql=>
 "SELECT \"blogs\".* FROM \"blogs\" WHERE \"blogs\". \"id\"

    = $1 LIMIT 1", … :binds=> [[#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn: 0x007fd13a879e58 @array=false, @cast_type=#<ActiveRecord::ConnectionAdapters::PostgreS QL::OID::Integer:0x007fd13a87afd8 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,…,42]] } ActiveSupport::Notifications.subscribe 'sql.active_record'
  15. @kwugirl { :sql=>
 "SELECT \"blogs\".* FROM \"blogs\" WHERE \"blogs\". \"id\"

    = $1 LIMIT 1", … :binds=> [[#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn: 0x007fd13a879e58 @array=false, @cast_type=#<ActiveRecord::ConnectionAdapters::PostgreS QL::OID::Integer:0x007fd13a87afd8 @limit=nil, @precision=nil, @range=-2147483648...2147483648, @scale=nil>,…,42]] } ActiveSupport::Notifications.subscribe 'sql.active_record'
  16. @kwugirl 2. remember your question 3. enlist help 1. see

    the code
  17. @kwugirl

  18. @kwugirl $ cd src/

  19. @kwugirl $ cd src/ $ git clone git@github.com:rails/rails.git
 $ cd

    rails/
  20. @kwugirl $ cd src/ $ git clone git@github.com:rails/rails.git
 $ cd

    rails/ $ git checkout v4.2.0
  21. @kwugirl # Gemfile gem 'rails', '4.2.0'

  22. @kwugirl # Gemfile # gem 'rails', '4.2.0' gem 'rails', :path

    => '../../src/rails'
  23. @kwugirl @instrumenter.instrument( "sql.active_record", :sql => sql, :name => name, :connection_id

    => object_id, :statement_name => statement_name, :binds => binds) { yield } def log(sql, name = "SQL", binds = [], statement_name = nil) # active_record/connection_adapters/abstract_adapter.rb
  24. @kwugirl @instrumenter.instrument( "sql.active_record", :sql => sql, :name => name, :connection_id

    => object_id, :statement_name => statement_name, :binds => binds) { yield } def log(sql, name = "SQL", binds = [], statement_name = nil) # active_record/connection_adapters/abstract_adapter.rb
  25. @kwugirl @instrumenter.instrument( "sql.active_record", :sql => sql, :name => name, :connection_id

    => object_id, :statement_name => statement_name, :binds => binds) { yield } def log(sql, name = "SQL", binds = [], statement_name = nil) # active_record/connection_adapters/abstract_adapter.rb
  26. @kwugirl def log(sql, name = "SQL", binds = [], statement_name

    = nil) @instrumenter.instrument( "sql.active_record", :sql => sql, :name => name, :connection_id => object_id, :statement_name => statement_name, :binds => binds) { yield } puts "====== HELLO IS IT ME YOU'RE LOOKING FOR?!!?? ======" # active_record/connection_adapters/abstract_adapter.rb
  27. @kwugirl git blame (can be) your friend

  28. @kwugirl 2. remember your question 3. enlist help 1. see

    the code
  29. @kwugirl don’t get lost

  30. @kwugirl don’t get lost learn one thing at a time

  31. @kwugirl don’t get lost learn one thing at a time

    what are your unknowns?
  32. @kwugirl the problem connection.execute("EXPLAIN #{query}") SELECT "blogs".* FROM "blogs" WHERE

    "blogs"."id" = $1 LIMIT 1 with
  33. @kwugirl the problem connection.execute("EXPLAIN #{query}") SELECT "blogs".* FROM "blogs" WHERE

    "blogs"."id" = $1 LIMIT 1 with
  34. @kwugirl the problem connection.execute("EXPLAIN #{query}") SELECT "blogs".* FROM "blogs" WHERE

    "blogs"."id" = $1 LIMIT 1 with
  35. @kwugirl 2. remember your question 3. enlist help 1. see

    the code
  36. @kwugirl pair programming

  37. @kwugirl pair programming ?????????? ?????????? ??????????

  38. @kwugirl

  39. @kwugirl What are you looking for?

  40. @kwugirl What are you looking for? Why?

  41. @kwugirl What are you looking for? Why? How did you

    know to look there?
  42. @kwugirl Kernel#caller

  43. @kwugirl active_record/connection_adapters/postgresql_adapter.rb:601:in `exec_cache' active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear' active_record/connection_adapters/postgresql/database_statements.rb:161:in `exec_query' active_record/connection_adapters/abstract/database_statements.rb:341:in `select' active_record/connection_adapters/abstract/database_statements.rb:37:in

    `select_all' active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all' active_record/querying.rb:39:in `find_by_sql' active_record/relation.rb:639:in `exec_queries' active_record/relation.rb:514:in `load' active_record/relation.rb:243:in `to_a' active_record/relation.rb:617:in `pretty_print' Kernel#caller
  44. @kwugirl Method#source_location

  45. @kwugirl Method#source_location $ bundle exec rails console

  46. @kwugirl Method#source_location $ bundle exec rails console irb(main):001:0> method(:try).source_location

  47. @kwugirl Method#source_location $ bundle exec rails console irb(main):001:0> method(:try).source_location =>

    ["/…/active_support/core_ext/object/try.rb", 62]
  48. @kwugirl method_defined? RubyTapas #439 “Method Search”

  49. @kwugirl method_defined? $ ObjectSpace.each_object(Class).select{|c| c.singleton_class.method_defined?(:try_convert) } => [File, IO, Regexp,

    Hash, Array, String] RubyTapas #439 “Method Search”
  50. @kwugirl method_defined? $ ObjectSpace.each_object(Class).select{|c| c.singleton_class.method_defined?(:try_convert) } => [File, IO, Regexp,

    Hash, Array, String] RubyTapas #439 “Method Search”
  51. @kwugirl method_defined? $ ObjectSpace.each_object(Class).select{|c| c.singleton_class.method_defined?(:try_convert) } => [File, IO, Regexp,

    Hash, Array, String] RubyTapas #439 “Method Search”
  52. @kwugirl method_defined? $ ObjectSpace.each_object(Class).select{|c| c.singleton_class.method_defined?(:try_convert) } => [File, IO, Regexp,

    Hash, Array, String] RubyTapas #439 “Method Search”
  53. @kwugirl method_defined? $ ObjectSpace.each_object(Class).select{|c| c.singleton_class.method_defined?(:try_convert) } => [File, IO, Regexp,

    Hash, Array, String] RubyTapas #439 “Method Search”
  54. @kwugirl 2. remember your question 3. enlist help 1. see

    the code
  55. @kwugirl the solution connection.exec_query("EXPLAIN #{statement.sql}", "Explain #{statement.name}", statement.binds) wow beauty

    wonder
  56. @kwugirl the solution connection.exec_query("EXPLAIN #{statement.sql}", "Explain #{statement.name}", statement.binds) wow beauty

    wonder
  57. @kwugirl the solution connection.exec_query("EXPLAIN #{statement.sql}", "Explain #{statement.name}", statement.binds) wow beauty

    wonder
  58. @kwugirl success!!1!one!!

  59. @kwugirl success!!1!one!! $1

  60. @kwugirl success!!1!one!! $1 }Explain plan

  61. @kwugirl success!!1!one!! released in 3.15.2 $1 }Explain plan

  62. @kwugirl reading ruby source

  63. @kwugirl gentle intro

  64. @kwugirl gentle intro

  65. @kwugirl which files?

  66. @kwugirl which files?

  67. @kwugirl github.com/ruby/ruby

  68. @kwugirl github.com/ruby/ruby “trunk” not “master”

  69. @kwugirl pretty flat directory

  70. @kwugirl pretty flat directory

  71. @kwugirl std library in lib/

  72. @kwugirl extension.rdoc

  73. @kwugirl parse.y

  74. @kwugirl other helpful points

  75. @kwugirl other helpful points rb_

  76. @kwugirl other helpful points rb_ regex search with ^ to

    find method definition
  77. @kwugirl other helpful points rb_ regex search with ^ to

    find method definition comments
  78. @kwugirl bugs.ruby-lang.org

  79. @kwugirl resources

  80. @kwugirl resources readingcodegood.com

  81. @kwugirl resources readingcodegood.com schneems' talk “Dissecting Ruby with Ruby”

  82. @kwugirl resources readingcodegood.com schneems' talk “Dissecting Ruby with Ruby” schneems.com/2016/01/25/ruby-debugging-magic-

    cheat-sheet.html
 tenderlovemaking.com/2016/02/05/i-am-a-puts- debuggerer.html
  83. @kwugirl resources

  84. @kwugirl resources Jason Clark’s talk “GDB: A Gentle Intro”

  85. @kwugirl resources Jason Clark’s talk “GDB: A Gentle Intro” Hsing-Hui

    Hsu’s talk “Time flies like an arrow, fruit flies like a banana”
  86. @kwugirl resources Jason Clark’s talk “GDB: A Gentle Intro” Hsing-Hui

    Hsu’s talk “Time flies like an arrow, fruit flies like a banana” Ruby Under A Microscope
  87. @kwugirl resources Jason Clark’s talk “GDB: A Gentle Intro” Hsing-Hui

    Hsu’s talk “Time flies like an arrow, fruit flies like a banana” Ruby Under A Microscope Definitive Guide to Ruby’s C API: 
 https://silverhammermba.github.io/emberb/c/
  88. @kwugirl Julia Evans jvns.ca

  89. @kwugirl Net::HTTP is maybe 2000 lines of code, not including

    comments. Julia Evans jvns.ca
  90. @kwugirl Net::HTTP is maybe 2000 lines of code, not including

    comments. It has ~1000 lines of comments… Julia Evans jvns.ca
  91. @kwugirl Net::HTTP is maybe 2000 lines of code, not including

    comments. It has ~1000 lines of comments… I can read 2000 lines of code, Julia Evans jvns.ca
  92. @kwugirl Net::HTTP is maybe 2000 lines of code, not including

    comments. It has ~1000 lines of comments… I can read 2000 lines of code, mostly! Julia Evans jvns.ca
  93. @kwugirl doge The White Rabbit, Alice in Wonderland sad Keanu

    pear/pair programming Lionel Richie + Where’s Waldo !!1!one!! the “!1” phenomenon breadfish Slurms MacKenzie, Futurama