Programming is not Algebra

A friend discovered a small interest in learning to program computers. She tried an online course but she didn’t get far before losing interest. She may have had other reasons for giving it up but by watching her and asking questions I did locate one significant hurdle: she was trying to read code through the lens of algebra.

I have seen this before. A student encounters a word, symbol, or pattern they recognize from some other context. Believing they have sufficient information to understand it in the new context, they push ahead. Soon their mental model fills with contradictions and the entire subject becomes intractable. This state of affairs causes people to quit their studies.

I’ve been there. Fortunately I learned the solution:

  1. Find what word or symbol was misunderstood.
  2. Learn the correct meaning.
  3. Re-study the material from the first occurrence.

The hardest part is completing the first step. This is especially true of students who already know a great deal and who are confident in their knowledge. Preconceptions hide themselves well but they surrender easily once found. Most people will be eager to complete the remaining steps after they have identified a real misunderstanding.

In this article I will expose some common preconceptions which can cause students of programming to quit. Teachers of beginner programming classes and writers of books would do well to treat these handicaps before setting students to code.

Remembering Algebra

The root of the problem is that programming uses many of the same symbols used by other disciplines. Some of the basic features of programming which are deceptively similar in algebra are variables, the equals sign =, functions, and lists of statements.

In elementary algebra a variable may refer to a quantity which is known or unknown. The context determines the type and meaning of each variable. It could be independent or dependent, variable or constant, free or bound, or even indeterminate.

Equations are similarly diverse in algebra. They are used express:

  • laws: a + b = b + a
  • functions: f(x, y) = 2x + 5y
  • and systems that may or may not have solutions:
    \left\{ {x+y=1\atop 0x+0y=2}\right.

Most adults who attempt to learn programming will have taken an elementary algebra class where they learned to substitute letters for numbers and to follow strict rules for manipulating equations. If they try to understand code through the lens of algebra they are in for a headache.

Programming is not like Algebra

Code can look like algebra problems. This is especially common in beginner-level examples. While a technological polyglot can see familiar symbols in fixed-width fonts and mentally execute the code, a person new to the field might attempt to understand it by applying the most similar subject in their repertoire: algebra. If you are teaching or writing about programming for beginners you should learn to anticipate and identify this mistake.

An exercise in role playing will illustrate this point. If you are a coder, try to forget what you know and read the following code as if its only parallel in your memory is your algebra lessons.

Sample Code

a = 10;
b = 20;
a = b;

// What are the values of a and b?

“Um… 10 and 20… but then how can a be equal to b? Programming is not for me.”

This is an actual response from my beginner friend. She had not progressed with her online course so I showed her the example above (from Programming Sheep) as a diagnostic.

Instead of a program, she saw three statements of equality and assumed they were a system of simultaneous equations. These preconceptions could have been handled by a simple introductory lesson defining the words “program” and “variable” and the symbol “=”.

The Simple Things

People who have been coding for a long time forget that their variables and equals signs are incompatible with algebra. My friend might have passed the sheep test if her “beginner course” had taught her only these basic definitions:

not like algebra
a sequence of instructions to be executed in order
a name which can be assigned a value
Assignment (a = b)
an instruction to assign to the variable a the value of b

Instead the course focused on instant gratification through immediately visible results. Lesson one launched a code editor and made the student call functions to display a message on the screen. She complied but did not understand. And with that, she was gone.

Goodbye, Students

Hello World is no basis for teaching programming to beginners! By assuming the student knows what programming is and how assignment works, the instructor relies on the student’s intuitive ability to induce the basic definitions for themselves. That way lies failure for many students because they are handicapped by earlier studies such as algebra.

It could be argued that an initially steep learning curve is good for the discipline because it filters out people who are not sufficiently clever or driven. I would counter that any teacher who intentionally leaves these pitfalls in place is not sufficiently clever or driven.

Sadly, academia and industry accept a high drop-out rate as if it were a natural consequence of the subject matter or some natural characteristic of the failing student. This acceptance is wrong. The drop-out rate is just as much a consequence of the teaching. Teachers who fail that many students have failed themselves.

Making it Better

Maybe intuition was how most professionals got over these first hurdles. Because we were never taught the basics, we don’t know how to teach them. Worse, we don’t even know that they must be taught.

If you write or teach a beginning programming course, or anything course, please invest some time into defining the basic terms. And if you find your students failing, please try to find out what word or symbol they did not correctly understand.

Cues from Knuth

In The Art of Computer Programming, Volume 1, Donald Knuth presents a most effective way to teach the meaning of programs, variables, assignment and control structures. On pages xiixiv the reader is instructed to study TAOCP by faithfully executing a program. The program is presented as both a flow chart and a list of written instructions:

3. Set N equal to 1.
4. Begin reading chapter N. …
16. Increase N by one. …
17. If N is less than 12, go back to step 4.
18. Congratulations. Now try to get your friends to purchase a copy of Volume 1 and to start reading it. Also, go back to step 3.

The student is made to emulate a computer even before the first chapter can begin. Expert programmers would agree that one of their most indispensable traits is their ability to mentally trace the execution of their programs–to emulate a computer. It makes sense to cultivate that ability as early as possible. If you are planning a lesson in programming for beginners, you could hardly do better than to follow Knuth’s example.