First Coding Lesson to my Son: Coding Saves Time

must-notFirst of all – it’s been a while since I’ve posted here.  The craziness of the fall, combined with some (what I feel was) much-overdue vacation, has dropped of my posting frequency.  I’m hoping to remedy this going forward!

So to jump back into things, I want to share a short, at home anecdote about the value of coding.  I wanted to show my son (9 years old) how coding can add efficiency to repetitive processes. 

To start, I told him to pretend he was in trouble (not a stretch for him..) for hitting his little brother.  I then told him that as his punishment he had to write “I will not hit my brother” 25 times on a piece of paper.  It took him about 10 minutes to do that.

During that time, I wrote this uber-basic method:

public static string WriteOnChalkboard(string WillNotWhat, int HowManyTimes)
{
var sb = new StringBuilder();
for (int i = 0; i < HowManyTimes; i++)
{
sb.AppendLine(string.Format("I will not {0}", WillNotWhat));
}
return sb.ToString();
}

 

I showed it to him, and then ran it from a console app.

static void Main(string[] args)
{
string ChalkboardText = WriteOnChalkboard("hit my little brother", 25);
System.Console.Write(ChalkboardText);

            System.Console.ReadLine();
}

        public static string WriteOnChalkboard(string WillNotWhat, int HowManyTimes)
{
var sb = new StringBuilder();
for (int i = 0; i < HowManyTimes; i++)
{
sb.AppendLine(string.Format("I will not {0}.", WillNotWhat));
}
return sb.ToString();
}

Maybe not the best life example since this scenario shows him how to “cheat” a punishment, but he got the point. ;)