Computer Science Canada

Momop's very own debugging guide to help you all out

Author:  Nick [ Wed Aug 22, 2007 9:12 am ]
Post subject:  Momop's very own debugging guide to help you all out

MOMOP'S GUIDE TO PERFECT DEBUGGING!!!


This is not really a tuturial so much as a guide. Don't you hate when you have written out a 1000 some odd program and it won't run or not how you want it too?

If you answered no then you can leave as this guide will not help you however if you answered yes then your normal

On to the guide

We will attempt to move a circle using the keyboard

now on to moving a circle!!!!!!!

Turing:
%The program that doesnt not run
%created by: Nick Hearn aka momop
%23-aug-07

%not too important just making program look nice
setscreen ("graphics:500;400,nobuttonbar,offscreenonly")

%some movement varibles
var circlex, circley : int
var key : array char of boolean

%draw your circle
drawfilloval (circlex, circley, 25, 25, red)

%now to set these varibles
circlex := 150
circley := 150

%and so on
loop
    %allow the user to move circle
    Input.KeyDown (key)
    if key ('w') then
        circley += 5
    elsif key ('s') then
        circley -= 5
    elsif key ('a') then
        circlex -= 5
    elsif key ('d') then
        circlex += 5
    end if
end loop


well that didnt work why not?

Turing:
varible has no value


well thats not good we set the varible but turing is not reading it let's check over the program

well first of all we did set the circlex and circley varibles
then we drew the circle
then we set the varibles

and thats where the answer lies...

we drew the circle before we set the varibles lets reverse that

Turing:


%The program that doesnt not run
%created by: Nick Hearn aka momop
%23-aug-07

%not too important just making program look nice
setscreen ("graphics:500;400,nobuttonbar,offscreenonly")

%some movement varibles
var circlex, circley : int
var key : array char of boolean

%now to set these varibles
circlex := 150
circley := 150

%draw your circle
drawfilloval (circlex, circley, 25, 25, red)

%and so on
loop
    %allow the user to move circle
    Input.KeyDown (key)
    if key ('w') then
        circley += 5
    elsif key ('s') then
        circley -= 5
    elsif key ('a') then
        circlex -= 5
    elsif key ('d') then
        circlex += 5
    end if
end loop


woot it runs!!!!

but wait I can't move the circle what's up wit dat yo?

lets review once more

we set the varibles
we set the varibles
we drew the circle
we created the main loop which houses the movement commands

once more we have found the answer!!!!!

we drew our circle before our loop so lets rewrite our program once more!!!!

Turing:


%The program that doesnt not run
%created by: Nick Hearn aka momop
%23-aug-07

%not too important just making program look nice
setscreen ("graphics:500;400,nobuttonbar,offscreenonly")

%some movement varibles
var circlex, circley : int
var key : array char of boolean

%now to set these varibles
circlex := 150
circley := 150

%and so on
loop

    %draw your circle
    drawfilloval (circlex, circley, 25, 25, red)
    %allow the user to move circle
    Input.KeyDown (key)
    if key ('w') then
        circley += 5
    elsif key ('s') then
        circley -= 5
    elsif key ('a') then
        circlex -= 5
    elsif key ('d') then
        circlex += 5
    end if
end loop


well it's still not working lets review once more *sigh*
we set our varibles, we created our loop with the draw command inside, this is a debugging guide and doesn't get into movement commands so thats not it, it must be something else

at the top where the program says it's just looking fancy... it's not, its also limiting movement with the "offscreenonly"

so what now? just erase it?

lets try

Turing:


%The program that doesnt not run
%created by: Nick Hearn aka momop
%23-aug-07

%not too important just making program look nice
setscreen ("graphics:500;400,nobuttonbar")

%some movement varibles
var circlex, circley : int
var key : array char of boolean

%now to set these varibles
circlex := 150
circley := 150

%and so on
loop

    %draw your circle
    drawfilloval (circlex, circley, 25, 25, red)
    %allow the user to move circle
    Input.KeyDown (key)
    if key ('w') then
        circley += 5
    elsif key ('s') then
        circley -= 5
    elsif key ('a') then
        circlex -= 5
    elsif key ('d') then
        circlex += 5
    end if
end loop


...well atleast it's working...

as you just saw the circle is moving too fast so lets slow it down a bit... you guessed it the delay([milliseconds]) command!!!

Turing:


%The program that doesnt not run
%created by: Nick Hearn aka momop
%23-aug-07

%not too important just making program look nice
setscreen ("graphics:500;400,nobuttonbar")

%some movement varibles
var circlex, circley : int
var key : array char of boolean

%now to set these varibles
circlex := 150
circley := 150

%and so on
loop

    %draw your circle
    drawfilloval (circlex, circley, 25, 25, red)
    %allow the user to move circle
    Input.KeyDown (key)
    if key ('w') then
        circley += 5
    elsif key ('s') then
        circley -= 5
    elsif key ('a') then
        circlex -= 5
    elsif key ('d') then
        circlex += 5
    end if
    delay(1000)
