Computer Science Teacher

February, 2011

• FizzBuzz–A Programming Question

Projects or exercises that lend themselves to many different “correct” solutions are just about the best sorts of learning experiences I can imagine. They really open the possibilities for discussion and from discussion – questions, answers, explanations – comes real learning. Recently Dennis Shaw, a student at Old Dominion University and a Microsoft Student Partner, told me about the FizzBizz exercise. (see Using FizzBuzz to Find Developers who Grok Coding for where it comes from) This is exactly that sort of multiple solution but not that difficult sort of exercise I love. Briefly stated the exercise is:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

So there you have it. It involves using some sort of conditional of course. Nested or unnested? It can be done either way. Which way is best? Do you check for multiples of three first or five? Do you check to see if the number ISN’T one of those cases  first or if it IS one of those cases? Or rather than If statements is there a different way completely? Perhaps a select/case structure? Does creating functions/methods complicate of simplify the result? If students know multiple programming languages is there one that is better for this than another (think especially about functional languages v. procedural languages). Are there special operators (like perhaps the ternary operator in C#, C/C++ and Java) that might make the code “tighter?” Is there a trade off between complication and understanding? Oh and of course, does your solution actually work?

Justify your answers. (Don’t you just just love the teacher-ness of “justify your answer?” )  I can see asking students to write up an explanation of how their solution works and why they did it that way as a means for forcing examination of the solution. And as a side benefit a lot of students can benefit from yet another writing exercise. Code that can’t be explained isn’t properly understood and that causes problems in the long run. A piece of code should not be “magic” to the person who wrote it.

There are many solutions on the Internet to the FizzBuzz question. So yes, some student might go searching for one and even try to use it. The “justify your solution” question means that they still have to understand and explain how it works and why they like it. I can’t see a student saying “I like this solution because I found it on the Internet and it works.” Well, I hope they wouldn’t try that. But if they do, grade them on how well they explain the benefits of using borrowed code and if they credited the original source properly. And of course they should also be able to explain why this solution is better than others they might have found. Surely they were not so lazy as to hand in the first solution they found?

BTW a couple of related posts of mine are Teaching, Learning and the Job Interview and Characteristics of a Good Programming Project

• Are Programming Languages Really Languages

Every so often someone suggests, sometimes in jest and some times in all seriousness, that programming languages “count” as a language for meeting graduation or degree requirements. According to Ian Bogost, who is not happy about it, some schools are actually doing this and others are close to it. (Computers are Systems, not Languages)

Is this a good idea? I think it largely depends on what you see as the value in learning a language other than your own native language. As someone who struggled his whole academic career with languages (first year French twice, First year German three times) I’m not so sure that language learning the way we do it in US is much good. Very few really become fluent enough to have more than the most casual of conversations (any one have evidence to the contrary?)  One reason I often heard for learning a foreign language (the politically correct term is world language I think) is that it gives one a better understanding of the culture of the countries where it is spoken. I guess that is true to some extent. Although with our “flat world” is it as necessary as it was a generation or two ago? Arguable. Isn’t the world of computers a culture though? Perhaps learning to program helps people, especially in the humanities, some insights into that “other” culture?

Another reason I have heard is so that one can read research and other works in the original language. Perhaps that made a lot of sense when French and German were the principal languages of some areas of study. But today people write in far too many languages to make focusing on one make for a solid argument – well unless that language is English. An awful lot is written first in English even by people whose first language is not English. On the other hand there may be some value in being able to read code in the “original” language. It’s a theory. And fortunately computer languages are often enough alike that learning one or two is enough to have a reading familiarity with others.

Does learning a different language help one understand their own language better? That seems a stronger argument than some others. There is no close corollary to this with programming languages with the possible exception of COBOL.

So am I arguing that programming languages should take the place of natural languages as general education requirements. Not really. I’m sort of asking why we have learning natural languages as a requirement. Sacrilege of course but why not? I do think that more people should take some programing or other real computer science course though. It is part of being a fully educated person in the 21st century.