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

JavaScript Debugging

JavaScript Debugging

Amjad Masad

May 05, 2014
Tweet

More Decks by Amjad Masad

Other Decks in Programming

Transcript

  1. WTF

  2. • WORK AT FACEBOOK & BEFORE THAT WAS FIRST ENGINEER

    AT CODECADEMY • JAVASCRIPT EXPERIMENTALIST • MADE REPL.IT, DEBUG.JS, SOUNDOFJS etc • amasad.me, @amasad ABOUT ME
  3. • CODE THAT’S EASIER TO DEBUG • DEBUGGING TIPS AND

    TRICKS • THE TOOLS JAVASCRIPT DEBUGGING
  4. CLOSURES HIDE THEIR STATE REALLY WELL function counter(value) {! !

    return function() {! ! ! value++;! ! ! return value;! ! };! } class Counter {! ! constructor(val) {! ! ! this.value = val;! ! }! ! increment() {! ! ! this.value++;! ! ! return this.value;! ! }! }
  5. PARTIAL APPLICATIONS AND LAZY EVALUATION ARE EVEN HARDER TO DEBUG

    function counter(value) {! ! return function() {! ! ! return value;! ! };! }! ! function increment(counter) {! ! return function() {! ! ! return counter() + 1;! ! };! }! ! var count = counter(2);! count = increment(count);! console.log(count());
  6. ALWAYS BE DEBUGGING • HAVE THE PROGRAM IN A RUNNABLE

    STATE • SPLIT UP BIG CHANGES INTO SMALLER STEPS • BREADTH FIRST > DEPTH FIRST • KEEPS THE MOMENTUM GOING
  7. CHANGE AND BREAK THINGS • RUNNING THE PROGRAM OFTEN ALLOWS

    YOU TO TAKE BOLD STEPS • WHILE IN THE DEBUGGER CHANGE THINGS • LEAN ON YOUR TOOLS
  8. LIVE EDIT • FIX MISTAKES WITHOUT RELOADING • IMPLEMENT FEATURES

    WITHOUT RELOADING • ADD CONDITIONAL DEBUG STATEMENTS
  9. SETTING UP TRAPS • IN LARGE APPLICATIONS AND TEAMS IT’S

    HARD TO KNOW WHAT EVERYONE IS DOING • CAN’T STEP-DEBUG ALL THE CODE • NEED A WAY TO BREAK ON ACTIONS
  10. BREAK ON METHOD CALLS Event.prototype.preventDefault = function() {! ! debugger;!

    ! return _original.apply(this, arguments);! } • CAN’T SET A BREAKPOINT ON NATIVE METHODS • NEED TO KNOW SOURCE FOR METHOD
  11. BREAK ON CUSTOM EVENTS monitorEvents(elem);! ! user.on('change', function() {! debugger;!

    }); • WHEN AN EVENT IS FIRED, I WANT STOP AND INSPECT • NEED TO MAKE SURE AN EVENT IS FIRING
  12. BREAK ON PROPERTY ACCESS • WTF CHANGES • SETTERS AND

    GETTERS counter.__defineSetter__(! ! 'value',! ! function() { debugger; }! );
  13. BREAK ON CALLBACKS • SIMILAR TO CUSTOM EVENTS • TO

    MAKE SURE AN ASYNC PROCESS FINISHES user.save(function() {! ! debugger;! });
  14. BREAK ON DOM MUTATION • CAN BE DONE FROM DEVTOOLS

    • FINE GRAINED CONTROL • ASYNC CALL STACK
  15. BREAK ON OBJECT MUTATION • Object.observe! • BREAK ON PROPERTY

    ADD, CHANGE, DELETE • ASYNC CALL STACK (NOT WORKING YET)
  16. PROBLEMS WITH LIVE EDIT • LIVE EDIT IN CHROME IS

    NOT FLEXIBLE • CAN’T HAVE A BUILD STEP • CAN’T HAVE A REMOTE FILE SYSTEM • LOCKED INTO THE DEVTOOLS EDITOR
  17. INTRODUCING flo • OPEN SOURCE TOOL BY FACEBOOK • LIVE

    EDIT JAVASCRIPT, CSS, IMAGES • SUPPORTS BUILD STEPS AND ANY DEV ENV • EASY TO INTEGRATE AND HACKABLE
  18. WHY flo • I JOINED FACEBOOK LAST YEAR • A

    LOT OF JAVASCRIPT GOODNESS • RELOADING AND GETTING TO THE USER FLOW TAKES A LONG TIME • COMPLEX DEV ENVIRONMENT
  19. HOW flo • SERVER COMPONENT IS NPM MODULE • CLIENT

    COMPONENT IS CHROME EXTENSION • facebook.github.com/fb-flo