4 Program Templates Every Beginning Programmer Needs to Learn

Program templates are the key mental representation for successful programming

Photo by Patrick Amoy on Unsplash

Program templates are common patterns of programming code usage that come up again and again as solutions to programming problems. A key skill in computer programming is learning to recognize these patterns and then applying them to unique situations. In this article I’m going to discuss four program templates every beginning programming student should commit to memory and practice recognizing and applying in their programming practice.

Program templates, especially these beginning templates, are often not taught specifically either in textbooks or by programming instructors. They should be, though, as learning them will give students key mental representations that make writing their programs easier.

An old Pascal textbook I happen to own, Designing Pascal Solutions, by Mike Clancy and Marcia Linn, teaches and uses program templates extensively and you should look for this book if you are interested in learning about program templates. The program templates I discuss in this article come directly from this book.

This book is also a great example of a textbook that emphasizes project-based learning. I find that students learn better when they have a set of projects to work on, rather than just unrelated problem sets. I’ll talk more about project-based learning for teaching computer programming in another article.

Input-Process-Output

In many ways, every computer program you write is an application of this template. If you are not just writing a “Hello, world!” program, your program will be taking some data in (input), transforming (process) it in some way, and displaying the result (output) or doing something else with it, such as writing it to a file. For the beginning programmer, applying this template is the best way to get started in writing a program to solve a problem.

This template can be written in pseudocode as:

Input all the values
Process all the values
Output the results of the processing

Here is an example of a problem that uses this template:

Find the grade average by first entering three grades from the keyboard. Add the three grades together to get a total. Divide the total by the number of grades to get the average. Display the average.

Below is the sample code to solve this problem in Python. I require students to add a comment before each step of the template to indicate their knowledge of the template.

#input
grade1 = int(input("Enter the first grade: "))
grade2 = int(input("Enter the second grade: "))
grade3 = int(input("Enter the third grade: "))
num_grades = 3
#processing
total = grade1 + grade2 + grade3
average = total / num_grades
#output
print("The grade average is",average)

Having students memorize this program template helps them organize their thoughts on how to go about solving problems. This lessens the cognitive load so that they can use it to concentrate on getting the syntax correct as well as the logic necessary to solve the problem.

Prompt, Then Read

This template is a very basic pattern but one that beginning students don’t necessarily know. When teaching this, I emphasize that the prompt needs to be very informative so that the user enters exactly the information needed.

The pseudocode for this template is:

Display a message asking for a specific input (value)
Read the input (value)

Here are some code examples of this template in JavaScript:

putstr("Enter your six-digit id number: );
id_number = readline();
putstr("Enter your first name: ");
firstName = readline();
putstr("Enter your last name: ");
lastName = readline();
putstr("Enter your salary: ");
salary = parseInt(readline());

It is especially important for new programming students to learn and understand the importance of clear prompts for guiding users as to how input should be received. I find this instruction is also useful in helping students learn the importance of the specificity of data so that proper values are computed.

Do Something a Specified Number of Times

This is the first template I teach my students when I introduce them to for loops. In fact, I don’t introduce loops explicitly first, I introduce the template and then demonstrate the for loop as a means of implementing the template.

Here is the template:

Repeat a specified number of times:
  Perform some action

The first example I demonstrate in class is a classic “Hello, world!” example – Display “Hello, world!” five times on the screen. Here is the code in C++:

# write "Hello, world!" to the screen five times
for(int i = 1; i <= 5; i++) {
  cout << "Hello, world!";
}

Then I will modify the example and combine it with the “Input One, Process One” template, by having students prompt the user to enter their first name and having the program say hello to the name entered:

string firstName;
# say "Hello" to a name five times
for (int i = 1; i <= 5; i++) {
  #input one (also prompt and read)
  cout << "What is your first name? ";
  cin >> firstName;
  #process one
  cout << "Hello, " << firstName << "!" << endl;
}

At this point, my students are ready for a problem so I give them this one: Prompt the user to enter five grades and add them to a total. Here is the code I am looking for as a solution:

int grade;
int total = 0;
const int NUM_GRADES = 5;
#enter and total five grades
for (int i = 1; i <= NUM_GRADES; i++) {
  #input one - also prompt and read
  cout << "Enter a grade: ";
  cin >> grade;
  #process one
  total = total + grade
}

Notice that the comments for each step are required. Again, having my students enter a comment for each step in the template helps cement in their minds the steps of the process.

Input One, Process One

This template is introduced when I teach my students about loops. I usually introduce it as part of a problem that involves having the user input some data and then doing something to that data.

The pseudocode for this template is:

Repeat the following steps:

  Input a value
  Do something to the value

I typically use this problem to introduce the template and to have my students practice writing loops: Write a program that prompts the user to enter a grade and add the grade value to a total variable.

The code I am looking for looks like this (in Python):

count = 1
total = 0
while count <= 5:
  #input one
  grade = int(input("Enter a grade: "))
  #process one
  total = total + grade
  count = count + 1

The comments before the steps in the template are important and I always require them so that I know the student understands how the template works. Not only do these comments confirm their template knowledge, but they also help the student build a solid mental representation of the template by explicitly saying where they are in the template’s steps.

Program Templates are Chunks of Programming Expertise

Researchers in the expertise field have long recognized that experts become experts by “chunking” the key skills and techniques in their field. The classic example is the thousands of board positions chess grand masters intimately know through years and years of intense study. The important thing to know about chunking is that these chunks are not learned through just playing more games of chess. The people who become chess grand masters study the board positions in isolation from a complete game, then they are able to apply this knowledge in order to gain advantages over their opponents in actually chess matches.

The beginning programmer can use program templates to the same advantage. Spend some time memorizing the various program templates, both why and how they are used to solve regularly-encountered programming scenarios, and you will find that you will more quickly master your programming language.

Then, when you feel you’ve mastered these templates, start using them to solve harder and harder problems. This is the essence of deliberate practice and it is what always separates the experts in a field from the rest. You won’t become an expert by solving the same problems time and again. You have to continually challenge yourself with new and interesting problems.