How I'd teach my granddad to program in C#
Michael Mugadza
Integration and Automation Specialist | .NET Developer | Azure Evangelist | I create tools that scale business processes.
This wouldn’t be easy my granddad has never turned on a computer or owned a smartphone. He thinks himself quite savvy for being able to text. This isn’t much of a setback however, he’s a willing learner and that’s all that really matters.
I could, like many textbooks begin with the theory of object oriented programming; explain inheritance, encapsulation, and polymorphism. I could go on to the specifics of C# syntax; declaring variables, writing loops, and maybe go as far as showing him cool features like LINQ. But I know this all wishful thinking, he’d be asleep before I explained what an object is. He’s a carpenter, a hands on type of man. He learns by doing and that’s how I’d teach him to program, by actually programming.
He loves playing games with us grand kids, on Sundays after church we often play card games, 30 seconds, and hangman. That’s where I’d start, I’d teach him how to create his own console hangman game in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hangman
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Hangman!!!!!!!!!!");
string[] listwords = new string[10];
listwords[0] = "brace";
listwords[1] = "crossgrain";
listwords[2] = "drill";
listwords[3] = "flitch";
listwords[4] = "grooving";
listwords[5] = "hardwood";
listwords[6] = "incannel";
listwords[7] = "jointer";
listwords[8] = "veiner";
listwords[9] = "warp";
Random randGen = new Random();
var idx = randGen.Next(0, 9);
string mysteryWord = listwords[idx];
char[] guess = new char[mysteryWord.Length];
Console.Write("Please enter your guess: ");
for (int p = 0; p < mysteryWord.Length; p++)
guess[p] = '*';
while (true)
{
char playerGuess = char.Parse(Console.ReadLine());
for (int j = 0; j < mysteryWord.Length; j++)
{
if (playerGuess == mysteryWord[j])
guess[j] = playerGuess;
}
Console.WriteLine(guess);
}
}
}
}
It's nothing big, just 46 lines, but with this he'll see that a namespace contains classes, how a method is called. We'd probably go over this code many times as I explain some of the specifics of C#, like arrays and loops. I'd let him tweak the code, simple changes at first. He could alter the size of the array and add more words, adjust the for loop to allow more guesses, whatever he fancies, I'll be there the whole time holding his hand as he explores.
After we've spent a couple of hours goofing around with the code. I'd delete it all, let go of his hand and ask him to write it all again. He'll stumble, miss-type some keyword, forget a semicolon, or incorrectly declare a variable. I'll give him time to try and figure it out, he's tenacious, he wouldn't give up at the first sign of trouble, but if he really get's stuck I'd help me out. Eventually he'll have the game up and running again. I'd delete the code again, have him recreate it again. This time he'd have to compile it on his own, the only clues he'll get will be from Visual Studio. No hand holding, no tips, heck at this point I won't be in the same room. When he's finally managed to do it all by himself that will be the end of the first day of programming. At this point he'd know enough to decide if he'll stick with programming.
The days that follow would be more of the same, each day I'll have a new project to work on. Each project teaching more about programming and C#. He'll learn to create and use types, manage program flow, debug applications, and implement data access. Somewhere along the line he'll realise he doesn't need me anymore, he'd just google what he needs to know, and he wouldn't be working on my boring project ideas. He'd be a programmer. Who knows what he'd create.