C3.2 Read and alter existing code, including code that involves sequential, concurrent, and repeating events, and describe how changes to the code affect the outcomes.
Skill: Reading Code
Reading code involves interpreting blocks or commands to deduce their meaning. This allows the student to make predictions about the behaviour of the code. Reading code 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 determine the reason(s) why it is not working.
The more complex the code, the more important the ability to read code to identify errors becomes. Several questions can be formulated from the following code:
image Blocks of code:Events block stating “start on when green flag is clicked”.Motions block stating “go to x: ‘-200’ y: ‘-150'.Variable block stating “set row to ‘0’.”Looks block stating “show”.Control block stating “repeat 2”. Inside are 4 nested blocks.Control block stating “repeat 4”.Inside are 3 nested blocks. Controls block stating ”create clone of myself.Motion blocks stating “move ‘70’ steps.”Motion blocks stating “go to x ‘-200’ y: ‘-150’.”Variable blocks stating “change row by ‘1’.”Motions block stating “changing y ‘30’ variable block ‘row’.”Looks block stating “hide”.- What will happen when the code is executed?
- Is the code functional? How do you know?
- What is the intent of the code? Does the code meet the intent?
In the case of this code, if the desired result is to produce an array of two rows of four sprites, the code is functional. However, if the goal was to arrange 24 sprites in an array, the code would not produce the desired result.
Skill: Altering Code
The culture of computer programming is one of sharing, and many programs are created based on existing code. Modifying code first allows the student to start from an existing and functional code sequence in order to build something new. Modifying code can also be done to simplify it, or to demonstrate that there is more than one way to represent the same situation with code. If necessary, the student can also modify erroneous code to make it functional, a process called debugging.
Taking the previous code example, students can be asked to make changes to the code to represent 24 sprites. The beauty of the code is that students can make very small changes (e.g., change the 2 to a 6 in the first loop) or drastic changes (e.g., eliminate one of the loops completely to make a single row or column of 24 sprites).
Skill: Describing the Impact of Altering Code
Understanding the sequencing of events that will occur based on the code is important for altering code so that one can make predictions about how the changes will affect the outcome.
In the example of the array, it is possible, for example, to ask students whether the loop in the code below represents the number of columns or the number of rows in the layout.
image Blocks of code:Events block stating “start on when green flag is clicked”.Motions block stating “go to x: ‘-200’ y: ‘-150'.Variable block stating “set row to ‘0’.”Looks block stating “show”.Control block stating “repeat 6”. Inside are 11 nested blocks.Controls block stating ”create clone of myself.Motion blocks stating “move ‘70’ steps.”Controls block stating ”create clone of myself.Motion blocks stating “move ‘70’ steps.”Controls block stating ”create clone of myself.Motion blocks stating “move ‘70’ steps.”Controls block stating ”create clone of myself.Motion blocks stating “move ‘70’ steps.”Motion blocks stating “go to x ‘-200’ y: ‘-150’.”Variable blocks stating “change row by ‘1’.”Motions block stating “changing y ‘30’ variable block ‘row’.”Looks block stating “hide”.Students can then test their prediction by changing the number in the loop, and then observe the behaviour of the sprite.