Byzantine Reality

Searching for Byzantine failures in the world around us

The Quine Kata

Many software developers agree that the best way to be a good programmer is to program. That sounds obvious to most of us (even those that don’t program), but it’s simply not enough to program whatever it is you do for a living. Improving your coding techniques on your work code helps, but I think it’s best to try out your new techniques on code that’s totally different from what you normally do. I picked up on it a while ago from CodeKata, and it’s why I like messing around with language interoperability and other random stuff that I haven’t had to use thus far in my programming life. It just boils down to the adage given by Abraham Maslow:

If the only tool you have is a hammer, you will see every problem as a nail.

So get out there, learn tons of different techniques (and possibly programming languages), and get as many tools mastered as possible!

That being said, some background is necessary. I recently came across an interesting notion in the current book I’m reading, Gödel, Escher, Bach. It’s called a quine, and can be summed up in a kata-like form as follows:

Write a program that uses no input and outputs only the source code of that program.

Wikipedia has some interesting solutions to the problem (one of which didn’t even work for me), but they all seem to rely on the ability of the printf function to engulf itself. If you haven’t looked at the solutions yet, give it a try. It’s not particularly obvious what to do, but when you see the solution you’ll have the “aha” moment that typically accompanies katas. That being said, I got more out of the kata by trying it in a language that I’m decent (but not great) at with, Scala. My Scala solution to the Quine Kata is:

object Quine extends Application {
var s = "object Quine extends Application { var s = %c%s%c; printf(s, 34, s, 34); }";
printf
(s, 34, s, 34);
}

Unfortunately to get it exactly the same you need to remove the newlines so that the code is all on one line (which I would have put it in originally if the formatting on WordPress didn’t chop it off).

If there’s a new language you’re so-so with, try it out! Even though you’ve seen the C solution from Wikipedia and this Scala solution that uses the same technique, there’s still much to get out of trying this out in a new language. If there isn’t a new language you’re messing around with, why not? Go out there and pick one already! Make sure that you use ‘diff’ to compare the results to make sure they’re EXACTLY the same (there were a few times when I was close but not exactly so)

Two other interesting questions remain: how do you get the newlines in (presuming you can), and are there other tricks you can use in other languages to achieve this goal? There’s a Scheme solution on Wikipedia but it doesn’t seem to produce the exact same output (but I don’t know Scheme well enough to defend this argument). Furthermore, using printf in whatever language you use is essentially a C solution since the style of your code is basically C (e.g., how similar the C and Scala solutions are).

This type of program is interesting to me in the same way that Reflection is (although they’re completely different): that they both are like metaprograms (programs that produce programs or programs that program). I’ll definitely go more into depth later on Reflection since there’s quite a bit that it offers. But the Quine Kata has merit in a different way: that the program produces exactly whatever you produced to make the program. It definitely follows the spirit of Gödel, Escher, Bach (also something we’ll review once I’m done reading it) and is just fun to mess around with for a little while (which is why it’s great as a kata).