C3.2 Read and alter existing code, including code that involves conditional statements and other control structures, and describe how changes to the code affect the outcomes and the efficiency of the code.

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. Note that we are not talking about the computer reading the code, but rather the student reading the code and checking it.

When reading code, the student should notice, among other things, repetitions of commands or blocks that could be more efficiently expressed as a loop. They could, for example, read the code to identify repetitive elements in it:

Blocks of code:Events block stating, “start on when green flag clicked.”Motions block stating, “go to x: -200, y: -150.”Variables block stating, “set row to 0”.Looks block stating, “show”.Controls block stating, “repeat 4”Inside 15 nested blocks. Controls block stating, “create clone of myself”.Motions block stating, “move 70 steps”.Controls block stating, “create clone of myself”.Motions block stating, “move 70 steps”.Controls block stating, “create clone of myself”.Motions block stating, “move 70 steps”.Controls block stating, “create clone of myself”.Motions block stating, “move 70 steps”.Controls block stating, “create clone of myself”.Motions block stating, “move 70 steps”.Controls block stating, “create clone of myself”.Motions block stating, “move 70 steps”.Motions block stating, “go to x: -200 y: -150”.Variable block stating, “change row by 1”.Motions block stating, “change y by opetators block “30, multiplied, row””.Looks block stating, “hide”.

Note also that there are several ways to make code more efficient (using variables to represent values that are repeated in various places in the code, for example).

Skill: Altering Code


The culture of computer programming is one of sharing, and many pieces of software are created based on existing code. Altering code first allows the student to start from an existing and functional code to build something new. The alteration of a code can also have as a goal to simplify it, or to demonstrate that there is more than one way to represent the same situation with code. We could also talk here about efficiency, which in the context of coding means using a minimum number of blocks or commands to achieve the desired outcome. If necessary, the student can also alter erroneous code in order to make it functional, a process called debugging.

For example, in the code above, we see elements that could be better represented using a loop. The student could first alter the code to look something more like this:

Blocks of code:Events block stating, “start on when green flag clicked.”Motions block stating, “go to x: -200, y: -150.”Variables block stating, “set row to 0”.Controls block stating, “repeat 4”Inside 4 nested blocks. Controls block stating, “repeat 6”Controls block stating, “create clone of myself”.Motions block stating, “move 70 steps”.Motions block stating, “go to x:-200 y:-150”.Variable block stating, “change row to 1”.Motions block stating, “change y by operator block “30, multiplied row””.Looks block stating, “hide”.

Skill: Describing the Impact of Changes on Code


As much as trial and error is an important part of creating code, it is important that the student be able to justify their choices of changes to an existing code by making predictions about the effect of the changes on the outcome of the code. This is also important when altering code to make it efficient, as it is possible to introduce changes that have an unintended or negative impact on the final outcome.

For example, students could use their knowledge of integers to write the effect of a sign change (+/-) on displacements in the following code:

Blocks of code:Events block stating, “start on when green flag clicked.”Motions blocks stating, “go to x: 0 y: 0”.Control block stating, “wait 1 seconds”.Motions block stating, “change x by operators block stating, “minus 3 multiplied 30”.Motions block stating, “change y by operators block stating, “3 multiplied 30”.Control block stating, “wait 1 seconds”.Motions block stating, “change x by operators block stating, “10 multiplied 30”.Motions block stating, “change y by operators block stating, “1 multiplied 30”.