How Introductory Computer Programming Should Be Taught

Hint: It’s not by writing Hello, world! first

Computer programming has been taught in the same manner for thirty or forty years, or at least since I entered the field. Have the student open an interpreter or compiler and write a program that displays Hello, world! on the screen. Then teach a few concepts, have the student write programs using those concepts, rinse, and repeat. There is a better way to teach programming, though, and in this article I’m going to share this new curriculum with you. This curriculum is based on a paper written by a group of researchers at the University of Washington’s Information School, with the lead author being Benjamin Xie.

A Theory of Teaching Computer Programming

Xie and his co-authors outline a sequence of steps for teaching and learning computer programming. I’ll outline the steps here and then delve deeper into each step in the rest of the article. The steps are:

  1. Learn to read code by doing variable traces, construct by construct.
  2. Learn to write construct syntax.
  3. Learn to understand how to read coding templates.
  4. Use coding templates to write code.

The authors’ beliefs are that writing code to solve problems before you thoroughly understand what a construct does and how its syntax is formed is getting ahead of the game. And, once you understand how to write the syntax for a construct, you’re not ready to solve problems using that construct until you’ve seen templates that demonstrate how to use the construct in a program.

Let’s break these steps down further to see what needs to be accomplished in each step.

Variable Tracing

The first part of learning to program needs to be learning how to read programs and understand what they do. The best way to do this is to write up a trace of all the variables in a program. Tracing variable values from the beginning to the end of a program demonstrates the learner can follow the flow of a program and understands all the nuances of the programming constructs used in the program. For example, here is a simple code fragment I often ask my students to trace (in C++):

const int SIZE = 5;
int grades[SIZE] = {71, 82, 77, 92, 84};
int total = 0;
for (int i = 0; i < SIZE; i++) {
  total += grades[i];
}

The trace students write looks like this:

SIZE: 5

i: 0, 1, 2, 3, 4, 5

total: 0, 71, 153, 230, 332, 416

Being able to trace code in this way does two things: 1) it shows that the student understands the semantics of the program; and 2) it shows that the student can predict the effect of syntax on the program’s behavior. These skills are very necessary before the student can begin writing their own programs.

Writing Program Code

Once the student becomes comfortable with reading and analyzing a program construct’s syntax, they are ready to write code for that construct. The best way to facilitate this learning is to provide the student with a clear description of the construct they need to code and have the student translate that description into a program.

An example from the paper by Xie, et.al. is taking a description of how to swap the values of two variables and turning that description into a program. Their description looks like this:

  1. Define variable x. Set it to 1.
  2. Define variable y. Set it to 2.
  3. Define variable temp and set it to x.
  4. Set x to y.
  5. Set y to temp.

The student who understands the basic syntax of writing assignment statements should be able to translate this description into a complete program, as shown below:

int x = 1;
int y = 2;
int temp = x;
x = y;
y = temp;

The problem with most programming instruction is that students are expected to be able to write code without the necessary practice in learning to read and understand code first.

Reading Templates

According to Xie, et.al., a template is a reusable abstraction of programming knowledge, and being able to read a template and know when and how to apply it to an appropriate problem is the next step in programming instruction.

The ability to read a template means the student can read the syntax of a code fragment and recognize that the code fragment implements a template, and the student also must know and understand the purpose of the code they are reading. So, when a student sees the variable swap code fragment shown above, they will recognize it and understand that it is the implementation of the variable swap template.

It’s important that before a student can successfully read templates they must be able to read code and be able to predict its behavior, which comes from lots of code reading and code writing. Not having this knowledge will lead to the student not understanding or incorrectly understanding what a code fragment does, leading them to misinterpret the template the code is implementing.

Writing Templates

The last step in programming instruction is for the student to be able to take a vague program description, determine the template needed to solve the problem, and being able to apply the template to that specific problem. The Xie paper demonstrates this with a word problem where a girl has two flashlights, one with more power than the other. The flashlight with more power breaks, however, so she needs to swap the batteries so that the working flashlight has the most power. The problem states that the amount of power in the flashlights is stored in the variables power1 and power2.

If the student has a strong knowledge of the previous three steps, developing a program to solve the problem above should be straightforward. However, if the student is deficient in any of the three steps, then attempting to move from a general template solution to a working program can lead to errors. If the student is deficient in code reading skills and/or code writing skills, they will likely make numerous syntax errors when trying to write the program. If the student is deficient in template reading skills, they will likely make logic errors when trying to apply the variable swap template to the flashlight battery problem.

Why Current Programming Instruction Doesn’t Work Well

The sequence of instructional steps developed by Xie and his co-authors demonstrate that when programming skills are properly sequenced, programming students learn more effectively and efficiently. The problem with most programming instruction today is these steps are either not followed or they are not followed with enough spacing between them so that students have time to get proficient in one step before moving on to the next step.

It would be easy to blame programming instructors for this problem, but to me, the real blame lies with the resources most instructors use for teaching programming – textbooks. I have written elsewhere on this blog about the problems with computer programming textbooks, but one problem is that these books do not follow this sequence of instruction, or at least they don’t follow the sequence with adequate spacing.

Programming constructs are introduced, two or three examples are given, and then students are asked to solve problems using the construct. Students are not usually asked to first demonstrate proficiency in being able to read and understand the construct’s syntax before writing some simple examples. And, very rarely are well-designed templates provided with programming constructs to help students solidify their knowledge of the construct.

A few textbooks provide some exercises in reading code at the end of a chapter before moving into the exercises to work. However, by placing these exercises at the end of the chapter, the student is not encouraged to work them first before moving on to more elaborate problems that require template application.

As this new theory of sequenced programming instruction becomes more widely known, I hope new textbooks and other learning resources are developed that follow this type of instruction.

If You are Learning Programming on Your Own

You can follow these sequences of topics if you are learning computer programming on your own and not in the classroom. To learn more about variable tracing, here is a great site from the University of Wisconsin’s Computer Science department: http://bit.ly/2OogxOg. There are many, many web sites devoted to teaching people how to write code. Many of them allow you to work in a web-based environment so you can get immediate feedback by running your code in the browser. Spend some time at w3schools.com or tutorialspoint.com to see if you like those sites, or just search for one more to your liking. As for working with templates, the paper by Xie, et.al. provides very good introductory information on using templates to teach computer programming. If you are very brave, the book Designing Pascal Solutions: A Case Study Approach, by Michael Clancy and Marcia Linn, provides many templates for introductory programmers. The problem is the book is written for the Pascal language, so it may be hard to follow if you don’t have any programming background. I will be writing more about using templates for learning computer programming on my blog in the future.

Learning to Program Can be Easier

Having taught computer programming for over twenty years now, I know that learning a programming language is a hard task for many students. However, by introducing programming topics in the right order, at the right time, and in the right dose, students will find programming a more pleasant topic to learn, and, hopefully, find useful for them regardless of what career they choose.