Saturday, March 19, 2005

I've posted a fair few stories and poems that I'd written a long time ago, and pointed out how different my style was then, expounding on its flaws and quirks. Today, I got the opportunity to do something similar, but this time, it wasn't a poem or story I looked at, but a piece of code. Hardly captures the imagination, eh? I don't think code is looked on as an art form, but I think (as with most things) it can be, but this is rarely achieved. Elegant, succint code (and, let it be said, algorithms), can be aesthetically pleasing, but I don't think that's enough to call a piece of a code a work of art; I actually recall an argument about this on the net, and I think the argument for the case that code can be art suggested that it's the overall form and structure that needs to be considered. Hardly a precise definition, but this whole "Is this art?" business can be pretty murky. (I remember a snippet of a conversation where it was said that an "artist"'s latest piece was her getting plastic surgery when a group of people were looking on, singing opera or some such bizarre thing. It almost seemed to suggest that something's art if you don't understand it!)

Anyhow, back to the code. I noticed such troubling snippets as this:

void Score::WriteScore (Score score)
{
// Write the scores on screen
WriteText("Score: ", 7, 10, 700, 14);
WriteText("Score: ", 7, 900, 700, 14);

char *s = new char[9];

s = itoa(score.UScore, s, 10);
WriteText(s, strlen(s), 55, 700, 14);

s = itoa(score.CScore, s, 10);
WriteText(s, strlen(s), 945, 700, 14);
}


Magic numbers, using char * where std::string is much more suited, itoa where a std::stringstream might be better, and (most troubling of all!), a memory leak. All in just 7 lines!! The perils of being a self-taught programmer, you see. The date on the file suggests it was written just under 3 years ago, and by that measure I've improved by leaps and bounds - I've gone from a poor programmer to an average one. At this point, I'm more or less able to write visually clean code, and avoid the majority of beginner-mistakes (about time!). This has led people to remark that I am a good-enough programmer, because my code looks nice enough, neatly indented with fairly detailed documentation and flashes of good habits. Yet this is very superficial, because under the hood it's a different story: design-wise, my brain doesn't seem to be able to handle complex problems. The same people who give me encouragement would probably run away if they'd seen some of the terrible monstrosities I've coded, and the truly apalling ways I've "debugged" pieces of code.

Of late, I've lost the interest in programming that I used to have a long time ago. I slowly feel like it's returning, albeit with a regret that I've wasted so many years when I could've improved even further, and made up for any inherent limitations I have when it comes to making up a design. After a good year or so, I took out the Python book I've had my eye on, and have started to dip my feet into the strange world of quasi-functional programming. Let this blog be a reference with which I can gauge my progress.

2 comments:

Jenny said...

self taught programmers are kinda worrying. you haven't seen stew's flying tank program code that he made in year12 (and you don't want to!)

AKM said...

"self taught programmers are kinda worrying."

Hmm, thanks for the encouragement! :) So I'm not the only one who thinks I'm a hopeless case when in comes to programming ;) And to think I was mentoring your year, 'twas shameful.