end loop


woot its working!!!!!... but not perfectly...

did you notice if you hold a direction the ball will keep moving after you've let go?

well lets fix that

but first an explanation of what is happening

when you press a key the program reads it and sets it in a line so if i hit the 'w' key the program would read:
w

however if i held the key 'w' the program would read:
wwwwwwwwwwwwwwwwwwww

each time the Input.KeyDown is reached it will knock off one key so:
wwwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwwww
wwwwwwwwwwwwwwww
wwwwwwwwwwwwwww
wwwwwwwwwwwwww
and so on

if i hit 'w' and 'd' then:
wd
d

or if I hit 'w' 'd' 's' and 'a' then:
wdsa
dsa
sa
a

but if I do not want this then i use the Input.Flush command

so if I had a prgram running like
Turing:
loop
   Input.KeyDown(key)
   Input.KeyDown(key)
   Input.Flush
   Input.KeyDown(key)
end loop

and I had typed wwwwwwww beforehand then:
wwwwwwwwww
wwwwwwwww
*nothing*

the third Input.KeyDown did not receice any keys because the Input.Flush got rid of them or flushed em down the toilet Razz

so back to the program

Turing:


%The program that doesnt not run
%created by: Nick Hearn aka momop
%23-aug-07

%not too important just making program look nice
setscreen ("graphics:500;400,nobuttonbar")

%some movement varibles
var circlex, circley : int
var key : array char of boolean

%now to set these varibles
circlex := 150
circley := 150

%and so on
loop

    %draw your circle
    drawfilloval (circlex, circley, 25, 25, red)
    %allow the user to move circle
    Input.KeyDown (key)
    Input.Flush
    if key ('w') then
        circley += 5
    elsif key ('s') then
        circley -= 5
    elsif key ('a') then
        circlex -= 5
    elsif key ('d') then
        circlex += 5
    end if
    delay(500)
end loop


thats much better!!! (ps you can lower the delay i had it higher to show the effects of Input stocking)

but not quite perfect... ya this is kinda repetitive but bear with me...

the circle leaves a trail, well ill take care off that...

the cls command is our friend this time
but there are 2 solutions to this...

either use the cls everytime a key is pressed or each loop run lets try both:

Turing:

    if key ('w') then
        circley += 5
        cls
    elsif key ('s') then
        circley -= 5
        cls
    elsif key ('a') then
        circlex -= 5
        cls
    elsif key ('d') then
        circlex += 5
        cls
    end if


or

Turing:

loop

    cls
    %draw your circle
    drawfilloval (circlex, circley, 25, 25, red)
    %allow the user to move circle
    Input.KeyDown (key)
    Input.Flush
    if key ('w') then
        circley += 5
    elsif key ('s') then
        circley -= 5
    elsif key ('a') then
        circlex -= 5
    elsif key ('d') then
        circlex += 5
    end if
    delay(500)
end loop


both ways work but for further work we'll stick to using cls each loop run for this program

i bet you've noticed the circle was flashing... didn't ya... did ya cause i did... i really did

anyway there is a way around this flashing and thats to bring back the "offscreenonly" but with his friend View.Update

heres how this works
offscreenonly draws all images off the screen which is why the circle did not move... it actually did just not on screen so you didn't see it move
whenever the View.Update command is used turing brings all images from offscreen onto the onscreen

Turing:


%The program that doesnt not run
%created by: Nick Hearn aka momop
%23-aug-07

%not too important just making program look nice
setscreen ("graphics:500;400,nobuttonbar,offscreenonly")

%some movement varibles
var circlex, circley : int
var key : array char of boolean

%now to set these varibles
circlex := 150
circley := 150

%and so on
loop

    %clear the screen to erase any trials
    cls
    %draw your circle
    drawfilloval (circlex, circley, 25, 25, red)
    %move the circle onscreen
    View.Update
    %allow the user to move circle
    Input.KeyDown (key)
    Input.Flush
    if key ('w') then
        circley += 5
    elsif key ('s') then
        circley -= 5
    elsif key ('a') then
        circlex -= 5
    elsif key ('d') then
        circlex += 5
    end if
    delay (500)
end loop


wow that seems perfect... not quite but almost there!!!!

if you pressed two keys at once you noticed the circle ignored one movement
thats because in an if statement once one requirement has been met it ignores all other elsif's and else's so what to do?

break up the if statement like so

Turing:
    if key ('w') then
        circley += 5
    end if
    if key ('s') then
        circley -= 5
    end if
    if key ('a') then
        circlex -= 5
    end if
    if key ('d') then
        circlex += 5
    end if


AND YOU'RE DONE we have just successfully debugged this program!!!!

but these arent the only ways to debug a program
heres some tips:

1)use put... put can also output varibles
use this if a varible is acting weird to help find the problem an example from our circle program would be

Turing:
put circlex


2)the break command... this will pause the program until the user hits the resume button on the button bar

use this to stop the program at any location to see what is happening

3) the assert command
this works like put but not in the same way

if a varible is acting strange and not how you want use assert to make sure the varible is what you want
the assert command will stop the program if a varible does not meet the assert requirements hence showing you where the problem is

an example:

Turing:
assert [var name] (>,>=,=,<=,<,not) [same argument type as [var name]]
so...
assert circlex>100


4)the debugger menu... to open this menu click on run at the very top of the turing window (not your programs run window as there is no run there)

be sure to click on the correct run as there are two the right one is above the other and slighty right to it
this should open a dropdown menu just click on show debygger menu from here and enjoy

well thats it thanks for your time
if there are any errors please PM me also note that some examples are not meant to run which is the point
have fun and never once again be frusterated at your program for not running

Author:  Mr. T [ Wed Aug 22, 2007 9:23 am ]
Post subject:  Re: Momop's very own debugging guide to help you all out

Good effort. In general, people (especially programming newbies) find themselves in debugging nightmares, because they build their programs without a preset plan. Before doing any coding, it is important to make a rough pesudo code outline, so that you can organize and visualize all the components of your program.

Author:  Nick [ Wed Aug 22, 2007 9:25 am ]
Post subject:  RE:Momop\'s very own debugging guide to help you all out

well thats a habit i should get into although im a pretty good debugger i still get typer's block and dont know what else to type
what im saying is write a pseudo code before programming and then maybe you wont even need my guide Razz

Author:  Aziz [ Wed Aug 22, 2007 9:34 am ]
Post subject:  RE:Momop\'s very own debugging guide to help you all out

"have fun and never once again be frusterated at your program for not running"

Don't give them false hope Wink

Anyways, Good job. I'd give you some bits, but I ain't got any!

Some tips: the proper syntax for the assert command is:

code:
assert _expression_


_expression_ is anything that evaulates to a boolean true/false. Thus you can call methods that may produce a boolean value. To use the assert is to make sure that something is what you expect, as you said. The following are pretty much the same:

code:
assert (expression)


code:
if not (expression) then
    % stop program with warning
end if


Also, in general, for tutorials you should organize your writing better, including using [ code ] tags where code is not exact syntax, using headings, and especially using proper grammar (Capitals, etc). Just makes it cleaner and easier to read.

Great job though Smile

+0 bits! (I'll try to give them, don't think it will let me)

Author:  Nick [ Wed Aug 22, 2007 9:39 am ]
Post subject:  RE:Momop\'s very own debugging guide to help you all out

thanks for the 0 bits the tips and the post Smile ill be sure to use proper tags headings and grammer in the future Razz

Author:  Mr. T [ Wed Aug 22, 2007 9:42 am ]
Post subject:  Re: Momop's very own debugging guide to help you all out

Grammar and spelling, as well. Unless you're Hacker Dan. Then you're excused. Wink

Author:  Aziz [ Wed Aug 22, 2007 9:43 am ]
Post subject:  RE:Momop\'s very own debugging guide to help you all out

I always forget that. Is grammer US spelling?

Author:  Mr. T [ Wed Aug 22, 2007 9:45 am ]
Post subject:  Re: Momop's very own debugging guide to help you all out

No. Grammer is just a phonetical misspelling of grammar.

Author:  Nick [ Wed Aug 22, 2007 9:46 am ]
Post subject:  RE:Momop\'s very own debugging guide to help you all out

lmao hacker dan's misspelling always makes me laugh Razz same with mr t's sig and aziz's

Author:  Aziz [ Wed Aug 22, 2007 10:24 am ]
Post subject:  RE:Momop\'s very own debugging guide to help you all out

My sigs were copied directly from users posts on the forum/irc channel. And Dan's spelling has gotten better since I got here. (Here, read this: http://compsci.ca/v3/viewtopic.php?t=12897)

Author:  rdrake [ Wed Aug 22, 2007 11:19 am ]
Post subject:  RE:Momop\'s very own debugging guide to help you all out

Here, have the 10 bits plus some more that Aziz was going to give you. Just drop into the IRC channel sometime after 7 PM or so if you require editing of your tutorials. I'll be happy to help.

Author:  Nick [ Wed Aug 22, 2007 5:17 pm ]
Post subject:  RE:Momop\'s very own debugging guide to help you all out

oh wow i had no idea about Dan must be hard with the youth of today always pointing out diffrences and demanding perfection... but still for a dyslexic to create such a great site and live his life regardless he has really proved to be just like anyone else

Author:  Nick [ Sun Nov 04, 2007 10:27 pm ]
Post subject:  Re: Momop's very own debugging guide to help you all out

after reading Tony's blog i found 2 new ways to debug

rubber ducking
and
super paper programming


: