One of the traits that separate experts in a field from amateurs is the expert’s ability to call up structures from memory at a very fast rate. In programming, one set of structures all beginning programmers should memorize are the syntax templates of the major programming constructs for a programming language. Programming constructs can also be considered chunks. In this article I’ll discuss what syntax templates are, how and why they are considered chunks, and why chunking is important for developing programming expertise.
Syntax Templates
A syntax template is the formal definition of a programming construct. In this section we’ll look at several example templates covering the major fundamental programming constructs in several popular programming languages.
Syntax templates describe the grammar of a programming language. These templates are used as models of how to write the various programming constructs of a language. For example, in C++, here is the syntax template for declaring a variable:
data-type variable-name;
This template indicates that to declare a variable in C++, you start with the data type of the variable, follow that with a variable name, and end the statement with a semicolon. Applying this to a real program, a variable declaration could look like this:
string name;
Most programming languages allow you to declare a variable and assign it a value in the same statement. Here is that syntax template for JavaScript code:
const|var|let variable_name assignment-operator expression;
In this example, there are three possible ways to start a variable declaration: const, var, or let. The bar indicates that a variable declaration can use any one of these keywords. The assignment operator is the = character and the symbol could be used in the template, but I prefer the name, as this aids in helping the student memorize the template.
I have the hardest time explaining to beginning students what expression means in the template. They may have already seen that literals are often used in these types of statements and that doesn’t jibe with what they believe expressions are. I must convince them that literals are just expressions that evaluate to themselves. If the language has REPL shell, you can demonstrate how a literal evaluates to itself by simply typing a literal at the command prompt and showing how the return value is just the literal.
Assignment statements have their own syntax template. Here is the assignment statement syntax template for Python:
variable-name assignment-operator expression
if statements have a syntax template for each type of if: simple if, if-else, and if-else if. Here are the templates for if statements in Java (and C, C++, etc.):
if (relational expression)
{
statement(s);
}
if (relational expression)
{
statement(s);
}
else
{
statement(s);
}
if (relational expression)
{
statement(s);
}
else if (relational expression)
{
statement(s);
}
…
[else
{
statement(s);
}]
Notice that inside the blocks the “s” in statement(s) is in parentheses. This means that there can be one statement or multiple statements (we’ll ignore the fact that there can be no statements at all) in each of the blocks.
The hardest part of teaching these if templates to students is the if-else if statement. The ellipsis in the template indicates that there can be multiple else if blocks and the square brackets around the else block indicates that it is optional.
Also notice that my use of syntax templates puts curly braces around the different blocks even if there is only one statement in the block. This is becoming the more standard usage when writing blocks after relational expressions to cut down on errors.
Syntax templates for loops are written similarly. Here is the syntax template for a C++ while loop:
while (relational expression)
{
statement(s);
}
The syntax template for a while loop in Python, however, looks like this:
while relational-expression:
statement(s)
Similarly, the syntax template for for loops in languages that follow C-like syntax looks like this:
for(variable-init; relational expression; variable-mod)
{
statement(s);
}
The Python for loop looks quite a bit different. Here is the syntax template:
for iterator-var in sequence:
statement(s)
Function definitions have a syntax template as well. For C-like languages, the template looks like this:
return-type function-name(parameter list)
{
[statement(s);]
return expression;
}
Or like this for void functions:
void function-name(parameter list)
{
statement(s);
}
I have my students indicate in the template that the statements in a function body are optional, since it is possible to define a function that only has a return statement. This template also leaves out additional modifiers to member functions in C++, such as const. I do include this when I have my students learn the templates for member functions.
Why Memorizing Syntax Templates is Important – Chunking
One of the key techniques learning researchers have identified as necessary for expert performance is the use of chunks. So what is a chunk? Barbara Oakley, an engineering professor at Oakland University, in her book A Mind for Numbers: How to Excel at Math and Science, defines chunks as “pieces of information that are bound together through meaning.” In chess, chunks are patterns of board pieces that represent different stages of a chess game played by expert chess players. Chess grand masters store about fifty thousand chess game chunks in their minds and can recall them in seconds when playing a chess game.
Why are chunks so important? The ability to chunk individual concepts makes the expert that much quicker at solving problems. For the chess player, being able to recognize a pattern of chess pieces as a chunk quickly allows them to both decide their next move faster and allows them to analyze how that move will affect future moves in ways that just looking at the individual pieces of a chess board simply would not allow.
For learning programmers, it’s very important to learn how to recognize when a specific programming construct is needed and how to use that construct without having to go back to notes on how the construct is formed. The second part to chunking is learning how to solve a problem by applying different chunks (constructs) in the right place at the right time. Computer programs always consist of if statements here, function calls there, and loops in several places. The knowledge needed to write good computer programs is built from the ground up and the foundation of this knowledge are the fundamental programming constructs we’ve been talking about in this article.
How to Create Programming Construct Chunks
The first step in creating a chunk from a programming construct is to focus on learning the syntax template. Study it carefully, but not just in one long session but over a longer period, such as two or three times in a day, and every other day until you really know it. Spaced learning is more effective than concentrated learning. During this learning time, try writing out the syntax template from memory and then checking your answer against your notes or the textbook. Retrieval practice is much better than just reading over the template time after time.
The second step is be sure you understand what the programming construct is used for and how to apply it to specific problems. I tell my students to start by doing a variable trace over several example problems until they completely understand how the construct works.
Next, run several of the examples from a class lecture or the textbook to see what output the program returns. Modify the values of the variables in the program and try to predict what the output will be.
Once you’re comfortable with this step, the final step is to start trying to solve some problems that use the construct in order to understand the context in which the construct is used. It is great if you can start by working from pseudocode of the problem, either your own or pseudocode that is provided to you before you try to solve problems completely from scratch.
Another way to perform this step is to work from problem templates. There are several problem templates in computer programming, such as variable swapping, computing a running total, etc. These templates give you the context for solving a problem and allow you to apply the construct to the template. Then, once you’ve mastered using the template, you will start seeing how to apply it to unique problems.
With little differences here and there, these are the steps that all learners take as they become experts in their field.
Practice Makes Chunks
Your ability to become a great computer programmer depends on your ability to chunk the various programming constructs so that you can immediately recognize when a chunk is needed and how to apply that chunk to the problem at hand. Constructs become chunks through consistent practice over time. The first step is to memorize the syntax template of the construct. The second step is to understand how the construct works and the third step is to know how to use the construct and when the construct’s use is appropriate. The best way to create programming construct chunks is to practice them a little every day. As you learn a new construct, put it into your programmer’s toolbox and use it, along with the other constructs you already know, to solve new and harder problems. Doing this regularly puts you on the path to programming expertise.