Here, I digress.
The irritation I've had for a few days now with the Perl version of a Markov (see previous entries, the one about _Practice Of Programming_) has come to a head and I finally sat down, typed the code in, and tried to understand it. (The irritation was a little cycle in my mind: "What, you think you can't even deal with a simple little Markov algorithm? You'll crumple like a house of cards if you ever get knee-deep in real code, you little pansy" over and over again, daring me to try.) Doesn't help that it's the first time I looked at it.
After several minutes of this, I decided I needed to play with the code, and their code was nearly indecipherable to me, so I just sat down and wrote as much of a Markov as I could.
A little voice said to me, "Write the tests first, that way you'll keep the steps straight."
Remarkably, it did. If I wrote the tests first and then wrote the code to fit the tests, the code worked, usually with just a few typo corrections. If I wrote the code, it was instantly a mess.
I don't know that I'll be able to actually improve the code in the book or not -- my Perl-fu is still kinda limited -- but I can sure give it a shot. Once I have a working version on my own, I'll look at their version and see if my idioms are better, and if not, why. If the code is an actual improvement I'll toss it in here; if it's not, I'm sure it will provide invaluable learning experience nonetheless.
And that's really the point anyway. Not like writing the best Markov implementation is going to win me a prize or something.
Write the tests first... (Score:1)
A little voice said to me, "Write the tests first, that way you'll keep the steps straight."
Remarkably, it did. If I wrote the tests first and then wrote the code to fit the tests, the code worked, usually with just a few typo corrections. If I wrote the code, it was instantly a mess.
I'm curious. How exactly does that work? I can't imagine writing a test for code that doesn't even exist.
Re:Write the tests first... (Score:1)
Y'see, in an ideal world, functions have pre conditions and post conditions. They have set inputs and set outputs. For example, if writing a modified julian day calculator, you should know that day number 51604 is 1 March 2000.
So one could happily write a test such as:
---ict / Spoon
Re:Write the tests first... (Score:2)
It also makes you think ahead a little more, feels like. "It should do this...how do I test that?" is the question you were supposed to be asking yourself anyway; writing the test makes you do it.
------------------------------
You are what you think.