Writing code in any language requires a set of common skills: commands, variables, arrays, loops, and logic. These skills can be taught without a computer. Once these skills have been introduced and learners have practised them, a programming language can be chosen and the learners can learn that language’s syntax. I am assuming that many of these skills will be included in the **Coding & Robotics Curriculum** that is currently being piloted in South Africa (see the draft CAPS documents here).

In this post:

- The Process
- 1st Iteration:
*SquareMaker* - 2nd Iteration: the
*SquareMaker*refactored - 1st Iteration:
*TriangleMaker* - 2nd Iteration:
*TriangleMaker* *1st Iteration: ShapeMaker*- LOGO Basic

## The Process

Writing code, programming, software development – whatever you chose to call it – is a process. In this article, you will see that I have broken the exercise down into sections.

## The Specification

We are going to write a set of instructions — a program — that can draw a simple, regular shape.

### Nominate a PCD

Choose a learner who will act as the class’s Programmable Computer Device (“PCD”) and then challenge the class to create a set of instructions that will result in their PCD walking in a simple, regular shape. Let them shout out instructions, while you write down the steps on the board.

Re-writing code to improve it without changing its function is called “refactoring”.

## 1st Iteration: *SquareMaker*

The first shape is a square. Ask the learners to describe a square to you — after all: how can you write a program to draw a square if you cannot accurately define one. You want them to list define it as a shape with four sides of equal length and four 90 angles.

Once they understand exactly what they are drawing, guide the learners towards something like the following set of instructions (written on a whiteboard) for their PCD:

walk forward 10 steps turn right 90° walk forward 10 steps turn right 90° walk forward 10 steps turn right 90° walk forward 10 steps turn right 90°

## 2nd Iteration: the *SquareMaker *refactored

Ask the learners if they see lots of repetition. Guide them towards seeing that the above 8 instructions are really the repetition of the first 2 steps, 4 times in total. The above “program” can therefore be reduced to something like:

Repeat 4 times: walk forward 10 steps turn right 90°

It is not that the 1st Iteration is incorrect, but rather that the 2nd Iteration is better, more *elegant*.

## 1st Iteration: *TriangleMaker*

Using the strategy employed in our SquareMaker program, we are now going to create a program to draw an equilateral triangle (three sides of equal length). The square was probably a bit easier to define, so draw a circle on the board as follows so that they can understand how (and eventually why) we are using 360 degrees/a circle for defining turns:

Try not to give the game away when you explain that now the PCD must turn 120° at each corner — the learners are going to want to tell you that the PCD must turn 60° each time. Let them try that so that they can see that the PCD does not land up on the spot that they started from (which is obviously a requirement of drawing these simple shapes). We want the learners to realise that the total of the turns must equal 360° each time for this to happen.

Repeat the 1st Iteration, this time the objective is to draw a triangle:

walk forward 10 steps turn right 120° walk forward 10 steps turn right 120° walk forward 10 steps turn right 120°

## 2nd Iteration: *TriangleMaker*

Again: ask the learners what patterns they see. You should end up with:

Repeat 3 times: walk forward 10 steps turn right 120°

## 1st Iteration: *ShapeMaker*

You now have the 2nd iteration of your two programmes which should look something like this:

Repeat 4 times: walk forward 10 steps turn right 90° Repeat 3 times: walk forward 10 steps turn right 120°

### Abstraction

If they look at the numbers they should realise that the square (4 sides) has 4 90° turns and a triangle (3 sides) has 3 120° turns. If they can see the above pseudo-code and the circle diagram they should easily now see that they keep coming to a total of 360° for the turns made (they may need some “prompting”!).

In our next iteration, we are going to abstract the *fixed values* and replace them with *variables*.

if x = 4 Repeat x times: walk forward 10 steps turn right 360°/x if x = 3 Repeat x times: walk forward 10 steps turn right 360°/x

Looking at the above the learners should quickly see that they can create a new programme called *ShapeMaker*, looking something like this:

Repeat x times: walk forward 10 steps turn right 360°/x

## LOGO Basic

Now we need to choose a language in which to write our program. Each language has its own syntax so the next step will be to write our program in that language.

For this exercise, we will use **LOGO Basic**, a language that has been around for many, many years and was created exactly for our purpose: learning how to program! Head on over to Learning to program with LOGO for the next steps!