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

The Debugging Checklist

The Debugging Checklist

Debugging can inspire homicidal thoughts, but it can also be fun!
Video from slide 6 can be found at http://www.youtube.com/watch?v=2Rt7w56RChc&feature=youtu.be

Danny Kopping

October 04, 2013
Tweet

More Decks by Danny Kopping

Other Decks in Technology

Transcript

  1. THE • DEBUGGING • CHECKLIST by Danny Kopping PHPSouthAfrica Cape

    Town October 2013 http://lanyrd.com/scpdkh 1 Sunday 06 October 13
  2. 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
  3. THE FIRST STEP IN SOLVING ANY PROBLEM IS ISOL ATING

    ITS CAUSE(S) 8 Sunday 06 October 13
  4. ‣ REDUCE SOLUTION SCOPE ‣ REDUCE ITERATION TIME ‣ REDUCE

    COMPLEXITY ‣ REDUCE FRUSTRATION LEVELS 11 Sunday 06 October 13
  5. <?php function register() { if (!empty($_POST)) { $msg = '';

    if ($_POST['user_name']) { if ($_POST['user_password_new']) { if ($_POST['user_password_new'] === $_POST['user_password_repeat']) { if (strlen($_POST['user_password_new']) > 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
  6. 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
  7. ‣ REDUCE SOLUTION SCOPE <?php $username = 'ilikethisusername!'; if (preg_match('/^[a-z\d]{2,64}$/i',

    $username)) { } else $msg = 'Username must be only a-z, A-Z, 0-9'; echo $msg; 14 Sunday 06 October 13
  8. ‣ REDUCE ITERATION TIME <?php $data = array( 'user_name' =>

    '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
  9. ‣ REDUCE ITERATION TIME <?php class UserTest extends PHPUnit_Framework_TestCase {

    const USERNAME_PATTERN = '/^[a-z\d]{2,64}$/i'; /** * @dataProvider usernamesProvider */ public function testUsername($expected, $username) { $this->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
  10. ‣ REDUCE COMPLEXITY <?php use Valitron\Validator; require_once __DIR__ . '/vendor/autoload.php';

    $data = array( 'user_name' => '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
  11. ‣ 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
  12. “If you hear hoofbeats, don’t assume it’s a unicorn” *

    * or a one-horned oryx 21 Sunday 06 October 13
  13. 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
  14. • 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
  15. <?php require_once 'vendor/autoload.php'; $data = array( 'user_name' => 'infomaniac', 'user_email'

    => '[email protected]', 'user_password_new' => '1234', 'user_password_repeat' => '1234', ); dd($data); 35 Sunday 06 October 13
  16. <?php class Test extends PHPUnit_Framework_TestCase { /** * @dataProvider dataProvider

    */ public function test($a, $b) { $this->assertEquals($a + $b, 4); } public function dataProvider() { return array( array(3, 1), array(4, 0) ); } } 37 Sunday 06 October 13
  17. 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
  18. 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
  19. • 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