Slide 1

Slide 1 text

and the Antikythera Mechanism @mheap at #phpbnl17

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

http://abstrusegoose.com/432

Slide 6

Slide 6 text

40-80% of a project is maintenance

Slide 7

Slide 7 text

Maintenance is good!

Slide 8

Slide 8 text

Our job is to read code, Not to write it

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Where do we start?

Slide 12

Slide 12 text

README.md

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Talk to people

Slide 17

Slide 17 text

Inventory the code

Slide 18

Slide 18 text

Dependencies

Slide 19

Slide 19 text

Build the project

Slide 20

Slide 20 text

Accept you're missing 10%

Slide 21

Slide 21 text

What is dangerous?

Slide 22

Slide 22 text

What should be saved?

Slide 23

Slide 23 text

Who created this?

Slide 24

Slide 24 text

Reconnaissance Time

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

I'm sorry, Dave. I'm afraid I can't do that

Slide 27

Slide 27 text

$ grep -lr "sorry, Dave" * src/HAL/Security/DoorPasswordValidator.php

Slide 28

Slide 28 text

namespace HAL\Security; use HAL\Security\Validator; class DoorPasswordValidator extends Validator { private $doorPassword = 'beneluxisawesome'; public function validate($input) { if ($this->doorPassword !== $input) { throw new \Exception("I'm sorry, Dave. I'm afraid I can't do that"); } return true; } }

Slide 29

Slide 29 text

We’re in!

Slide 30

Slide 30 text

Navigation

Slide 31

Slide 31 text

Read the code

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Don’t trust the comments

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Don’t trust the code

Slide 37

Slide 37 text

Look for side effects

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Double Check Your Understanding

Slide 40

Slide 40 text

Names are important

Slide 41

Slide 41 text

Find Patterns

Slide 42

Slide 42 text

Prove you understand it

Slide 43

Slide 43 text

I don’t understand it

Slide 44

Slide 44 text

Debugging

Slide 45

Slide 45 text

use everything

Slide 46

Slide 46 text

Everything goes

Slide 47

Slide 47 text

DDD

Slide 48

Slide 48 text

Die Driven Debugging

Slide 49

Slide 49 text

Does this code even run?

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

echo '######################'.PHP_EOL; echo count($vistorsInLastDay); echo '######################'.PHP_EOL;

Slide 52

Slide 52 text

if (count($visitorsInLastDay) > 5){ var_dump(visitorsInLastDay);die; }

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

class MyVerifier extends SomethingVerifier { public function userIsAdmin($user) { return true; } }

Slide 55

Slide 55 text

XDebug

Slide 56

Slide 56 text

XDebug.scream

Slide 57

Slide 57 text

XDebug_break();

Slide 58

Slide 58 text

if (count($visitorsInLastDay) > 5){ xdebug_break(); }

Slide 59

Slide 59 text

XHPROF

Slide 60

Slide 60 text

DDD +XDebug + XHProf == WINNING

Slide 61

Slide 61 text

Observer Effect

Slide 62

Slide 62 text

I’m feeling better

Slide 63

Slide 63 text

But I’m still noT 100%

Slide 64

Slide 64 text

Project History

Slide 65

Slide 65 text

git log -S

Slide 66

Slide 66 text

git log -S antikythera

Slide 67

Slide 67 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 68

Slide 68 text

What’s changed recently?

Slide 69

Slide 69 text

What changes together?

Slide 70

Slide 70 text

Explicit

Slide 71

Slide 71 text

commit 0dc164fb052f4b838a674648aa29d83b60f01fa2 Author: Rasmus Lerdorf Date: Fri May 20 12:14:55 2016 +0000 Added HAL to the villa’s security system on both the front and back doors 5 2 src/Villa/Structure/Door.php 14 0 test/Villa/Structure/DoorTest.php

Slide 72

Slide 72 text

Implicit

Slide 73

Slide 73 text

commit 2915dabebc8b87b84ec40fac0f4954fb84f3b7cd Author: Rasmus Lerdorf Date: Sun May 22 21:27:31 2016 +0000 Bought a new car. Updated town parking permit to new registration 12 0 src/Garage/Car/Ferrari.php 1 1 src/Town/ParkingPermits/Rasmus.php

Slide 74

Slide 74 text

Common Causes

Slide 75

Slide 75 text

Code Maat

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

entity,n-authors,n-revs src/Room/Library.php,26,181 src/Casino/Game/Roulette.php,22,76 src/Controller/HallwayController.php,21,110 src/People/Cleaner.php,19,91

Slide 78

Slide 78 text

entity,age-months src/Security/PasswordManager.php,1 src/People/Cleaner.php,1 src/Casino/Game/Roulette.php,3 src/Security/SafeCombination.php,7

Slide 79

Slide 79 text

entity,coupled,degree,average-revs src/People/Cleaner.php,src/HAL/Security/DoorPasswordValidator.php,100,19 src/Room/Lounge/Wallpaper.php,src/Room/Bedroom/Wallpaper.php,60,10

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

Visualisations Help

Slide 82

Slide 82 text

symfony/symfony

Slide 83

Slide 83 text

rails/rails

Slide 84

Slide 84 text

symfony/symfony

Slide 85

Slide 85 text

rails/rails

Slide 86

Slide 86 text

Code Ownership

Slide 87

Slide 87 text

Author Coupling

Slide 88

Slide 88 text

Code Ownership

Slide 89

Slide 89 text

Code Ownership

Slide 90

Slide 90 text

Commit Msg

Slide 91

Slide 91 text

Commit Msg

Slide 92

Slide 92 text

Commit times

Slide 93

Slide 93 text

Commit History

Slide 94

Slide 94 text

github/adamtornhill/code-maat Codescene.io codehistoryminer.com github/smontanari/code-forensics

Slide 95

Slide 95 text

History Analysis is flawed

Slide 96

Slide 96 text

Leaving a legacy

Slide 97

Slide 97 text

Secure the site

Slide 98

Slide 98 text

Glossary

Slide 99

Slide 99 text

Logging

Slide 100

Slide 100 text

Name things well

Slide 101

Slide 101 text

Build a knowledge map

Slide 102

Slide 102 text

Leave useful comments

Slide 103

Slide 103 text

Attribution https://upload.wikimedia.org/wikipedia/commons/f/f5/ Second_world_war_europe_1943-1945_map_en.png CC-BY-SA https://archive.org/details/PET717_Var3 CC-BY https://www.flickr.com/photos/132130100@N02/27448816326/ CC-BY-ND https://www.flickr.com/photos/rockandrollfreak/10154906473 CC-BY-ND https://www.flickr.com/photos/johnwardell/80125882 BY-NC-ND

Slide 104

Slide 104 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! https://joind.in/talk/b0184