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:
![](/img/activite/algebre/en/6e/VE6_Algebre_Image191_en.png)
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:
![](/img/activite/algebre/en/6e/VE6_Algebre_Image192_en.png)
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:
![](/img/activite/algebre/en/6e/VE6_Algebre_Image193_en.png)