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

Wow Code, Such Read!

Wow Code, Such Read!

Abstract:
An exploration of improving at the skill of reading source code.

This talk was presented at the New Relic afterparty for RailsConf 2016 in Kansas City, MO.
Blog post: http://kwugirl.blogspot.com/2016/05/wow-code-such-read.html

Katherine Wu

May 04, 2016
Tweet

More Decks by Katherine Wu

Other Decks in Technology

Transcript

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

    View full-size slide

  2. @kwugirl
    This document and the information herein (including any information that may be incorporated by reference) is provided for informational purposes only and should not
    be construed as an offer, commitment, promise or obligation on behalf of New Relic, Inc. (“New Relic”) to sell securities or deliver any product, material, code,
    functionality, or other feature. Any information provided hereby is proprietary to New Relic and may not be replicated or disclosed without New Relic’s express written
    permission.

    Such information may contain forward-looking statements within the meaning of federal securities laws. Any statement that is not a historical fact or refers to
    expectations, projections, future plans, objectives, estimates, goals, or other characterizations of future events is a forward-looking statement. These forward-looking
    statements can often be identified as such because the context of the statement will include words such as “believes,” “anticipates,” “expects” or words of similar
    import.

    Actual results may differ materially from those expressed in these forward-looking statements, which speak only as of the date hereof, and are subject to change at any
    time without notice. Existing and prospective investors, customers and other third parties transacting business with New Relic are cautioned not to place undue
    reliance on this forward-looking information. The achievement or success of the matters covered by such forward-looking statements are based on New Relic’s current
    assumptions, expectations, and beliefs and are subject to substantial risks, uncertainties, assumptions, and changes in circumstances that may cause the actual
    results, performance, or achievements to differ materially from those expressed or implied in any forward-looking statement. Further information on factors that could
    affect such forward-looking statements is included in the filings we make with the SEC from time to time. Copies of these documents may be obtained by visiting New
    Relic’s Investor Relations website at ir.newrelic.com or the SEC’s website at www.sec.gov.

    New Relic assumes no obligation and does not intend to update these forward-looking statements, except as required by law. New Relic makes no warranties,
    expressed or implied, in this document or otherwise, with respect to the information provided.

    View full-size slide

  3. @kwugirl
    the goal:
    get better at
    reading code

    View full-size slide

  4. be fearless
    in diving into
    code you don’t know
    Edmond Lau
    theeffectiveengineer.com

    View full-size slide

  5. @kwugirl
    2. remember your question
    3. enlist help
    1. see the code

    View full-size slide

  6. @kwugirl
    the problem

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. @kwugirl
    {
    :sql=>

    "SELECT \"blogs\".* FROM \"blogs\" WHERE \"blogs\".\"id
    \" = $1 LIMIT 1",

    :binds=>
    [[#0x007fd13a879e58 @array=false,
    @cast_type=#QL::OID::Integer:0x007fd13a87afd8 @limit=nil,
    @precision=nil, @range=-2147483648...2147483648,
    @scale=nil>,…,42]]
    }
    ActiveSupport::Notifications.subscribe 'sql.active_record'

    View full-size slide

  11. @kwugirl
    {
    :sql=>

    "SELECT \"blogs\".* FROM \"blogs\" WHERE \"blogs\".\"id
    \" = $1 LIMIT 1",

    :binds=>
    [[#0x007fd13a879e58 @array=false,
    @cast_type=#QL::OID::Integer:0x007fd13a87afd8 @limit=nil,
    @precision=nil, @range=-2147483648...2147483648,
    @scale=nil>,…,42]]
    }
    ActiveSupport::Notifications.subscribe 'sql.active_record'

    View full-size slide

  12. @kwugirl
    2. remember your question
    3. enlist help
    1. see the code

    View full-size slide

  13. @kwugirl
    $ cd src/

    View full-size slide

  14. @kwugirl
    $ cd src/
    $ git clone [email protected]:rails/rails.git

    $ cd rails/

    View full-size slide

  15. @kwugirl
    $ cd src/
    $ git clone [email protected]:rails/rails.git

    $ cd rails/
    $ git checkout v4.2.0

    View full-size slide

  16. @kwugirl
    # Gemfile
    gem 'rails', '4.2.0'

    View full-size slide

  17. @kwugirl
    # Gemfile
    # gem 'rails', '4.2.0'
    gem 'rails', :path => '../../src/rails'

    View full-size slide

  18. @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

    View full-size slide

  19. @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

    View full-size slide

  20. @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

    View full-size slide

  21. @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

    View full-size slide

  22. @kwugirl
    git blame (can be)
    your friend

    View full-size slide

  23. @kwugirl
    2. remember your question
    3. enlist help
    1. see the code

    View full-size slide

  24. @kwugirl
    don’t get lost

    View full-size slide

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

    View full-size slide

  26. @kwugirl
    don’t get lost
    learn one thing at a time
    what are your unknowns?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. @kwugirl
    2. remember your question
    3. enlist help
    1. see the code

    View full-size slide

  31. @kwugirl
    pair programming

    View full-size slide

  32. @kwugirl
    pair programming
    ??????????
    ??????????
    ??????????

    View full-size slide

  33. @kwugirl
    What are you looking for?

    View full-size slide

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

    View full-size slide

  35. @kwugirl
    What are you looking for?
    Why?
    How did you know to look there?

    View full-size slide

  36. @kwugirl
    Kernel#caller

    View full-size slide

  37. @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

    View full-size slide

  38. @kwugirl
    Method#source_location

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. @kwugirl
    Method#source_location
    $ bundle exec rails console
    irb(main):001:0> method(:try).source_location
    => ["/…/active_support/core_ext/object/try.rb", 62]

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  45. @kwugirl
    success!!1!one!!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  48. @kwugirl
    success!!1!one!!
    released last week
    in 3.15.2
    $1
    }Explain plan

    View full-size slide

  49. @kwugirl
    2. remember your question
    3. enlist help
    1. see the code

    View full-size slide

  50. @kwugirl
    resources

    View full-size slide

  51. @kwugirl
    resources
    readingcodegood.com

    View full-size slide

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

    View full-size slide

  53. @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

    View full-size slide

  54. @kwugirl
    appendix

    View full-size slide

  55. @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

    View full-size slide