Cognitive scientists tell us that we learn and remember the things we think about. We need to make sure that our lessons get students to think about what we actually want them to learn. That’s why word searches have no educational value (other than for teaching students to spell) - they make students think about what the words look like, rather than what they mean.
I wouldn't, therefore, start students off with exercises on spotting syntax errors in existing code. It's not very exciting, and it also seems somewhat back-to-front – like getting primary-school children to proofread novels. More crucially, it’s getting students to think about the not-very-important issue of syntax (which they can look up if they need to) when they should be thinking about the programming techniques and the steps required to create the program.
With that in mind, I’m going to tell you about my top ten language-independent programming techniques – almost all will work in any language. I think they form a good basis for a KS3 programming course - you can add examples in your chosen language to illustrate the ideas, and maybe use the list as a checklist of things to try when approaching a new programming task.
Naturally, a list of the most fundamental techniques will contain a few really obvious ones, which I won’t need to describe in detail. I might expand on some of the more complex ideas in the coming months if I feel that there's a need.
You’re unlikely to be able to create a useful or interesting program that doesn’t store any information - you wouldn't want to play a game that couldn't give you a score, for example. Variables, therefore, are probably the most important thing in programming. The number of types, and whether you need to declare them varies from language to language, which is why I start with Just Basic (with just two types – numbers and strings) and progress to Visual Basic (with more types, but essentially the same commands).
This list isn't ordered, but I think that loops are probably the second-most important construct in programming.
A program that does the same thing every time wouldn’t be very engaging or flexible, so we need to get our programs to react to user input and other events. As well as the almost-universal if… then… else construct, most programming languages (but not Python) have some sort of case/select method for controlling program flow. You can also use array/list indexes as a form of selection (see below).
Arrays are useful for collections of like items, e.g. lottery balls, but using indexes as a form of selection or look-up is also a useful technique. For example, we can only create random numbers, so if we want a random thing, such as a day of the week, we can use the random number as the index in an array of strings containing the names of the days.
Python doesn’t have native support for arrays, but some of their functionality can be replicated using lists or tuples. Sparsely-populated arrays, or implementing a binary tree in an array, can be a bit messy, however, as are two-dimensional arrays.
5. Boolean Logic
Students need to learn about AND, OR, NOT, etc. for combining truth values, and how to create truth tables, for the theory paper, but Boolean operators are used all of the time in programming.
6. Bitwise Logic
Using bitwise logic to set/unset and mask bits was something of a programming staple when I was a teenager and memory was at a premium. It could be used to combine multiple values into binary flags to save memory and to mask bits to read them back again.
Dijkstra predicted (in 1979) that “the advent of cheap and powerful devices will set computer science back 25 years”. The need to save memory has largely passed, but this is still a useful technique for combining multiple values to pass between functions as a single argument, or to pass variable numbers of values between web-pages in a cookie or querystring. You can also use it as a quick and simple way to convert between denary and binary, or to encipher text like a Lorenz machine using bitwise EOR.
7. Modular Arithmetic
This was the subject of a previous blog, but, briefly, modular arithmetic is dividing and looking at the remainder. It’s a good way of limiting the number of outputs of a program or function or of getting things to “wrap around”, e.g. getting angles to go from 359 back to 0. It’s so simple and so useful a technique that I can’t think why you wouldn’t include it in any KS3 programming course.
8. Manipulating Text
I’ve cheated here slightly by combining the manipulation of characters and strings. They’re really different things, but I wanted to sneak in trigonometry at number ten!
The internal representation of data is the topic that links Computing together, so students should already be aware that text is stored as numbers. Being able to convert characters to ASCII and vice versa is a useful technique for a variety of reasons - you can use it to check for upper/lower case, for example, or non-alphabetic characters, and you can also use ASCII codes to create Caesar shift ciphers or Lorenz-style ciphers using bitwise EOR.
Chopping up strings, e.g. using left() and right() in BASIC, or string-slicing in Python, is also useful for a variety of tasks from displaying initials (given a name) to creating anagrams. Displaying text in different forms, e.g. changing case for use in headings, is also something that you might want to do regularly to improve the appearance of your program (I find that you can often spot a confident programmer because they go beyond the coding and refine the user interface of their program).
9. Random Numbers and Scaling
Some languages, such as Python, have library functions to create random integers (i.e. randint()), but you might be using a language that doesn’t, or you might not want an integer, so it’s useful to learn how to scale random numbers yourself. In fact, scaling numbers generally – e.g. for fitting things on a screen – is a useful skill in itself. I demonstrate that technique in my Scratch polygon program to ensure that the polygon is always approximately the same size regardless of the number of sides.
You might want a random number for its own sake, e.g. to simulate a die roll, or to use as an array/list index to pick a random thing, but you can also add a degree of randomness to things to make them look more “natural”. For example, I used recursion to make a tree in Scratch, but it didn’t look like a real tree. Adding a small amount of randomness creates a more natural-looking result.
Python’s randint() function and the scaling method I describe create pseudorandom numbers that are uniformly distributed throughout the chosen range. You might not always want that to be the case. On my angles page, for example, I wanted smaller angles to be chosen more often than larger ones, because most angles that students encounter are less than 180º.
The solution is surprisingly simple; because the random function in most languages generates a number between 0 and 1, if you raise that number to any power it stays between 0 and 1, but the distribution changes. If you square the number, for example, 0 stays as 0 and 1 remains 1, but 0.5 becomes 0.25 – i.e. the distribution is skewed towards the lower numbers. You need to do that before scaling. If you want to skew the distribution upwards, simply subtract the result from 1.
I almost went with “Maths” for this last point, because understanding mechanics topics, such as conservation of momentum or Newton’s equations of motion, can be useful, especially when creating games or animation. “Maths” might be a bit broad, though, so I’ve chosen trigonometry because, although I don’t use it every day, it is the more advanced mathematical technique that I use most often.
Sines and cosines are useful when drawing circles, creating circular motion, laying out items on apage, creating patterns, and also for working out the angles of lines and direction of motion. As the trigonometric functions are computationally quite complex, they can also be used in discussions of programming efficiency.
You might disagree with some of the techniques I’ve included in my list, or you might have others that you’d want to include (functions and procedures are important, for example, but I didn’t include them because you need to first have some techniques to use in those functions and procedures), so why use your Computing forum of choice to debate the topic?
Repl.it didn't exist when I wrote this article, but there is now a page of Python programming examples grouped by the programming techniques that they use.