Today is a good day to code

Teaching my 7 Year Old Daughter to Code (Crypto)

Posted: August 24th, 2011 | Author: | Filed under: Lifestyle, Parenting, Programming, Ruby, Teaching Coding, Uncategorized | Tags: , , , , | 1 Comment »

When I started teaching programming to my children, I thought starting with JavaScript was a good idea. I still think that JavaScript is one of the most important languages to learn early in a programming career.  It just doesn’t seem to be the right choice for teaching someone to program when they are 7.

The reason is likely not what one would naturally think, the code isn’t too opaque, and the syntax wasn’t much of a problem for her.  It was just so much work to get output.  With my son, we worked on a really simple Python program working on his hand-me-down OLPC, my daughter was upgraded to an Acer Aspire 1 for her birthday.

With the Python, I felt like we made more progress due to the availability of a REPL.  We were able to make changes to the core code that was solving the problem and see results on the command line quickly.  With the JavaScript, we had to create an HTML page, load it into it, create some sort of markup output, etc… It just wasn’t as clean an approach to programming.

I have been asked by many about using “kid friendly” programming languages.  I think the people working on those are doing good work, however there is nothing “kid unfriendly” about the languages that I use for programming as an adult.  I think that in general when educating our kids, we need to stop coddling them so much.  Creating an approximation of an already dumbed down environment to write software to drive machines will not help them.  Most of the kids that I have seen are already beyond logo and they don’t even know it.  What they seem to want to do are real world things, and there is no reason they can’t.

What I settled on was to use Ruby for the tasks.  It is a language that has a great REPL, and is easier to read.  It also has the benefit of having a sane shell input mechanism as well as not requiring a ton of objects to get started.

We discussed what she wanted to do, there were several things, all of them were deeply social, but the one we settled on, I thought was the easiest to implement.  I thought that encrypting messages to her friend where only she and her friend had the crypt key would be enlightening.

She agreed, so we started into coding it up.  First we ended up working through a few encryption techniques on paper, taking short messages, converting them into their character codes and then shifting them by adding the char codes of each letter of the crypt key to each letter of the message.

def encode msg,key
	coded_msg = ""
	msg.each_char do |letter|
		coded_msg = coded_msg + add_cipher(key,letter)
	end
	return coded_msg
end

def add_cipher cipher,letter
	code = ""
	cipher.each_char do |cl|
		code = code + pad(cl[0] + letter[0])
	end
	return code
end

def pad num

   length=num.to_s.length

   if length > 3 then
      num.to_s
   else
      padded_id='0' * (3-length)  + num.to_s
   end
   return padded_id
end

At first, she put the crypt key into the program, but we discussed that it would be a bad idea since anyone with the source code could then crack the message.  She then asked me how her friend would decode the message.  I told her that the only way was for her to create a “pre-shared key,” something that she told her friend verbally that they would both have to remember, only then could that key be used to decrypt the messages.

What we did was to create a multi step command line program to accept the key and then the message.  We haven’t gotten around to the demux yet, but here is the mux:

print "Enter the crypto key, or die!: "
key = $stdin.gets.chomp

print "Tell me what to do ( 1 for encode, 2 for decode ): "
op = $stdin.gets.chomp

if(op.to_i == 1)
	print "Enter message to encode: "
	message = $stdin.gets.chomp
	coded_message = encode(message, key)
	print "Here is your message: \n"
	print coded_message
else

end

The nice thing about all of this is that the code is approachable, and the execution path makes sense… this happens, then this happens, etc… she can easily understand the flow of this program. We had significant problems with the flow of a client web application.

One of the first things that my daughter noticed was that whenever you make the crypt key longer, the message gets a bit longer, and that the encrypted message was many times longer than the original.  So it is working, not only is she understanding programming, but basic cryptography as well.  The only thing I am concerned about now is what happens when she is encrypting her posts on the social media site du jour at 16 with quantum encryption techniques.  How will I ever crack her codes?


  • Tim Kramer

    (heh) Concerning your last: it’ll happen much sooner than you think (or are ready for).