How to Learn to Program: Writing Code

Photo by Christopher Robin Ebbinghaus on Unsplash

You can’t write code before you can read code

The four steps in a sequenced instructional approach to teaching programming are: 1) reading and understanding code; 2) writing code; 3) reading and understanding program templates; and 4) applying program templates to unique situations. I discussed the first step in the process in a previous article and in this article I dive into the second step: writing code from simple, clear instructions.

The Transfer from Reading Code to Writing Code

A mistake that many programming and textbook authors make when teaching beginning programmers is having the learner start writing code too soon. The beginning programmer needs to spend a lot of time reading code and learning to be able to predict the behavior of a sequence of code before they see it executed.

Learning how to read code should be broken down by programming construct, such as if statements and different types of loops. Before a learner can be expected to write an effective while loop, for example, they should have practiced reading while loops and tracing the code many times in many different situations, such as count-controlled loops and sentinel-controlled loops. It is only after having practiced on loops by tracing multiple times will the learner have enough experience understanding the behavior of loops to be able to start writing their own, effective loops.

It is when the learner starts to write code too soon that they experience errors in their code. They will receive syntax errors if they haven’t read enough code in a programming construct to understand how the construct should be formed. Then, if they aren’t experienced with predicting the behavior of a programming construct through code reading, they will have logic errors in their code when trying to write programs using that construct.

Learning to Write Code

At this stage of a learner’s development, learning to write code involves taking clear, simple instructions and translating them into a program.

Here’s one example of clear, simple instructions involving variable swap:

  • Declare x and y as variables and set them to the values 1 and 2, respectively.
  • Declare temp as a third variable and set its value to x.
  • Set x’s value to y.
  • Set y’s value to temp.
  • Display the new values of x and y.

The learning programmer should be able to translate these instructions into a working program. Here is the translation into Python:

x = 1
y = 2
temp = x
x = y
y = temp

Here is another example to use when learning the if-else construct:

  • Prompt the user to enter the cost of a soda.
  • If the cost of the soda is less than or equal to $2, then recommend buying the soda.
  • If the cost of the soda is greater than $2, then recommend not buying the soda.

Here is the translation in JavaScript (using the Spidermonkey shell):

putstr("Enter the price of a soda: ");
let price = parseFloat(readline());
if (price <= 2.00) {
  print("You can buy the soda.");
}
else {
  print("Do not buy the soda.");
}

Commenting Code for Understanding

Though there is some dispute among programmers as to when and how much to comment code, the learning programmer should be writing comments often. They should do this to demonstrate their understanding of what they are writing.

Each line of code should have a comment that expresses what the programmers thinks that line of code should be doing. Here’s an example of how a learner should be using comments in a program that utilizes a while loop to sum numbers entered by the user:

# set accumulator to zero
total = 0
# counter to control loop starts at 1
count = 1
# loop condition tells how many times to iterate
while count <= 10:
  # prompt the user to enter a number
  print("Enter a number:", end=" ")
  # get number as string from user
  number = input()
  # convert number to integer

  number = int(number)
  # add number to the total
  total = total + number
  # increment the count by 1 so loop will stop
  count = count + 1
# display the total
print(total)

Of course, the experienced programmer would never put such obvious comments in their programs, but learning programmers need to make their intentions explicit, both for themselves and for whoever is giving them feedback.

Even though comments can be placed at the end of a statement, I prefer learners put the comments before the line. This forces them to state what the next statement is going to do and then they can write that statement.

Eventually, as learning programmers become more experienced, I allow them to drop the comments from their code for constructs they have demonstrated proficiency with. When they learn a new construct, however, their first coding attempts must include comments for each statement.

Comments are Important for Understanding Errors

I have my students write comments before every line so that I can check their intention. This way, I can more easily decide if an error is caused by a misuse or misunderstanding of syntax or a misuse or misunderstanding of program logic.

Another way to state this is to say that comments allow me to quickly figure out if a student understands what to use a programming construct for and/or if a student doesn’t understand how to properly construct that programming construct. When this is the case, I take the student back to the first step and have them do more variable and program tracing until they feel confident they understand how that construct works.

But the comments aren’t just for the teacher. As the student is beginning to learn how to write code, writing the comment first allows them to think through what they are going to do and make sure they understand the problem they are trying to solve.

Memorizing Syntax Templates is Important

All through the code reading phase and into the code writing phase, if necessary, I have my students learn and memorize the syntax template for each construct they are learning. This is important because while they are writing code, if they have the syntax template for a construct memorized, they already know how to form the construct syntactically and they can concentrate on using it to solve a problem. This reduces cognitive load and it also lessens the chances that the student will make many syntax errors while moving through the code writing phase.

I have written separately about syntax templates, but here are some examples for a few popular languages:

Variable declaration – Java:

data-type variable-name;

Assignment statement – C#:

variable-name assignment-operator expression;

for loop – C++:

for (init-variable; condition; variable-mod)
{
  statements;
}

while loop – Python:

while (condition):
  statements

function (traditional) – JavaScript:

function function-name (parameter list)
{
  statements;
  return statement;
}

When these templates are memorized, the student can concentrate on using them rather than having to recall how they are formed. I quiz my students daily on syntax templates at the end of class, just like my junior high civics teacher used to quiz us on state capitols at the end of every class.

Code Writing is Just Step 2 in the Process

As important as learning to write code is for any beginning programmer, it is just a second step in a four-step process. First, the learning programmer needs to know how to read and interpret (in the non-technical sense) code. Next, he or she needs to learn how to write code from clear, precise instructions. The third step, and the next article in this series, is to be able to apply a template of common code use for the construct they are learning. And finally, the learning programmer needs to be able to apply these templates to unique problems.

Once the student reaches this fourth stage, they are ready to move into more advanced programming topics such as algorithms, data structures, and object-oriented programming.