A Strategy for Teaching Programming – Logic First

Programming is all about the creative logical thinking flow, and very little about the syntax. However, a lot of teaching materials take the reverse approach and teach the syntax of variables and if statements without giving them meaning.

In my experiences with teaching robotics students and adults learning programming, it’s much better to start with an interesting problem and break that down into code.

1. Get Them Excited About Programming

Start by getting them excited about the things this code will allow them to create. On the robotics team this is easy; what they learn will make different parts of the robot move and give them behavior. Talking about how programming—and this particular language—are used is a good way to engage students in the value of learning to code in that language.

Then, provide the students with a small sample problem. In the past, I’ve used a program that returns whether or not an input day of the week (i.e. “monday” or “fizzday”) is a valid day of the week.

logic-first03

2. Teach Students to Break Code Down

When I’m working as a developer and about to code a feature, I start with the whiteboard. This lets me break down the problem into pieces that I’m sure I know how to implement, from a high level down to a low level.

We should teach beginners to do the same. For a beginner, we need to break out our variables (possibly on a whiteboard) and define how we’re going to use them to get to the answer. This gives the beginner a chance to ask questions and start seeing the picture of how the code is going to work. This is also the time for the teacher to introduce features of the language that might be helpful—things like storing values in variables, using if statements, and using loops.

For the relevant day of the week problem, we can store the input day of the week and a list of all the days of the week. We can then use a loop to go through every day in our list, and see if any of them match the input day. If there’s a match, we can mark that it is valid, otherwise we can keep checking, and so on. It’s a simple problem, but it’s useful, can be solved a couple different ways and it takes a little bit of logical thinking to figure out.

3. Give them a Working Example

Providing the student with a piece of working code that they know how to run. For python, having python installed and a starter.py program ready to go means that a beginner can focus right away on the code to implement the problem. Ideally, the sample program should contain examples of all the types of statements that are needed to solve the problem, such as variable declaration and if- then- else statements.

The student can then copy the statements from the sample program and string them together to solve the problem at hand. From the examples provided and the pseudo coding up front, it should be pretty clear at this point exactly how the problem is solved.

At this point, students probably have a lot of questions about syntax. The example should be able to help them, and you as the instructor can jump in and start explaining what the rules are.

logic-first01

Why Logic-First Teaches More than Syntax-First

A syntax-first approach can be effective, especially for some types of students. However, I find that starting by teaching the problem solving side of coding rather than the syntax side of coding has lots of advantages:

  • It makes for a much more interesting and engaging lesson with a memorable ‘story’.
  • It more accurately portrays the process of solving coding challenges and therefore equips students to start problem solving on their own.
  • It teaches good methodology. I’ve seen firsthand as a professional developer that developers who start their programs on a whiteboard are much more successful at problem solving than ones that jump in and start with the code itself.
  • It show students that logic is the challenge you should be focusing on, and stressing out about the syntax isn’t worth the energy, especially since you can so easily figure it out from other code or looking it up.

In my experience, this strategy for teaching creates beginner programmers who can think about problem solving, and start implementing useful code on their own. It also encourages good questions about how to form computer logic, and deemphasizes the need for questions that are less interesting to beginners like “why do I need a semicolon anyway?”

What techniques have you found useful when teaching programming to beginners?