Code Archaeology: Becoming Indiana Jones

Code Archaeology: Becoming Indiana Jones

Code archaeology is one of the most important skills you will ever learn as a developer. How many times have you been thrown into legacy projects with no environment setup, no documentation, and little support from coworkers? The ability to hit the ground running on new applications is a sought-after skill for many employers. In this talk, we will take an open-source project that we have never seen before and work through the steps required to get it up and running so we can start contributing today.

Bbf9decfbfc2ab5b450ec503749ded28?s=128

Michael Heap

May 27, 2016
Tweet

Transcript

  1. and the Evil Architects @mheap at #tek16

  2. Antikythera Mechanism

  3. It’s Been Stolen!

  4. Why do we want it?

  5. 40-80% of a project is maintenance

  6. Why so much?

  7. Our job is to read code, Not to write it

  8. *flying noises*

  9. Where do we start?

  10. What should be saved?

  11. What is dangerous?

  12. Who created this?

  13. README.md

  14. Talk to people

  15. Inventory the code

  16. Dependencies

  17. Build the project

  18. Accept you're missing 10%

  19. Time to go in!

  20. Security!

  21. “Incorrect Password”

  22. $ grep -lr "Incorrect password" * src/Architect/Security/DoorPasswordValidator.php

  23. namespace Architect\Security; class DoorPasswordValidator { private $doorPassword = 'shibboleet'; public

    function validate($input) { if ($this->doorPassword !== $input) { throw new \Exception("Incorrect password"); } return true; } }
  24. We’re in!

  25. Navigation

  26. Draw a map

  27. None
  28. Read the code

  29. Don’t trust the comments

  30. namespace Architect\Game; class Roulette { /** * Returns a random

    number */ }
  31. Don’t trust the code

  32. namespace Architect\Game; class Roulette { /** * Returns a random

    number */ private function spinRandom(){ return 4; } }
  33. Does this code even run?

  34. /** * Returns a random number */ private function spinRandom()

    { die('Yes it does'); return 4; }
  35. Look for side effects

  36. public function triggerFireAlarm() { $this->startSprinklers(); $this->callFireBrigade(); $this->releaseDoorLocks(); }

  37. Naming Things

  38. Find intentions

  39. Find Patterns

  40. Prove you understand it

  41. I don’t understand it

  42. Debugging

  43. use everything

  44. Everything goes

  45. DDD

  46. Die Driven Debugging

  47. echo '######################'.PHP_EOL; echo $currentCasinoCapacity; echo '######################'.PHP_EOL;

  48. if (!isset($visitors['derick_rethans'])){ var_dump($visitors);die; }

  49. echo '######################'.PHP_EOL; var_dump(debug_backtrace()); echo '######################'.PHP_EOL;

  50. class MyDoorMan extends DoorMan { public function changeShiftAt($val) { throw

    new Exception("Changing at $val"); } }
  51. XDebug

  52. XDebug.scream

  53. XDebug_break();

  54. if (!isset($visitors['derick_rethans'])){ xdebug_break(); }

  55. xdebug_debug_zval();

  56. $michael = new Person(‘Michael’); $bouncer =& $michael; $dealer =& $michael->hands;

    xdebug_debug_zval( 'michael' ); // Outputs michael: (refcount=2, is_ref=1)=array ( 0 => (refcount=1, is_ref=0)=1, 1 => (refcount=1, is_ref=0)=2, 2 => (refcount=2, is_ref=1)=3)
  57. XHPROF

  58. DDD +X Debug + XHProf == WINNING

  59. Observer Effect

  60. I’m feeling better

  61. Project History

  62. git log -S <search>

  63. git log -S antikythera

  64. commit 5288d5804a3fc20dae4f3b2deeaa7f687595aff1 Author: Eli White <eli@example.com> Date: Tue May 24

    09:33:59 2016 +0000 Add new secure storage facility. (Closes #42) The existing storage facilities use symmetrical encryption keys which allow anyone with the password to access them. The new storage is more secure and requires both a password and my handprint. There’s nothing in there yet but we’ll put the Antikythera in it as soon as it arrives
  65. What’s changed recently?

  66. Temporal Coupling

  67. Explicit

  68. commit 0dc164fb052f4b838a674648aa29d83b60f01fa2 Author: Oscar Merida <oscar@example.com> Date: Fri May 20

    12:14:55 2016 +0000 Changed from European to American roulette table. Gives the house a better edge 5 2 src/Architect/Game/Roulette.php 14 0 test/Architect/Game/RouletteTest.php
  69. Implicit

  70. commit 2915dabebc8b87b84ec40fac0f4954fb84f3b7cd Author: Eli White <eli@example.com> Date: Sun May 22

    21:27:31 2016 +0000 Removed the corner roulette table as it was crooked. Updated Fire plan to remove table 0 2 src/Architect/FloorPlan.php 0 1 src/Architect/Policy/FireEscape.php
  71. Common Causes

  72. Code Maat

  73. statistic,value number-of-commits,2254 number-of-entities,3179 number-of-entities-changed,9979 number-of-authors,106

  74. entity,n-authors,n-revs src/Room/VipLounge.php,26,181 src/Game/Blackjack.php,22,76 src/Controller/CloakroomController.php,21,110 src/People/FloorManager.php,19,91

  75. entity,age-months src/Security/PasswordManager.php,1 src/Bar/DrinkPrices.php,1 src/Game/Poker/Stud.php,3 src/Security/RadioChannels.php,7

  76. entity,coupled,degree,average-revs src/Manager/FloorManager.php,src/Factory/TableFactory.php,100,19 src/Vendor/Drinks.php,src/Vendor/Snacks.php,60,10 src/Room/VipLounge.php,src/Room/Cloakroom,14,172

  77. entity,coupled,degree,average-revs application/config/config.php,application/config/database.php,90,6

  78. History Analysis is flawed

  79. Leaving a legacy

  80. Secure the site

  81. Rosetta Stone

  82. Logging

  83. Name things well

  84. Build a knowledge map

  85. Leave useful comments

  86. // // Dear maintainer: // // Once you are done

    trying to 'optimize' this routine, // and have realized what a terrible mistake that was, // please increment the following counter as a warning // to the next guy: // // total_hours_wasted_here = 42 // I’ve been @mheap, you’ve been awesome! https://joind.in/17041