Instructional Methods for In-class Code Demos When demoing code in class, I have tried at least three different methods of instruction:


Write code live and have students type along
Write code live and have students follow my logic while only I type and narrate my thinking
Start with a full working demo and analyze an already written program


In all circumstances students can access the programs on GitHub after class. Moving forward, I would like to develop a more consistent and effective approach. 

What instructional methods are most effective for teaching with sample programs?
I think this depends on the curriculum and goal. If the goal is not to teach them specific algorithms, but to teach them problem solving, then have them write the code first. I come from adult education, and this approach works fine there for me.

What I do is I give gradually more complex exercises, and give them time to develop a solution. I also give them a few keywords, commands that they will likely need. I go around and help with individual solutions. They are allowed to work together, communicate, help each other if they want to. They can use the help, internet, the course book, whatever they can get their hands on.  

This means a lot more work for the teacher, as you have to run around, understand different approaches, and not force your solution on them.

If I find a particularly good or interesting solution, I show it to the class (after everyone has finished).

I only solve the program in front of the class once everyone has a solution. (Pretty disappointing, but even this way I see some of them just delete their working solution and mindlessly copy mine.) I always try to emphasize there is no one good solution. There might be better, faster solutions, but as long as theirs is correct and works, it is a good solution.

As for the inevitable speed differences in the group, instead of giving one exercise, I give 3-4. The first is the mandatory, the rest is extra. (Maybe for + points or such to motivate them; luckily in adult education I don't need the extra external motivation.) When the last person finishes (or gives up) the main exercise, that is when time is up.

So I say the most effective way for teaching with sample programs is let them try, fail, suffer, find their own way, then finally succeed. Don't spoonfeed them.