Practice debugging with problem solving skill
Case 1 - for novice programmer:
We will debug based on the program's behavior in a programming game - BOTS[1]. You don't need to know much programming to debug this program
A robot needs to put weight (box or itself) on all yellow rings to launch to the next level. The robot's program consists of basic commands that guide its movement, at the unit of one square. For example, a "move forward" means the robot move forward one square on a flat ground.
The graph on the left is the starting environment, and the right is a student's solution(code, and resulted environment). Give two approaches to debug this program, and compare them.
(**You can play BOTS here! Play as a guest.)
Case 1 - for novice programmer:
We will debug based on the program's behavior in a programming game - BOTS[1]. You don't need to know much programming to debug this program
A robot needs to put weight (box or itself) on all yellow rings to launch to the next level. The robot's program consists of basic commands that guide its movement, at the unit of one square. For example, a "move forward" means the robot move forward one square on a flat ground.
The graph on the left is the starting environment, and the right is a student's solution(code, and resulted environment). Give two approaches to debug this program, and compare them.
(**You can play BOTS here! Play as a guest.)
Sample Answer:
1. Identify the problem:
The problem is that the robot blocked his own access to the last yellow ring.
2. Representing the problem
The original problem is: "moving robot to all cubics on yellow circle".
The new problem is: "move the box on the right to the inner yellow circle, so that the robot can step on the outer yellow circle".
3. Selecting a Strategy
Strategy A: "find the lines of code that moved the wrongly-place cubic. Make the robot move forward one more unit before putting the cubic down."
Strategy B: "add code at the end of this buggy program, for the robot to pick up the cubic on the right, move forward, and put the cubic down.".
Strategy B is easier. If we select strategy A, after edit the commands that moved the cubic to the correct yellow circle, the robot's location and facing direction will be changed. We may need to add more commands to connect it with the rest of the code.
Implementing and evaluating the strategy can be done easily in the game environment.
Sample Answer:
1. Identify the problem:
The problem is that the robot blocked his own access to the last yellow ring.
2. Representing the problem
The original problem is: "moving robot to all cubics on yellow circle".
The new problem is: "move the box on the right to the inner yellow circle, so that the robot can step on the outer yellow circle".
3. Selecting a Strategy
Strategy A: "find the lines of code that moved the wrongly-place cubic. Make the robot move forward one more unit before putting the cubic down."
Strategy B: "add code at the end of this buggy program, for the robot to pick up the cubic on the right, move forward, and put the cubic down.".
Strategy B is easier. If we select strategy A, after edit the commands that moved the cubic to the correct yellow circle, the robot's location and facing direction will be changed. We may need to add more commands to connect it with the rest of the code.
Implementing and evaluating the strategy can be done easily in the game environment.
Case 2 - for beginner in programming:
Nancy claims that her Java Script code just runs forever and won't return the result she expects. She designed an error detection method but no errors are thrown. She is very frustrated and does not know what to do. What is your Strategy to help her debug her code?
"private var angle : int = Mathf.RoundToInt( transform.eulerAngles.y / 90.0 );
private var xMove : int = 0; private var zMove : int = 0; function start(){ this.loadEnvironment(); } function MoveForward () { switch( angle ){ case 0: zMove = 1; break; case 1: xMove = 1; break; } nextBlockPos = Vector3( xBlockPos + xMove, yBlockPos, zBlockPos + zMove ); handlePotentialErrors(); } function move(){ if (startButton.clicked()}{ int i=0; while( i<10){ robot.moveForward(); robot.informLeaderBoard(); } } } |
handlePotentialErrors(){
//Out-of-bounds error detection if( (xBlockPos+xMove<0) || (xBlockPos+xMove>WorldManager.worldMaxX) || (zBlockPos+zMove<0) || (zBlockPos+zMove>WorldManager.worldMaxZ) ){ ThrowError( "I can't move forward! I'll fall off the edge!" ); return; } //Collision error detection if( WorldManager.block[ xBlockPos + xMove, yBlockPos, zBlockPos + zMove ].hasCollision ){ ThrowError( "I can't move forward! There's something in front of me!" ); return; } if( WorldManager.block[ xBlockPos + xMove, yBlockPos+1, zBlockPos + zMove ].hasCollision && carryObj != null ){ ThrowError( "I can't move forward! There's something in front of me!" ); return; } }" |
Sample Solution:
I honestly do not understand all the codes here. The code is long, and the definition of many functions are not included. However, many programs you see in real life, especially programs from large collaboration, look like this. Here is my strat
1. Identify the problem:
I would first set break point with a debugger, or use print statement to find from where the program starts to get stucked
I found once the program gets inside the below loop, it never get out.
if (startButton.clicked()}{
int i=0;
while( i<10){
robot.moveForward();
robot.informLeaderBoard();
}
}
2. Representing the problem
The original problem is: "the code runs forever and doesn't give me result".
The new problem is: "the loop is wrong".
3. Selecting a Strategy
I print out the values inside the loop at each iteration of the loop to investigate what is going on. I found i=0 in every round. I want i increase 1 in each iteration, until it reach 10. i realized i forgot increasing i.
4. Implementing a Strategy
I add i=i+1; after robot.informLeaderBoard();
5. Evaluating the Strategy
print i, see if it increases. Run the program, see if it gives the correct output
Sample Solution:
I honestly do not understand all the codes here. The code is long, and the definition of many functions are not included. However, many programs you see in real life, especially programs from large collaboration, look like this. Here is my strat
1. Identify the problem:
I would first set break point with a debugger, or use print statement to find from where the program starts to get stucked
I found once the program gets inside the below loop, it never get out.
if (startButton.clicked()}{
int i=0;
while( i<10){
robot.moveForward();
robot.informLeaderBoard();
}
}
2. Representing the problem
The original problem is: "the code runs forever and doesn't give me result".
The new problem is: "the loop is wrong".
3. Selecting a Strategy
I print out the values inside the loop at each iteration of the loop to investigate what is going on. I found i=0 in every round. I want i increase 1 in each iteration, until it reach 10. i realized i forgot increasing i.
4. Implementing a Strategy
I add i=i+1; after robot.informLeaderBoard();
5. Evaluating the Strategy
print i, see if it increases. Run the program, see if it gives the correct output
Case review
Before you realized it, we just experienced the two major debugging strategies with our problem solving knowledge[2].
Citations
[1] Drew Hicks, Veronica Catete, Tiffany Barnes. 2014. “Part Of The Game: Changing Level Creation to Identify and Filter Low-Quality User Generated Levels.” In Proceedings of the International Conference on the Foundations of Digital Games
[2] Yoon, B. D., & Garcia, O. N. (1998, March). Cognitive activities and support in debugging. In Human Interaction with Complex Systems, 1998. Proceedings., Fourth Annual Symposium on (pp. 160-169). IEEE.