An Approach to Motivating Coding
One of the many challenges when teaching programming to adults is to find motivating examples. There’s not a lot people do in real life that you can do in five or ten lines of Python, and the things you can do (like Scratch-style graphics) aren’t things most people with jobs and families have time to care about.
Data analysis is an exception. I’m increasingly convinced that analytics-first is the best way to convince most adults that lessons will pay off, but R and Python both have unfortunate quirks. Tidyblocks was an attempt to build something Scratch-like as a bridge between spreadsheets and textual coding, but foundered on the inability of click-together blocks to express joins (and on the fact that nobody would fund further work). I’m now wondering whether some combination of click-together blocks and block-and-connector dataflow would work better, and whether we could use the question, “How does a spreadsheet work?” as a running example. A possible teaching sequence would be:
-
A single input cell and a single output cell
with a block in the middle that does
out := 2 * in
. - Three such cell pairs, each with the same formula. clearly we don't want to have to do this N times.
-
Two columns of three cells each to teach
in[i]
andout[i]
, with subscripting represented as two-blocks (name and index expression). -
The same input and output columns but a loop block:
for i in 1..length(in)
.
- Attach a simple inspector showing (i, val) as an (x, y) bar plot to visualize while single-stepping.
-
Introduce a conditional block:
for... if in[i] >= 0
, then point out that holes (NA) appear in the output. (The watch plot shows special markers for missing values.) -
Introduce solution #1: the
else
clause (and watch what it does by single-stepping). -
Introduce solution #2:
for... if... append(out, value)
and show the output column growing. The final output column is a different length than the input. - Next, add two columns of the same length.
-
And then ask what happens if the input columns have different lengths? Options are:
- Fail with an out-of-bounds error.
- Use the shorter length.
- Append NAs to the output.
-
We can now do things like sum the values in a column.
- Create a scalar variable (visually like a column, but indexing not required).
- Add values in column into that scalar.
- Attach a watch plot that visualizes changes to the scalar's value.
I know this isn’t how spreadsheets actually work (mumble mumble observer/observable, mumble mumble), but what I don’t know is if anyone has ever used calculations on columns to motivate basic programming for adult learners? I think it would be easy to extend this: for example, the next step could be “how to create a function that operates on an arbitrary column”, which opens up all sorts of interesting learning possibilities. If anyone has done this, I’d be very grateful for pointers.