Read before you write

By Will Grey. Posted

It’s important to give students the ability to read and understand code before they start writing it

Will Grey shares some evidence-based approaches for helping pupils to read code

Before students can write code, they need to be able to read code. Computer science pedagogy is often based around the ideas of Piaget’s constructivism – where pupils develop their knowledge through exploration, and Papert’s constructionism – where pupils learn through creating artefacts. However, learners need guidance to gain useful knowledge efficiently, and to organise that knowledge in a clear and logical way. They need to be able to break a problem down, remove the unnecessary detail, find patterns, and think algorithmically before they can start to write programs for solving problems.

Just as we wouldn’t expect a young child to write prose before they can read, we need to provide guided approaches that use direct instruction and scaffolding to help our students read code before they can be expected to write code themselves. These guided approaches are needed just as much as, if not more than, creative discovery activities.

Explain the code

My first approach to improving code comprehension is to ask my pupils to ‘explain in plain English’ what a piece of code does. There are many variations on this activity, but I find that this works well when pupils explain to each other what the code does. That way, pupils have valuable contributions from others that they can then incorporate into their own written explanations. 

We could take this further and ask students to carry out various explorations of the code. For instance, pupils could annotate or add comments to code, list and explain the purpose of the variables and functions, and create structure diagrams. I particularly enjoy doing this with the AQA A-level pre-release material – a reasonably substantial piece of code that enables a rich investigatory experience.

Worked examples

Another approach is to use worked examples. They can be delivered by modelling with live coding, or using tutorial guides. During a live coding demonstration, the teacher explains each step of the code as it is being written. This is beneficial because students can see how teachers tackle problems. Pupils also see that making coding errors is a normal part of the process. Another benefit of live coding is that the pace of delivery is generally slow and this may help some groups of pupils – I have found it particularly helpful with underachieving boys. 

Nevertheless, demonstrations need to be kept short and only progress a few steps at a time, so as not to overload working memory. Lessons should have several shorter demonstrations spaced throughout, rather than fewer longer demonstrations. Perhaps live coding demonstrations could be pre-recorded as screencasts with audio and made available to pupils to watch, pause, and replay in their own time, thereby helping pupils who did not follow the first time round. I tend to couple the demonstrations with tutorial guides because it gives my pupils greater autonomy. I have used worked examples across the age ranges to deliver lessons on Scratch to Year 7 pupils, all the way through to advanced concepts in Python like server-side scripting with sixth-form students. 

Sub-goals

The third approach is to group individual steps under meaningful sub-goal labels. In addition to helping students organise and structure the code in a coherent manner, this reduces the load on working memory because individual steps are chunked together. Pupils could be asked to do this themselves, or sub-goal labels could be provided by the teacher, according the specific needs of the student.

Trace the code

The fourth approach is for pupils to trace code by hand to simulate the outcome of each instruction in an algorithm. This is where the student replicates the task of the computer by keeping track and recording the values of the variables at each step, and performing the various arithmetic and logical operations. Pupils find tracing code tricky as it requires considerable patience and concentration to not lose their train-of-thought. It takes practice to master, especially when tracing recursive algorithms like merge-sort. I tend to practise these regularly with my GCSE and A level groups – both with the familiar algorithms for searching, sorting, and traversing, and with algorithms that they have not seen before. There are plenty of past paper questions across the exam boards that can be used support this activity.

Parson’s puzzles

Parson’s puzzles bridge the gap between reading and writing code. Here, code is presented to pupils jumbled up. The task is to put the instructions into the correct order so that the whole code performs a predefined task. Students do not need to be concerned with remembering syntax, and can focus on logic and sequencing. They make a lovely starter activity, and can come in a variety of forms, from those on a computer, to paper-based and jigsaw-type puzzles with snippets of code that can be pieced together, but this does require quite a bit of preparation.

An example of a Parson’s puzzle: can you draw a square?

Putting it all together

Once pupils have gained comprehension of the various programming constructs and seen them applied in different contexts, they will then be able to write better code. There are many scaffolded techniques that can help students write code by taking away the challenges of code design and abstraction.  

These ideas for reading code are generally very simple to implement, and can be used together and alongside methods for writing code and more general teaching pedagogies, such as flipped learning, direct instruction, questioning, and verbal feedback, to deliver effective lessons on coding in visual and textual languages to schoolchildren of all ages.

Further reading


https://helloworld.cc/grey

Print

Free - UK only

If you’re a UK-based teacher, volunteer, librarian or something in between, we'll send each issue free to your door.

Digital

Free

Just want to read the free PDF? Get each new issue delivered straight to your inbox. No fuss and no spam.

Buy

From £6

If you are UK-based but not involved in education, you can get hard copies by buying back issues.