Slide 1

Slide 1 text

THE • DEBUGGING • CHECKLIST by Danny Kopping PHPSouthAfrica Cape Town October 2013 http://lanyrd.com/scpdkh 1 Sunday 06 October 13

Slide 2

Slide 2 text

HI 2 Sunday 06 October 13

Slide 3

Slide 3 text

Danny Kopping Senior PHP Developer @ Flow Communications Culture-less (from Joburg) 24 years old PHP for 25% of my life 3 Sunday 06 October 13

Slide 4

Slide 4 text

DEBUGGING = PROBLEM SOLVING 4 Sunday 06 October 13

Slide 5

Slide 5 text

HOW DO WE SOLVE PROBLEMS? 5 Sunday 06 October 13

Slide 6

Slide 6 text

Clip courtesy of FOX 6 Sunday 06 October 13

Slide 7

Slide 7 text

FOCUS 7 Sunday 06 October 13

Slide 8

Slide 8 text

THE FIRST STEP IN SOLVING ANY PROBLEM IS ISOL ATING ITS CAUSE(S) 8 Sunday 06 October 13

Slide 9

Slide 9 text

9 Sunday 06 October 13

Slide 10

Slide 10 text

THE KEY TO PROBLEM ISOLATION IS REDUCTION 10 Sunday 06 October 13

Slide 11

Slide 11 text

11 Sunday 06 October 13

Slide 12

Slide 12 text

‣ REDUCE SOLUTION SCOPE 11 Sunday 06 October 13

Slide 13

Slide 13 text

‣ REDUCE SOLUTION SCOPE ‣ REDUCE ITERATION TIME 11 Sunday 06 October 13

Slide 14

Slide 14 text

‣ REDUCE SOLUTION SCOPE ‣ REDUCE ITERATION TIME ‣ REDUCE COMPLEXITY 11 Sunday 06 October 13

Slide 15

Slide 15 text

‣ REDUCE SOLUTION SCOPE ‣ REDUCE ITERATION TIME ‣ REDUCE COMPLEXITY ‣ REDUCE FRUSTRATION LEVELS 11 Sunday 06 October 13

Slide 16

Slide 16 text

