my faceChris Foley is a computer programming enthusiast. He loves exploring new programming languages and crafting nice solutions. He lives in Glasgow, Scotland and works as a software developer.

Misplaced Advice

I was doing some marking in the first year teaching labs and there was an incident that made me think hard about the way I give advice. On this particular day the students had to write a method to add up all the numbers in an array. Here was one solution:

public int sum(int[] arr)
	if (arr.length == 0)
		return 0;
		int sum = 0;
		for(int i = 0; i < arr.length; i++)
			sum = sum + arr[i];
		return sum;

There's a lot of unnecessary stuff going on there so I told him that he didn't need the if statement at all and that the zero case would be handled by the termination condition in the loop. I was quite proud of myself ...

... but later I realised that the student had correctly identified the base case for recursion. I was focussing on one aspect of the skill to the detriment of the wider picture. I'm sure he would have discovered my trivial optimisation eventually but many students struggle with recursion and base cases. In this case, he was identifying an empty array as a special case.

Don't get me wrong, the check for an empty array is misplaced in this case and it may have just have been an artefact of the trial and error approach that some students use. However, I would have been far better asking the student for his thought process when writing that method. I could have explained recursion if I thought it was appropriate and compared the iterative approach to the functional approach if the student was already familiar with the latter.

To draw analogy to martial arts, the instructor's job is to teach the techniques and skills of their chosen art. A mistake often made is to break down the skill into its most basic parts, and get the student to repeat those parts over and over in isolation without letting them use it in the larger context of self defence or competition sparring.

It all sounds reasonable until you consider that nobody comes in without at least some skill. Some people have fought a lot at school or in the street. Some may have done other martial arts. Some may have done contact sports like rugby. Some may be dancers or gymnasts. (Dancers and gymnasts make terrific martial artists, by the way!) A good instructor will still break down the techniques for the student to learn but he will also allow the student to incorporate them into the skills they already have. I can't imagine it's very different in any walk of life, including programming.

Since that incident, I have tried to be more careful about the nature of the advice I give and also in the delivery. Listening to students' comments about their own code can lead me to give better tips or to ask them more thought provoking questions.

09 February 2015