Slide 1

Slide 1 text

and the Antikythera Mechanism @mheap at #PHPCONFPH

Slide 2

Slide 2 text

Antikythera Mechanism

Slide 3

Slide 3 text

It’s Been Stolen!

Slide 4

Slide 4 text

Why do we want it?

Slide 5

Slide 5 text

40-80% of a project is maintenance

Slide 6

Slide 6 text

Why so much?

Slide 7

Slide 7 text

Our job is to read code, Not to write it

Slide 8

Slide 8 text

*flying noises*

Slide 9

Slide 9 text

Where do we start?

Slide 10

Slide 10 text

What should be saved?

Slide 11

Slide 11 text

What is dangerous?

Slide 12

Slide 12 text

Who created this?

Slide 13

Slide 13 text

README.md

Slide 14

Slide 14 text

Talk to people

Slide 15

Slide 15 text

Inventory the code

Slide 16

Slide 16 text

Dependencies

Slide 17

Slide 17 text

Build the project

Slide 18

Slide 18 text

Accept you're missing 10%

Slide 19

Slide 19 text

Time to go in!

Slide 20

Slide 20 text

Security!

Slide 21

Slide 21 text

“Incorrect Password”

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

namespace Architect\Security; class DoorPasswordValidator { private $doorPassword = 'manilarocks'; public function validate($input) { if ($this->doorPassword !== $input) { throw new \Exception("Incorrect password"); } return true; } }

Slide 24

Slide 24 text

We’re in!

Slide 25

Slide 25 text

Navigation

Slide 26

Slide 26 text

Draw a map

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Read the code

Slide 29

Slide 29 text

Don’t trust the comments

Slide 30

Slide 30 text

namespace Architect\Game; class Roulette { /** * Returns a random number */ private function spinRandom(){ } }

Slide 31

Slide 31 text

Don’t trust the code

Slide 32

Slide 32 text

namespace Architect\Game; class Roulette { /** * Returns a random number */ private function spinRandom(){ return 4; } }

Slide 33

Slide 33 text

Does this code even run?

Slide 34

Slide 34 text

/** * Returns a random number */ private function spinRandom() { die('Yes it does'); return 4; }

Slide 35

Slide 35 text

Look for side effects

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Naming Things

Slide 38

Slide 38 text

Find intentions

Slide 39

Slide 39 text

Find Patterns

Slide 40

Slide 40 text

Prove you understand it

Slide 41

Slide 41 text

I don’t understand it

Slide 42

Slide 42 text

Debugging

Slide 43

Slide 43 text

use everything

Slide 44

Slide 44 text

Everything goes

Slide 45

Slide 45 text

DDD

Slide 46

Slide 46 text

Die Driven Debugging

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

if (!isset($visitors['michael_cheng'])){ var_dump($visitors);die; }

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

class MyDoorMan extends DoorMan { public function changeShiftAt($val) { throw new Exception("Changing at $val"); } }

Slide 51

Slide 51 text

XDebug

Slide 52

Slide 52 text

XDebug.scream

Slide 53

Slide 53 text

XDebug_break();

Slide 54

Slide 54 text

if (!isset($visitors['michael_cheng'])){ xdebug_break(); }

Slide 55

Slide 55 text

XHPROF

Slide 56

Slide 56 text

DDD +X Debug + XHProf == WINNING

Slide 57

Slide 57 text

Observer Effect

Slide 58

Slide 58 text

I’m feeling better

Slide 59

Slide 59 text

Project History

Slide 60

Slide 60 text

git log -S

Slide 61

Slide 61 text

git log -S antikythera

Slide 62

Slide 62 text

commit 5288d5804a3fc20dae4f3b2deeaa7f687595aff1 Author: Rasmus Lerdorf 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

Slide 63

Slide 63 text

What’s changed recently?

Slide 64

Slide 64 text

Temporal Coupling

Slide 65

Slide 65 text

Explicit

Slide 66

Slide 66 text

commit 0dc164fb052f4b838a674648aa29d83b60f01fa2 Author: Rasmus Lerdorf 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

Slide 67

Slide 67 text

Implicit

Slide 68

Slide 68 text

commit 2915dabebc8b87b84ec40fac0f4954fb84f3b7cd Author: Rasmus Lerdorf 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

Slide 69

Slide 69 text

Common Causes

Slide 70

Slide 70 text

Code Maat

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

History Analysis is flawed

Slide 77

Slide 77 text

Leaving a legacy

Slide 78

Slide 78 text

Secure the site

Slide 79

Slide 79 text

Glossary

Slide 80

Slide 80 text

Logging

Slide 81

Slide 81 text

Name things well

Slide 82

Slide 82 text

Build a knowledge map

Slide 83

Slide 83 text

Leave useful comments

Slide 84

Slide 84 text

// // 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!