C3.2 Read and alter existing code involving the analysis of data in order to inform and communicate decisions, 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 make predictions about its outcome. Reading code is also an important part of debugging to identify and correct errors of why the code is not working or getting the desired outcome.

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, 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


One of the reasons to alter code is to save time by not starting from scratch but to use existing code to build something new. Altering code can also be done to make the code more efficient and reduce runtime or to demonstrate that there is more than on way to use code to represent a situation. Another reason to alter code is to fix errors and make it functional, a process called debugging.

For example, sometimes there are different coding tools that will do a job more efficiently than others. Altering code to make it useful in different program or in a different language can, in the long run, be more efficient.

The calculation of the mean for a set of data in block-based coding program might look like this:

While the same outcome can be obtained with a single line of code in a spreadsheet.

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.

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 their averaging sequence.

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.