things i like: refactoring, reuse

rjbs on 2004-12-14T20:52:58

Refactoring code is the best. By making a promise to myself to refactor (mercilessly), I can get my code written quickly, It isn't beautiful, but it works. I get to write lots of code. Then, when it works, I get to go back /and write it again/! Not only that, but I get to make it better. Then, because it's better, I can use it to do more things, which I code quickly, and then get to re-code.

"I will show less mercy in my refactoring" would be a good New Year's resolution for any programmer.

Also fantastic is having a really, really nice set of tools at one's disposal---and not just that*, but knowing how to use them. It's also nice to see unintended ways to use them, although that's not as easy a skill to acquire. The more I write code, lately, the happier I feel as it takes fewer and fewer words to say what I mean. The clarity of the code feels like it enhances the code's meaning, like a haiku. As in a haiku, the utter simplicity of the image is, itself, complex and profound. (I realize that I am no Basho of coding, but I am feeling less like a Bukowski.)

I rewrote some ugly parsing code, today. It reads my blog entries, pulling out the "name: value" headers and the body, and does some processing on the values. It was darn easy to replace with Email::Simple, which is now going to show up in a lot of my non-email code. Config::Auto keeps teasing me with ideas for improvements that I can't quite put my finger on. I know I can do something simpler with SQL::Abstract to Rubric's most important search method tiny. And maybe it's time to get back to Querylet and GutenbergRoget?

Last week, I was feeling bad about coding. I am feeling good, today. I think I just needed some positive re-enforcement. Halo 2 co-operative might have helped, too. I wish I could play co-op online!

[*] Another resolution: I will try to stick to just one way of writing em-dashes. Probably \w---\w, as in LaTeX.


Merciless Refactoring

ziggy on 2004-12-14T21:38:11

"I will show less mercy in my refactoring" would be a good New Year's resolution for any programmer.
Yes, but how merciless will you be?

A good friend of mine, let's call him "George", would get deeply involved in some code he was writing, and would occasionally get tired of chasing the odd bug. His response was the famous three step process:

$ rm *.[ch]

wait 15 minutes

Tell the boss, "Oops. I accidentally deleted all my source code. Guess I'll have to rewrite it now."

Now, George wasn't cavilier about this. He'd invest a decent amount of time in his code. Occasionally, he gets to a point where his understanding of a problem is much deeper now than it was when he started coding. For George, deleting all his code on a project[*] (or sub-project) was something to do when he was tired of dealing with yesterday's bugs and wanted to code it up the way he should have written it in the first place. Classic example of "throw one away".

You can't get much more merciless than that. ;-)

*: For best results, temporarily forget about the backups and CVS revisions before you rewrite the code you just accidentally deleted. ;-)

Re:Merciless Refactoring

rjbs on 2004-12-14T22:18:29

I'm not quite that merciless, and I'm not quite sure I'd want to be! Then again, I've gotten merciless enough in that direction to get some funny looks, following my reading of chromatic's writing on "Integrate Often," something like this here.

As is normal, I try to commit after every (write-test; code-code; pass-test) cycle, and I try to keep those cycles smallish. I refuse to leave my CVS checked out overnight, and delete it if it isn't committed. Some people think this is bizarre enough, but I've found that going further is better: frequently, the next morning, I realize that the reason I couldn't get that last hour's work checked in is because the previous seven hours were conspiring to make it more difficult than it had to be. I check out 24 hours earlier, and start over. Re-doing eight hours of work usually takes about an hour, and leaves me feeling much better.

It's like refactoring, after razing the first factor down and dancing on its grave!

Still, deleting all the code? I don't think I'm that hardcore.

Re:Merciless Refactoring

jmm on 2004-12-14T23:38:36

If nothing else, I suspect you'd generally want to keep the tests so that you can verify that the "refactoring" didn't break anything. So, starting the project over, completely from scratch, is a bit extreme in a non-XP sort of way.