Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd...

39
Debugging It is what you do

description

"Poor Man's" debugging Find how to OUTPUT (print) variables alert( this ); console.log( this ); //need dev tools Find out how to INPUT (type or choose) prompt( this );

Transcript of Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd...

Page 1: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

DebuggingIt is what you do

Page 2: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Debugging

• Where your time is spent:• 1st place: documentation• 2nd place: debugging

• Most valuable skill:• Debugging!

Page 3: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

"Poor Man's" debugging

• Find how to OUTPUT (print) variables• alert( this );• console.log( this ); //need dev tools

• Find out how to INPUT (type or choose)• prompt( this );

Page 4: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

TechniquesA few formal approaches

Page 5: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Science Experiments

• Guess (theory)• Change something (variable)• Run it (experiment)• repeat the trial and error

• could be formal or informal

Page 6: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Test Cases• Program testing code

• Recycle your experiments!• Repeatable• Automate repetitive tests• Do stress testing

• Test Driven Design (TDD)

Page 7: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

//buggy code:var dog;function bad_dog(){ dogg= "woof";}

//test case #1:bad_dog();if( ! dog ){ alert( 'dog was not set' );}

Page 8: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

assertions• test cases PUT INTO THE PROGRAM• preconditions

• check that things are as you expect• postconditions

• check that result is as expected• Not popular for scripting / runtime

Page 9: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

function average(){ if(arguments.length==0){ throw('nothing to average!');} var total=0; for(i=0; i<arguments.length; ++i){ total+= arguments[i]; } return total / arguments.length;}average(); // incorrect use

Page 10: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

• Break problems down; simplify• Science tests the fewest variables• Block Comments /**/ <!-- -->• Test files

• Duplicate: delete without worry• Test pages: use as a future reference

Divide and Conquer

Page 11: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

• Remove extraneous stuff• The "innocent" may only look it...

• Call function / methods directly• try out a sub-system• console; or write a little code

• Deconstruct function code• break it but see that it acts as you

expect

Page 12: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Postmortem• Bug happened; it crashed/died etc.• Dumps/Logs (not likely here)• Browser Error Console might help• Trace - using the info you have, you

follow the steps taken to cause the problem

• Like a Murder Mystery

Page 13: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Firefox Console

Page 14: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Firebug Console

Page 15: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

tracing• Postmortem needs evidence• Use Poor man's debugging technique• OUTPUT (print or log) during exec• Mental tracing would be reading the

code and following it's flow• 1st attempt is ok; but will u see it?

Page 16: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

function goofy_function(x){ alert(x); x+= 3; alert(x); if(x = 5 ){ x*10; } alert(x);}

Page 17: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Reconstruction• Rewrite in smallest steps possible• Useful for exploring APIs• Useful in a console…• Useful in LEARNING• Could end up in another

implementation; in which case chuck the buggy one.

Page 18: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!
Page 19: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Tool of the tradeDebuggers

Page 20: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

• Run, Break, Step, Step In, Step Out• Break points - mark lines in code• Variable Viewer and/or Watch• Call Stack• Profiler• Console and/or command line tools

Debugger features

Page 21: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

most popular javascript debuggerFirebug Debugger

Page 22: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Firebug add-ons

• This add-on has it's own add-ons!• FireRainbow

• adds color coding to js debugger• Firebug Autocompleter

• adds autocomplete to console!

Page 23: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

https://getfirebug.com/javascript

Page 24: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Choose .js file

Page 25: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Exec Controls

Page 26: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Exec Controls

• Rerun (reload script)

• Continue (run/play)

• Step Into function

• Step (aka "step over")

• Step Out of function

Page 27: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

QuickTime™ and aGIF decompressor

are needed to see this picture.

Position Displays In Code

• Example steps three times

• "Step Over"

• Clicking Continue/run

• does not show

• stops on breaks

Page 28: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Set Break Points

Page 29: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

QuickTime™ and aGIF decompressor

are needed to see this picture.

Set Break Points

Page 30: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Clear Break Points

Page 31: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Clear Break Points

• All break points set

• Disable (checkbox)

• Delete (X circle on right)

Page 32: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

FireRainbow

• Default install may be all BLACK

• Script Tab of Firebug:

• Colors tab on right

• Reset to default

• Randomize…

Page 33: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Firebug Autocompleter• Automatic in Console

• DOM Reference!

• View menu:

• Sidebar:

• Firebug Autocompleter

• Displays last autocompleted word

Page 34: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!
Page 35: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Firefox ConsoleBuilt-in tools rapidly evolving

Page 36: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

Built-in Firefox

Page 37: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

type help Show Console

HTML / CSSInspector

Javascript Debugger

Page 38: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

• Not as powerful or as mature as Firebug

• No add-on installations• Command line tool is powerful

• it is NOT a javascript console• Console is more compact than firebug• FASTER RUNNING• Inspector the is weak too; debugger ok

Page 39: Debugging It is what you do. Debugging Where your time is spent: 1 st place: documentation 2 nd place: debugging Most valuable skill: Debugging!

3D html inspector