5) { if (strlen($_POST['user_name']) < 65 && strlen($_POST['user_name']) > 1) { if (preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name'])) { $user = read_user($_POST['user_name']); if (!isset($user['user_name'])) { if ($_POST['user_email']) { if (strlen($_POST['user_email']) < 65) { if (filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) { create_user(); $_SESSION['msg'] = 'You are now registered so please login'; header('Location: ' . $_SERVER['PHP_SELF']); exit(); } else $msg = 'You must provide a valid email address'; } else $msg = 'Email must be less than 64 characters'; } else $msg = 'Email cannot be empty'; } else $msg = 'Username already exists'; } else $msg = 'Username must be only a-z, A-Z, 0-9'; } else $msg = 'Username must be between 2 and 64 characters'; } else $msg = 'Password must be at least 6 characters'; } else $msg = 'Passwords do not match'; } else $msg = 'Empty Password'; } else $msg = 'Empty Username'; $_SESSION['msg'] = $msg; } return register_form(); } http://codecrap.com/content/986/ 12 Sunday 06 October 13

Slide 17

Slide 17 text

WHY DOES THIS SUCK? • BAD CODE STRUCTURE • LOCALISATION WOULD MAKE IT EVEN MORE COMPLEX • HARD TO TRACK DOWN CAUSES OF ISSUES • DIFFICULT TO READ • RELIANCE ON $_POST • NO ERROR CONTEXT - ONLY STRING MESSAGES 13 Sunday 06 October 13

Slide 18

Slide 18 text

‣ REDUCE SOLUTION SCOPE

Slide 19

Slide 19 text

‣ REDUCE ITERATION TIME 'infomaniac', 'user_email' => '[email protected]', 'user_password_new' => '1234', 'user_password_repeat' => '1234', ); register($data); function register($data) { if (!empty($data)) { $msg = ''; if ($data['user_name']) { if ($data['user_password_new']) { if ($data['user_password_new'] === $data['user_password_repeat']) { if (strlen($data['user_password_new']) > 5) { if (strlen($data['user_name']) < 65 && strlen($data['user_n if (preg_match('/^[a-z\d]{2,64}$/i', $data['user_name'] $user = read_user($data['user_name']); if (!isset($user['user_name'])) { if ($data['user_email']) { if (strlen($data['user_email']) < 65) { 15 Sunday 06 October 13

Slide 20

Slide 20 text

‣ REDUCE ITERATION TIME assertEquals($expected, (bool) preg_match(self::USERNAME_PATTERN, $username)); } public function usernamesProvider() { return array( array(true, 'phpsouthafrica'), array(false, 'ilikecheese!'), array(false, 'something-with-an-ümlaut'), array(false, 'somesillyusernamethatisincrediblylongandwillfailvalidationimmediately'), array(false, ''), array(true, '123456'), ); } } 16 Sunday 06 October 13

Slide 21

Slide 21 text

‣ REDUCE COMPLEXITY 'infomaniac', 'user_email' => '[email protected]', 'user_password_new' => '123456', 'user_password_repeat' => '123456', ); $v = new Validator($data); $v->addRule('username', function ($field, $value, array $params) { // 2 to 64 alphanumeric characters of any case $pattern = '/^[a-z\d]{2,64}$/i'; return preg_match($pattern, $value); }, 'is invalid because it does not contain 2 to 64 alphanumeric characters'); $v->rule('required', ['user_name', 'user_password_new']); $v->rule('username', ['user_name'])->label('Username'); $v->rule('email', ['user_email']); $v->rule('equals', ['user_password_new'], 'user_password_repeat'); $v->rule('length', ['user_password_new'], 6); 17 Sunday 06 October 13

Slide 22

Slide 22 text

‣ REDUCE FRUSTRATION LEVELS • GET AWAY FROM YOUR COMPUTER! • TRY A DIFFERENT APPROACH • BITCH TO A COLLEAGUE IF ALL ELSE FAILS: • START SMOKING * * NOT KIDDING 18 Sunday 06 October 13

Slide 23

Slide 23 text

REDUCE TO RESOLVE 19 Sunday 06 October 13

Slide 24

Slide 24 text

20 Sunday 06 October 13

Slide 25

Slide 25 text

“If you hear hoofbeats, don’t assume it’s a unicorn” * * or a one-horned oryx 21 Sunday 06 October 13

Slide 26

Slide 26 text

“THE BLAMELIST” 22 Sunday 06 October 13

Slide 27

Slide 27 text

1. Blame yourself. ◦ In debugging, having an ego will only cause you to work longer and harder to solve a bug. Be humble; accept your humanity. Make 100% sure that the way you're attacking a problem is actually isolating the problem. ◦ Are you able to reproduce the problem every single time you try? People use testing frameworks to not only replicate bugs, but to make sure the bug actually goes away 2. Blame your framework. ◦ Are you working with a framework or library of some kind? ◦ Are you sure it's not manipulating the results or getting in your way? 3. Blame your tools. ◦ Have you got cache enabled? Are you working in IE6? Try to ensure that your tools aren't giving you false-positives or false- negatives. Use an HTTP proxy like Charles Proxy to debug HTTP requests. 4. Blame your environment. ◦ Is your server configured the way you need it to be? ◦ Are you running the correct version of the software you need? ◦ Could another piece of software be getting in the way? ◦ Be sure to disable any caching technologies to ensure you're always looking at the most recent data, and that your actualcode is executing every time (not some cached version). ◦ Have you tried turning it off & on again? =) 5. Blame your language. ◦ Is the code you're debugging very complex or novel? Maybe it's a bug in your language (highly unlikely) ◦ Always break the problem down into as small a piece as you can - it's probably you 6. Blame the world. ◦ Have you tried logging a bug? ◦ Have you tried asking a question on Stack Overflow? ◦ Have you asked a friend? Sometimes speaking the problem out loud can give you insight into the problem at hand. Contribute: http://bit.ly/DebugChecklist 23 Sunday 06 October 13

Slide 28

Slide 28 text

YOURSELF TOOLS FRAMEWORK ENVIRONMENT LANGUAGE WORLD PROBABILITY 24 Sunday 06 October 13

Slide 29

Slide 29 text

TOOLS DEBUGGING TECHNIQUES 25 Sunday 06 October 13

Slide 30

Slide 30 text

SOFTWARE 26 Sunday 06 October 13

Slide 31

Slide 31 text

• INTELLISENSE! • SPOT ERRORS BEFORE THEY ARISE • SYNTAX HIGHLIGHTING - LESS COGNITIVE WORK • YOU DON’T NEED A GOOD MEMORY • YOUR MEMORY SUCKS • REFACTORING • TOOLS (PHING, PHPUNIT, BEHAT) WHY USE AN IDE? 27 Sunday 06 October 13

Slide 32

Slide 32 text

SOFTWARE 28 Sunday 06 October 13

Slide 33

Slide 33 text

29 Sunday 06 October 13

Slide 34

Slide 34 text

SOFTWARE 30 Sunday 06 October 13

Slide 35

Slide 35 text

31 Sunday 06 October 13

Slide 36

Slide 36 text

SOFTWARE 32 Sunday 06 October 13

Slide 37

Slide 37 text

33 Sunday 06 October 13

Slide 38

Slide 38 text

SOFTWARE 34 Sunday 06 October 13

Slide 39

Slide 39 text

'infomaniac', 'user_email' => '[email protected]', 'user_password_new' => '1234', 'user_password_repeat' => '1234', ); dd($data); 35 Sunday 06 October 13

Slide 40

Slide 40 text

L ANGUAGE USING THE RIGHT 36 Sunday 06 October 13

Slide 41

Slide 41 text

assertEquals($a + $b, 4); } public function dataProvider() { return array( array(3, 1), array(4, 0) ); } } 37 Sunday 06 October 13

Slide 42

Slide 42 text

Warning: Missing argument 1 for Test::test() in Test.php on line 8 ... There was 1 failure: 1) Warning The data provider specified for Test::test is invalid. Failed asserting that 4 matches expected 0. 38 Sunday 06 October 13

Slide 43

Slide 43 text

“PHPUNIT MISSING ARGUMENT DATAPROVIDER” 39 Sunday 06 October 13

Slide 44

Slide 44 text

REPORTING DEBUGGING TECHNIQUES 40 Sunday 06 October 13

Slide 45

Slide 45 text

ERROR REPORTING OPTIONS: error_reporting log_errors display_errors display_startup_errors xdebug.scream 41 Sunday 06 October 13

Slide 46

Slide 46 text

SETTING DEVELOPMENT PRODUCTION display_errors On Off (!!) display_startup_errors On* On error_reporting E_ALL | E_STRICT E_ALL log_errors Off On xdebug.scream On Off 42 Sunday 06 October 13

Slide 47

Slide 47 text

GENERAL NOTES 43 Sunday 06 October 13

Slide 48

Slide 48 text

• ALWAYS check your premises. Assuming makes you an ass. • ALWAYS make sure your problematic code is actually being executed. • ALWAYS be patient. Getting hot-headed, murdering prostitutes and swearing at co-workers won't solve the problem. • ALWAYS remember that you're human, the code you're writing was written by a human, the language with its frameworks, servers & environments were all written by humans. Shit happens - be humble. • ALWAYS share your experiences so that other poor souls can solve their problems quicker with your help. You may be a snowflake, but your code problems probably aren't. 44 Sunday 06 October 13

Slide 49

Slide 49 text

MORE INFO http://lanyrd.com/scpdkh [email protected] will exchange advice for whiskey 45 Sunday 06 October 13