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

In the Scratch coding program, 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 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 sill 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 his 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.