Live Coding
Time to get back to those teaching techniques Alfred Thompson went over at his CSTA-NY talk a few weeks ago.
This time, live coding.
Live coding, is well, coding live - writing programs live in front of a studio audience - that is, your class. It's always been on of my go to techniques.
Of course, it isn't quite that simple.
You can't just sit at your desk and start writing programs. That's a great way to lose your class in a hurry.
You've got to set the stage and then as you type you've got to talk. Not dictate your code, or at least not entirely but rather, describe what you're doing.
Let's look at the following code - part 1 from today's Advent of Code competition in Python. I used a couple of loops instead of list comprehensions for the sake of the live coding discussion.
1data = []
2for item in sample.split():
3 data.append(int(item))
4
5freqs = {}
6for item in data:
7 if item in freqs:
8 freqs[item] = freqs[item]+1
9 else:
10 freqs[item] = 1
11
12vals = sorted(freqs.values())
13min_value=vals[-3:-2][0]
14
15prod = 1
16for k,v in freqs.items():
17 if v >= min_value:
18 prod = prod * k
19print(prod)When live coding lines 1 through 3 which parses the input (stored in sample but omitted above for the sake of brevity) I might say something like this (bold indicates a stage direction):
We have to get the data data into a list of integers. It's being given to us as text - a string with spaces separating each element.
We can use the split() method on sample (as I type sample.split()) to convert or split the string into a list where each item is a string representing an integer. We can then loop over the items one at a time (as I type the rest of line 2 around it)
In the loop we can convert each item to an int and add it to the end of our list, data (as I type line 3)
Of course you might narrate differently depending on our style, your class and other factors.
It's also critical that you go slow. For CS0 or CS1 classes, even much of CS2 it's pretty easy for an experienced teacher to just type off a standard solution, particularly if they've been teaching a topic for years. Students can't read and process that fast and they type and handwrite even slower. When live coding, you've got to slow it down. Type slowly, one concept at a time, and pause frequently.
I also compile and run my code frequently through this process. Usually testing every concept. By concept I mean an if statement, loop, or similar. When I start live coding, I say as I do it that I'm going to quickly test what I wrote but later on I just do it.
The kids see me running and testing constantly. I say I do it per "concept" becuase that's the smallest code unit where an error might crop up and it's certainly easier to find an error in 2 or 3 new lines of code than say 20 or more if you're writing full functions or more between testing.
So that's the basic idea but there are variations.
As you're live coding, you an ask questions about the code or ellicit code from the class. You can have the students make predictions on the code, or suggest alternatives. Sky's the limit.
You can also do live coding from subgoal labels or even develop the labels in a live coding session, then either live code fleshing them out or set the class loose as a lab.
Lots and lots of variations.
There are also some concerns to be aware of.
If you are truly live coding, you're being authentic bug you're also taking a risk. You might get the code wrong - particularly if you're live coding something complex.
Getting something wrong of course can be a good thing - you're modelling real software development - bugs happen, mistakes happen and you can model or involve the class in solving the problem.
On the other hand, you can end up live coding yourself into a corner, nobody can find the error and the end of class clock is ticking.
You also can lose the authenticity if you're teaching the same class multiple times as you are likely to do in K12, all in one day adn maybe over a multiple days in college.
If an error cropped up in period 1 what do you do in period 2? Fix it, try to make the error seem authentic the second time? Code it correctly but interject that there was an error in the last period and look at the error? All possible approaches.
Same thing with jokes, side stories and insights that came up naturally during a live coding session earlier in the day.
Now, to cut down on the errors you can prewrite the code but don't just copy it. It's an emergency backup. Also, be careful about cut and pasting from your solution (as opposed to cut and pasting code you get from say a web site). Students observing code that's cut and pasted is different than from when they see code typed a character at a time. Just like seeing a slide and possibly taking its picture engages students differently than writing by hand on the board and having students copy notes either verbatim or by summarizing.
As a final note, I'll also say that live coding is a good model for getting students to talk about their code and talk through attempted solutions - good for technical interviews and communication in general.
Live coding is one of my favorite techniques. It's fun and has lots of variants and truth be told, requires relatively little prep or overhead. If it's not in your teaching arsenal, it should be.