Monday 4 May 2009

Refactoring Madness

I know what Krice is going through...some days it seems like most of the code I've written is utter garbage. Its not as bad as that, and it won't need doing what Netscape famously did (with disasterous results), but I do sometimes wonder, late at night/in the wee small hours/during the long dark tea time of the soul, what I was smoking when I wrote a particular chunk of code...

The main problems I've got with a lot of my current code are what Joel Spolsky refers to in the article above as "architectural problems":
First, there are architectural problems. The code is not factored correctly. The networking code is popping up its own dialog boxes from the middle of nowhere; this should have been handled in the UI code. These problems can be solved, one at a time, by carefully moving code, refactoring, changing interfaces. They can be done by one programmer working carefully and checking in his changes all at once, so that nobody else is disrupted
I'll give you an example of something that was a pain in the arse to fix but when fixed has really added value to the code. Previously (before 0.02b), the function to output text to the message log at the bottom of the screen took two parameters as input:
  • the text to add (as a string)
  • the colour to display it in (as a TColor).
As I'm only human, the natural result of this was that the colours were inconsistently used - a message in one section of the code for would be a different colour to a message resulting from another section of the code even though both messages were used for the same purpose.

Of course, with hindsight, the correct way to handle this is obvious - now, instead of a colour, a message type is passed in, which happens to be a enumerated TColor type, e.g. all messages that refer to killing enemies now are passed in with the second parameter of 'mesPlayerKill' which evaulates to the colour Orange.

Simple, yet the correct solution eluded me the first time.

Now imagine this multiplied by a factor of 50 and this is the task ahead of me (and most other roguelike developers it seems)

Are we masochists or perfectionists? You choose.

No comments: