Debugging Aids |
JumpUp(); MoveAcross(); JumpDown();To find out why Karel is going on some crazy path, we can turn the last two function calls into comments, like this:
JumpUp(); /* MoveAcross(); JumpDown(); */Because the compiler ignores comments (that is, does not try to translate a comment into machine language), the MoveAcross and JumpDown functions will not be invoked. Admittedly, we could have simply deleted the calls but then we would have had to remember what we had written when it comes time to reinsert the code. If Karel "jumps up" as intended, we can move the /* down, like this:
JumpUp(); MoveAcross(); /* JumpDown(); */and see what happens. If we now discover that Karel does not behave as intended, we have narrowed down the bug to inside the MoveAcross function. Once we have repaired the MoveAcross function, we can remove all of the comment delimiters.
(NOTE: Once finished debugging, be sure to check that no code remains commented out. It is considered a bad practice to submit any program that still contains commented out code; see the article Code in Comments for a discussion on why this is a bad practice.)
NOTE: Besides these statements (functions), toggling between the modes can be done by hitting the # key.
For example, assume a programmer had placed
Trace("End of while loop");at some strategic spot in their program code. Then when that spot was reached and the Trace function invoked, a message would flash in the upper left-hand corner something like this:
End of while loop . CORNER FACING BEEP-BAG BEEP-CORNER [A Stair Cleaning Task ] (2, 3) EAST 2 0 . ST.+-----------------------------------------------------------------------+ 9 | . . . . . . . . . . . . . . . . . . | | | 8 | . . . . . . . . . . . . . . . . . . | | | 7 | . . . . . . . . . . . . . . . . . . | | | 6 | . . . . . . . . . . . . . . . . . . | | | 5 | . . . . . . . . . . . . . . . . . . | | | 4 | . . . . 1 . . . . . . . . . . . . . | | +---+ | 3 | . . . 1 | . | . . . . . . . . . . . . . | | +---+ | | 2 | . > > | . . | . . . . . . . . . . . . . | | +---+ | | 1 | . ^ | . . . | . . . . . . . . . . . . . | +-------+-----------+---------------------------------------------------+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 AVE.
NOTE: The BreakPoint function is likely to be of more use than any of the other debugging functions.
TIP: If using the Bell function, always invoke it as Bell(1);
Example | Explanation |
void FaceWestIfFacingSouth() { Trace("inside FaceWestIfFacingSouth function"); if (facingSouth) { do { TurnLeft(); } while (notFacingWest); } } |
To help determine if the FaceWestIfFacingSouth() function is being invoked, the programmer has placed a Trace() at the beginning of the function. Each time the FaceWestIfFacingSouth() function is entered, the trace message will flash across the top of the Karel execution screen. |
void PickBeepersToWall() { if (nextToABeeper) do { PickBeeper(); } while (nextToABeeper); while (frontIsClear) { BreakPoint("Another iteration with frontIsClear"); Move(); if (nextToABeeper) do { PickBeeper(); } while (nextToABeeper); } Bell(1); } |
The logic to the left has Karel pick up all beepers between him and the wall ahead of him, including beepers on Karel's beginning street corner. Each time the while loop begins, a message is displayed in the upper left-hand corner of the execution screen ("Another iteration with frontIsClear") and execution pauses until the spacebar is hit or Continuous mode execution is resumed. When the while loop is finally exited, a bell sound is rung. |
Leave the problem alone and get some rest. Often even a short break will allow your mental processes to get back on track to finding the source of your problem! Some final advice: Often programming errors are hard to find but, once discovered, painfully obvious. Do not be too upset with yourself when you find these errors. If you find a "stupid" error, do not think "I'm really dumb"; instead think "I must be a genius to locate such an obscure and hard-to-spot error!" Granted, both are overstatements, but the second is closer to the truth than the first. |