How to Learn to Program: Reading Program Templates

Photo by Kelly Sikkema on Unsplash

Recognizing templates makes you a more expert programmer

The third step in a sequential instructional approach to learning computer programming is learning how to read program templates. A template is a set of programming instructions that represent a common code use pattern. Templates provide a means of moving from learning how to write code to being able to use programs to solve problems. Becoming an expert programmer involves learning to recognize when a specific template should be used to solve a problem.

Examples of Programming Templates

A first example of a template is “Variable swap”. The template looks like this:

  1. You are given two variables with values.
  2. Introduce a temporary variable and assign it the value of the first variable.
  3. Assign the first variable the value of the second variable.
  4. Assign the second variable the value of the temporary variable.

This template can be applied to every situation when you need to swap the values in two variables. This comes up most often in sorting applications, but it can happen in other situations as well.

Another template example is “Read one, process one”. The template is defined as:

  1. Get a value from the user or from a data structure.
  2. Perform some computation with the value.
  3. Go back to step 1 until there is no more data or the user chooses to stop.

This template can be used in several situations. One is when you need to read the elements of an array in order to perform some task, such as finding the average of a set of test scores stored in the array. Another example is when you want to prompt the user to enter data for either a set number of iterations or until the user chooses to quit by using a sentinel value.

A third template example is “Max/min”. This template is used to find the maximum and/or minimum values of a set of data. The template looks like this:

  1.  Use if statements to compare a first value with the other values in the data set.
  2. If there are remaining values to check, skip the first value checked and repeat step 1 for the other values.
  3. The else condition is reached if there are no other values to compare.

This template is used in many situations when searching or sorting data and is also a good template to use to have students practice using Boolean logic.

How to Practice Reading Code Templates

Students practice reading code templates by looking at templates and determining if they have been implemented correctly or not. If the template has not been implemented correctly, the student needs to be able to spot were the code was written incorrectly and correct it so that the template works the way it is supposed to.

Here is an example of an incorrect variable swap program in Python:

number1 = 1
number2 = 2
temp = number1
number2 = number1
number1 = temp

The student then does a variable trace on the program and discovers that number1 and number2 both end up with the same value – 1.

To correctly solve the problem, the student needs to see that after the first variable’s value is assigned to the temporary variable, the first variable needs to get the second variable’s value and the second variable gets the temporary variable’s value. The code should look like this:

number1 = 1
number2 = 2
temp = number1
number1 = number2
number2 = temp

Here is an example from another template – processing all the elements of an array, this time written in C++, and this code is also written incorrectly:

int grades[] = {81, 77, 75, 89, 78};
int total = 0;
for (int i = 0; i <= 5; i++) {
  total += grades[i];
}

When a variable trace is done on this program, including drawing out the array, the students sees that the loop goes beyond the upper bound of the array and accesses uninitialized data. Here is what a trace of this code looks like:

Variable trace for processing all elements of an array

In C++, this is not an error and the value of total will be modified by whatever numeric data (I call it junk data) is stored in memory at the location the program tried to access. Students need to be experienced at reading code to understand why this error occurred.

Describing Program Templates in English

Another exercise I use with my students is have them read code that implements a template and have them describe what the code does. This is a very eye-opening exercise for my students because they discover that having weak knowledge of either the template and the code, or both, makes it hard to write up a good description.

The following Python code implements a digit processing template that breaks down a number into its constituent digits:

n = 2945
print(n)
while n > 0:
  digit = n % 10
  print(digit, end=" ")
  n = int(n / 10)

An example description of this program is:

This is a digit-extraction program. The program breaks down a number into individual digits by computing the remainder of the number when divided by 10 (modulus) and then dividing the number by 10 to permanently remove the digit. Because Python does floating-point division, when the division operation is performed, the result must be converted to an integer before being stored back in the number. The program loops until the number reaches 0 or into the negative.

The process of having the student write out the description of the program helps seal the meaning of the program in the student’s mind. I often even have my students write this out on paper rather than type it up since recent research has shown hand-writing facts help students memorize them better than typing them up does because typing tends to cause the typist to just let the words flow through their fingers while they are typing rather than paying attention to the what the words are saying.

Program Templates are Chunks of Expertise

Researchers in the field of expertise have long found that experts become experts by “chunking” the important patterns in their field. The classic example is board-position chunks chess grand masters spend hours studying. By studying these positions over and over, the chess player will be able to recall them very quickly when needed during a chess match.

In much the same way, program templates are the board position chunks of programming, especially for beginning programmers as they transition to intermediate and advanced levels of expertise. By learning to read and understand how program templates work, the learning programmer makes these templates available to them for faster problem solving and more programming efficiency.

Reading and understanding program templates is the third step in the sequenced instructional approach to learning computer programming. The first step is learning to read code and the second step is learning to write code based on a set of clear instructions. The last step in the sequence is learning to apply program templates to unique situations and I’ll be covering this step in a future article.