C3.2 Read and alter existing code, including code that involves events influenced by a defined count and/or subprogram 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 outcome 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.

Reading code that uses subprograms requires practice and an understanding of the general structure of the code in question. For example, when reading the following code:

Blocks of code:Events block stating, “when green flag clicked.”List block stating, “delete all of list of shoe sizes.”My blocks stating, “data collection.”My blocks stating, “sort list in increasing order.”

the student will be able to deduce that the code will remove items from a list, do a data collection, and sort the data in ascending order. However, a great deal of information is omitted from this code, as the blocks in pink call for subprograms. The student must first stop reading the main code to look at the subprogram in question to understand the meaning of the code.

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, the student may be trying to put a set of data in ascending order, but does not know how to do this. The student searches the Internet and finds a code that includes elements of an answer to their problem. The student can take this code and alter it to meet their needs. Some coding platforms even offer the option of taking a copy of the code to make what is called a remix, with an automatic thank you to the person who programmed the original code.

Remix - Thanks to ettorac for the orignal projet 7e tendances cnetrales

In the Scratch coding software, the Remix button allows you to create a new project with existing code assembled by someone else. When this option is used, a credit is automatically given to the original user of the code.

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 alterations to an existing code by making predictions about their effect 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, when the student tries to make a code more efficient with subprograms, an error can easily appear in several places. In the example code that follows, the student chose to create a block to define his averaging sequence.

My block stating, “define average.”Variables block stating, “set list number to 1.”Variables block stating, “set sum to 0.”Control block stating, “repeat’’ list block stating, “length of list of shoe size.”Inside 2 nested blocks.Variables block stating, “change sum by’’ list block stating, “item” list number of list of shoe sizes.”Variable block stating, “change list number by 1”Looks block stating, “say join the average shoe size for the class is sum divided length of list of shoe sizes.”

First, it would be possible to ask the student questions about their choices in defining this block. For example:

  • How would your program change if the "say" block was in the main code instead of the subprogram?
  • Is it better to set the variables to 0 at the beginning of the main code or at the beginning of the subprogram? How would this change the outcome of your program?
  • What is the advantage of isolating the code for the mean in a subprogram instead of placing it in the main code?

Note that the answers to all of these questions will vary considerably from student to student given the variety of ways in which the same situation can be represented with code.