C3.2 Read and alter existing code, including code that involves sequential events, and describe how changes to the code affect the outcomes.

Skill: Reading Code


Code reading involves interpreting blocks or commands to deduce the meaning of the code. This helps the student make predictions about the behavior of the code. Code reading is also an important part of debugging, which is the identification and correction of errors in the code, because by reading the code, it is possible for the student to find the reason(s) why the code is not working. Note here that this skill does not correspond to the computer reading the code, but rather to the student reading the code to verify it.

For example, present the following situation to the student:

The interface of a game is a field. In the lower left corner of the screen is a frog next to 2 mushrooms. At the far right of the screen is another mushroom.A frog to the left of 5 objects with adjacent directions. The first 2 objects are marked with an arrow pointing to the right, once, the last 2 are marked with an arrow pointing up, once.


The student could predict the behavior of the frog before executing the code. In this situation, executing the code would not work because a starting condition (the green flag) is missing. Therefore, the student could also find errors in the code as a result of reading it and correct them.

Skill: Modifying Code


The culture of computer programming is one of exchange, and much software is created based on existing code. Modifying code gives the student the opportunity to start from an existing and functional code pattern in order to build something new. Modifying code can also be done to simplify it, or to show that there is more than one way to represent the same situation using code. If necessary, they may also modify erroneous code to make it functional, a process called debugging.

For example, a code that places Canadian coins in a pattern might place nickels as follows:

5

5 5

5 5 5

5 5 5 5

5 5 5 5 5

5 5 5 5 5 5

Are there other possible codes to represent this same value?

The student could modify the code to extend the pattern or to replace the nickels with sprites of different coins that would represent the same values.

Example

5 10 5

5 10 10 10 25 25

Skill: Describing the Impact of Changes on Code


As much as trial and error is an important part of creating code patterns, it is important that students can justify their choices of modifications to existing code by making predictions about the effect of those modifications on the behaviour of the code.

For example, sequential moves to the left followed by the same number of sequential moves to the right will result in a null move - the initial and final position of the sprite will not have changed. The student who understands this concept will be able to use moves in all directions to create series of 10 moves with a specific end point.

Example

A sprite must move two units to the right to be on top of another sprite, but the instruction is to create combinations of 10 moves. The student could make a move of six units to the right and four units to the left for a total move of 10 units, but the final position of the sprite is only two units away from its initial position.

This kind of exercise can be useful to link the learning of relative position with addition facts.

Knowledge: Sequential Events


The basis of all code is pattern. Sequential events are a set of instructions executed one after another, usually from top to bottom or left to right on a screen.

For example, in the map of a fictional city like the one shown below, one could start with a specific code and ask the student to predict the final location of the sprite or even modify the code so that the sprite could go to another location.

A road frames the green town with a few streets. The town has a house, a store, a police station, a hospital, a school, a fire station, 2 gas pumps, 2 traffic lights, a few trees and some road signs.Coding line:Start on green flag, move down one time 5 times, move right one time 2 times.

The pseudocode associated with this code might look like this:

Initial position = to the left of the yellow house

Starting condition = true

Move 5 steps

Direction = bottom ↓

Move 2 steps

Direction = right →

Final position = (unknown)

The student could predict, for example, that the final position of the sprite will be either at the store, the police station, or the gas station, since the sprite starts at the yellow house and moves down and to the right. They could then verify their prediction by running this code.