Saturday, April 23, 2005

I only just realized the point behind the Liskov Substitution Principle - a rather interesting point, from a software design perspective. I once thought that the material we were covering in the OO design unit was quite superflous, and in my arrogance thought that most relevant topics must surely be intuitive and not worth the lengthy tomes that lie shelved in the library. I see now just how wrong I was. I can't say that I will enjoy the rigmarole of design, but I think I have gained a little more respect for it as a formalized process (obviously I don't think there's anything wrong with design itself; I just had qualms with UML diagrams, and questioned their purpose). What gets me down is the fact that we were spared the interesting examples that are abound on the net, and instead were treated with a dry, theoretical statement that did not make any conceputal sense. The lecturer said words to the effect of "Now we get to the interesting stuff" when he introduced it, but his treatment led me to believe it was very ordinary, and somewhat obvious. It's strange, because the lecturer definitely knows what he's talking about, yet his teaching is somewhat lacking.
There was a documentary on the beginning of the universe yesterday, and I saw a few minutes of it (I was far more tempted by the rare glimpse of The Daily Show; I think I prefer comedy over the profound nature of reality!). For some reason, whatever few minutes I saw of it made me realize how strange this all is. I was reminded of a funny line I read on a site that was philosophising about the meaning of life (unfortunately, I don't have the link), which, to paraphrase, went something like "Well, no-one knows what we're doing here". But thinking about the origins of the universe from a physics point of view makes me remember why I was so interested in physics at the end of high-school. Although I didn't understand most of it, the more bizarre topics (such as quantum superposition) really caught my interest, and I would take great pleasure in reading anything I could on such topics when they appeared in New Scientist. I went into uni with the intent of completing at least two years of physics, actually, but the first year wasn't as glamorous as I'd hoped. Fluid-flows, forces, they didn't really interest me; I was waiting for Young's double-slit experiment, and talk of alternate dimensions and parallel universes! Disappointed, I resigned myself to a year of calculus and software design. The interest is still there, although it's currently dormant and resurfaces only when provoked by an interesting book. I ought to try and rekindle the interest in my spare time, if only to get a different perspective on what in the world this all means.

Incidentally, this site about "The Singularity" came up when I was entertaining myself by reading FAQs on the meaning of life (it's better than working out class invariants, believe me). I haven't read it enough to really understand what it's about, but another great use of this blog is reminding me of things to read when I have the time (does that sound like a joke? It isn't..!).

Friday, April 22, 2005

Another post intended for the future-me, but I don't mind at all if you read it, gentle reader! Yet it is long, and ultimately but an expression of happiness at having found some joy with programming, for the first time in a long time!

The latest assignment for software has provided an excellent opportunity to try out test-driven development. I started off well enough, writing a decent (if excessively verbose) set of tests. A little background on the project - it is meant to be an implementation of a mathematical set. Fairly interesting, and it seems reasonably doable to boot. I learnt an important lesson, and that's that it's good to design your test suite too! I had the impression that I could get away with hacking it together, but aside from the fact that it's "good practice" to have clean design, there are the obvious benefits that come with clean code; primarily among them, reduced repetition and updating.

I could have been fancy and used std::map to help automate the tests completely, but I decided that it would be simpler and quicker to just hand code the tests and the desired output. It seemed easy enough, so I rushed in and made a few tests for equality and subset testing. It all worked, and I was happy. However, there were a few problems; chief among them was that I was having to initialize the set like so:


int elements [] = { 0, 1, 2, 3, 4 };
IntSet set (elements, 5);


As you can see, I was forcing myself to manually specify the length of the array. This could be overcome by using the sizeof (elements) / sizeof (elements [0]) trick, but it still didn't seem "right". Since arrays are evil, I thought there must be an elegant way to do it with a vector instead. No luck, because a vector can be initialized "as-is" with an array: so,


IntSet set (std::vector <int> ({ 0, 1, 2, 3, 4 }));


doesn't compile.

It dawned on me that an easier way would be to read in data from a file, as then I could read directly into a vector and then everything would be automated, and I wouldn't have to specify array lengths and muddy up the code. But I plodded on anyway, with the (very dangerous) thought that it would be too much effort to change. This is usually a very bad mistake to make! There are too many places to name where this very style of thinking is rejected completely, but reading and rationalizing is never quite enough for me. It took me a few hours to see that I was digging myself into a hole, because there seemed to be no end to the amount of repetition in the code. The turning point was when I had made a mistake in one of the statements which led to an obscure bug with one of the tests. Much toil and sweat later, I decided it was time to refactor.

So, last evening, I spent a good hour or so trying to get the tests to be read in from a file. In the process, I realized that this allowed me to be far more flexible with my tests - it became trivial to add new tests, and maintaining them was again a piece of cake. Previously, I was comparing each set to the very first set in the test method; I thought then I would code up separate tests for separate first sets. Terrible idea! With a test file, it took a few minutes to make the code able to read in a pair of sets, and perform the needed comparison. All of a sudden, the tests became much more robust! But I'm jumping ahead of myself, because there was another interesting problem to tackle - how could there be a generic method that worked for all the operator comparisons? I needed the code to work for equality (==), less than equals (<=), and so on - the basic idea was the same, but a different operator needed to be invoked in each case. "Pity you can't pass in an operator", I said to myself, before suddenly remembering the documents I'd read on SGI's STL reference about functors. I was overjoyed, because thanks to std::equal_to and friends, I could effectively pass in an operator as a functor! At this point, I recalled the older days of programming, the unbridled joy to be found when discovering something new. Here, I was realizing how something I'd seen so many times before could be of practical use and massively reduce the work needed.

I was quite satisfied with the progress made, and so I was prepared to move on and test more operators. Again, I came across a design flaw that I should have paid more attention to at the start - all my code was built under the assumption that the return type of the operators being tested was a boolean. When it came to set unions and what have you, the code had no chance of working. You'd think by now I'd learnt the art of patient design, but I was on a roll, and thought to myself "This can't be too hard to fix". Bold words! I quickly wrote up something that would work where the result was another set. It all seemed to compile, and I thought by now that I must be done.

But this time I decided not to let repetition go unnoticed; the code was full of it now, since the method for a boolean comparison and a set comparison were nearly identical, except for a few lines. Essentially, there are two things that are different. First, that we must read in the data from the file differently - namely, we should make sure that we read into a set, not to a boolean. Secondly, we should invoke the appropriate comparisons. Similar behaviour, yet different types..ring a bell?

Templates! I'd used them before, but not very seriously, definitely not with anything of any practical use. So it was rough going at first, trying to make it work. It seemed first that I needed a template of templates: sounds nasty, but it isn't really. Essentially, I needed to make sure that the invoked function could be called either for a pair of sets and a bool, or for a pair of sets and a set. "Seems easy enough", I said, and tried to do this in a few minutes. But I came across a brick wall - since we can't overload based on return types, my code didn't know which function to invoke in order to read the test data. I had originally given them the names getBoolData () and getSetData () (not very informative names, I know), and I obviously needed some way of making them have the same name if I were to invoke a different one based on type. However, the prototype for both methods was identical, except for the different return type, so the compiler couldn't decide which one to choose.

At this point I thought to myself "Let's look at what's different with these two functions". They were virtually identical, except that they have different ways of converting the result of the test-operation (equality would convert it to bool, since the result of equality is a bool). Common functionality everywhere else, so I was pleased with myself and thought "Easy enough, let's move the conversion code outside and merge both methods together". Sounds good, but I ran across the same problem - the comparison code was also being overloaded based on type! So again, the compiler was at odds to choose the right function.

Faced with the bleak prospect of giving in to mindless repetition, I flipped through Stroustrup's book, hoping to find some clues to help me. I came across template specialization, and it struck me that it could be useful - define a generic conversion function like template <class T> convert (const string &);. Then define specializations for bool and IntSet. It seemed easy enough to code, but I was skeptical as to whether this would work. But it did! The now generic getData () function was able to call convert <T>, and the appropriate type was filled in at compile time! Very nice!

Once this was done, the code looked much neater. It worked seamlessly for comparisons returning either booleans or sets, and there was no further redundant updating required. All this effort just for a test mechanism, yes, but it was a valuable learning experience. And, to boot, I can save myself the future worry of a buggy program, thanks to an automated suite!

One might be tempted to say that the proof really lies in the final mark, but I disagree. I found this to be a very valuable learning experience, and that, I think, is what matters more, even if it's very easy to be led otherwise.

There are probably lots of errors in the code fragments, most of them are off the top of my head.

Thursday, April 21, 2005

What if someday I confess to a group of youngsters how I like nothing more than listening to psychotic riffs?

Sunday, April 17, 2005

I decided to change the colour scheme with some vague notion of casting away the sometimes gloomy dark blue. True to my nature, I've only half-finished, because as you can see there are many discrepancies, not to mention that the colour is now rather dull on the eyes (or is it just me?). I'll probably fix it all up sometime in the next few days, but until then, I've given you yet another reason to avoid this blog..

Saturday, April 16, 2005

As usual, it took doing C++ in uni for me to finally buckle down and try to grasp the language properly, as I should have done some three years ago. I got Stroustrup's book in mid-2002, but I never really progressed very far. Admittedly, my programming and design skills were not very impressive at that stage, but nonetheless I find myself disappointed that I couldn't try harder to understand things back then. It's only now that I'm being forced to learn it (and get assessed on it) that I am getting sufficient motivation to read and comprehend the subtleties of this complicated (relative term?) language.

For some reason, my interest in extreme programming (XP) was rekindled once I started reviewing C++. Test driven development, for instance, seems a pretty radical shift in the way of approaching problems. There are no silver bullets in the programming world, but the excess hype surrounding TDD has made me think I ought to give it a try sometime. The problem is a mental block when it comes to using it - the part that says "Are you crazy? This is never going to work!". I've read up on critique of XP, and most critics are asked whether they've actually tried it; some say yes, but there are quite a few who admit they haven't, just because it seems so outlandish that it will be a waste of time! Although I've read many people praising the techniques (particularly writing unit tests before coding), including the sometimes infuriating Craftsman series at ObjectMentor (infuriating because I think "Argghh, you're joking, this isn't right!", much like the protagonist!), I'm a bit skeptical when it comes to larger projects, the ones where my brain tends to let me down. Rigorously testing components there seems to be a much harder task, and refactoring and making modifications as more tests are added seems like it wouldn't be as easy. What I'm having in mind in particular are the games I used to spend my time trying to write; I'd love to see whether my "knowledge" gained by experience these past three years has given me a better chance of completing these properly. Purely because I'm familiar with it, I would want to restart work on these using C++ with DirectX - part of me thinks I should give C# a try, but maybe I should go about it one step at a time.

I'm curious as to whether there is integration of a testing framework (I'm thinking CppUnit at these early stages) with any IDEs. Lots of examples of unit testing seem to be Java based, and with IDEs like Eclipse around I can see why (you never have to leave your workspace!).

Friday, April 15, 2005

There was an article about Woody Allen in today's paper, talking about Allen's fixation on death, and ending with a firm rejection of his permanent mental block, as well as his post Annie Hall movies! I have nothing of merit to say about Allen, but he said something I found to be amusing:

"...I had a line in one my movies - 'Everyone knows the same truth'. Our lives consist of how we choose to distort it. One person will distort it with a kind of wishful thinking like religion, someone else will distort it by thinking political situations are going to do something, someone else will think a life of sensuality is going to do it, someone else will think that art transcends. Art for me has always been the Catholicism of intellectuals..."



They don't come much more pessimistic than Allen, eh? The last line in particular struck me as being very interesting, because as with all views on matters like this, it could well be true. Of course, I disagree emphatically, but had I read this a mere six months ago, maybe this quote would be the one you saw on the side of this blog; remarkable, isn't it? I don't think anyone can entirely discount the possibility of death making everything without meaning, but really, one can get a sense that such a thing is just so improbable that it cannot be true! It depends on whether you find beauty in the world (I'm sure Allen does), and then whether you believe this can make everything worthwhile (Allen doesn't!). Allen no doubt considers this to be delusion, "wishful thinking" as he says, but hey, it's understandable. Such debates are with limited point I think, because I doubt either side can make an impact on the other (actually, maybe the pessimist has a better chance of influencing the optimist? Hmm).

Tuesday, April 12, 2005

There was a caravan somewhere in my neighbourhood today, which demanded a curious glance. This morning, I briefly touched on the subject of old adventure games in a lengthy conversation about video games and their perceived diminishing of quality. The caravan was sufficiently out of place for me to think it was there for a reason; for a second then, I toyed with the fantasy of exploring this caravan as though it would give me some clues to help me complete my "quest"; cue Sierra's "received points" music! Ahhh days of my childhood, where have you gone? And will I ever stop thinking about you?

Sunday, April 10, 2005

I really should stop making posts that are merely links to other posts I made last year, but I can't help myself. I'd forgotten the details of this one. I wish I'd kept the link to the blog I mentioned there, just for old time's sake. Lord knows I was in a similar state of mind then, I could easily have written something like that (albeit not entirely seriously). Looking back over the hazy period where I would oscillate between bleak existential laments and subdued (yet very strong) affirmations of life, I really don't remember very much. Selective memory at it's finest, I suppose? Actually, I do remember, but just overall. It's not a period I wish to relive ever. For the thousandth time, I will reiterate how glad I am that I took the trip last year.

This little bloggie of mine has really lost all its power huh? I seem to have this compulsion to compare my blog to other random ones I come across with a similar flavour. The way this is done is usually by reading through the front page, and I'm quite sure anyone who read my front page would yawn and move on. A pity, because it wasn't always like this, was it? It's funny, I think the way it is right now is much better than what it was this time last year. I think it was only in the order of 6 months ago or so that I became demi-serious about posting here, and posted things that I still find interesting. I've had it in my head that I would try to write something "any day now", but nothing has come of it. Could this be the end? (And the people shouted "Let's hope so") Heh, knowing my insatiable ego, the end of this blog is far away!

Saturday, April 09, 2005

Radio Paradise is a great eclectic online radio, probably the best one I've found. It's pretty hard to match it for variety; it looks like they'll play nearly anything, which means it can be uneven, but I think it's more or less interesting most of the time, and there usually isn't anything particularly offensive. It goes without saying that it can be uneven, simply because with a range this huge, there's bound to be quite a few average numbers. But it's more satisfying than classic rock stations that play the same things over and over again..

The ratings of other listeners are, as always, very interesting to take note of. I don't know what exactly it is I feel when I read comments mocking things close to my heart. This is only compounded by the fact that I think I am like most of those people, and that if I hadn't listened to (in this example) "Franklin's Tower" in those naive and innocent days, I might like them mock it and accuse it of stringing random words together..thereby not opening my mind to the subtle talents of Robert Hunter. There have been numerous instances where I've listened to something and said "Right, that's just trash" (maybe not so emphatically, but suffice to say I didn't care for it), and moved on. Funnily enough, reading a fan's comments was a strange experience, because I know that on a few occasions it led me to believe that the people who liked it must be really out there. It's quite probable then that there are many things which I am passing up on, more due to a lack of proper taste than anything else.

It's always good to be reminded of the need for self-assessment, isn't it!?

Edit: Now I have proof that I am no good. There was a cover of Tom Wait's "Jockey Full Of Bourbon" (which I haven't heard in a good year or two), and for the first few seconds, I was telling myself "Wow, what deliberately obscure lyrics!", thinking that the artist must be one of those people who (as Robert Hunter was accused of) strings random images together. I was sheepish to discover it was a Waits song I was dismissing (I rate Rain Dogs as one of my favourite albums; you'd think I'd remember all the songs, no?). So, it seems safe to say that if they did in fact play Waits' original version, I'd have the same reaction, and make a mental note not to pay attention to this wannabe-Dylan (or somesuch invalid comparison), thereby shutting off something that's actually really good.

The wannabe-Dylan comment didn't just come out of thin air, I think one of the things that originally flashed through my head when hearding "Jockey" was "Whoah, this person sounds like they're really trying to be Dylan". I didn't think there was anything bad about the lyrics, just that they were trying too hard to be poetic. Naturally, when I heard Waits' original a few years ago, no such thought popped into my head; sad, ain't it? But this leads me to pose a question: can something be good if it's made by someone, but not good if it's made by someone else? As in, because artist Y is essentially imitating artist X, the merit of Y's work is diminised, but if Y did the same thing, it would be hailed a masterpiece? It's always confusing when dealing with inspiration and originality.

I really don't like this tendency to dismiss anyone I haven't read about, especially newer groups, this is just a really bad knee-jerk reaction I have to cure myself of! Perhaps I should leave the radio on the whole day till it's all I can think about? (Actually, I'd probably never get bored, knowing the funky playlists these guys have..)

It seems somewhat fitting that I saw VH1 do their take on the worst #1 songs ever (in my defense, I was quite bored, and in absolutely no mood for anything cerebral). Initially I took it as just time-pass, but midway I was outright angry, because it seemed clear to me that most of the people trashing these songs from the past were probably in no position to make the statements they did, and the things they were saying were just so elitist and condescending that I just wanted to...ahh, deep breaths, I know. Yes, it was just meant to be entertainment, and I shouldn't take it all so seriously, but the "lesson" (is it really? This whole thing seems rather facetious actually) is that I should be a bit more careful pronouncing judgements (even if they are never proclaimed to the world, or just to this blog).

I was also reading comments about Peter Gabriel's "Sledgehammer" on RP, and someone said they were sick of Peter Gabriel and music like his, such as Rod Stewart. I have a funny lecturer who once insisted that "We are all ignorant", and ya know he might be on to something there. I suppose most people will be familiar with Gabriel as just the person who did "Sledgehammer" (and maaybe "Digging In The Dirt", which was also pretty popular), and think of him as a schlocky pop artist not worth their time. When, of course, there is a much larger story to Gabriel's career, with early Genesis and his own artsy solo albums that most people aren't familiar with. And Rod Stewart had his glory days too you know! Unfortunately, most things are not simple, but ironically it's dreadfully easy to form an inaccurate view of someone or something, without grasping the bigger picture.

I think my edits are invariably more interesting (to me) than my original posts.
I found the ads for the cartoon Star Wars: Clone Wars to be oddly amusing, thinking that the flaws of Episodes I and II were now being shamelessly converted into animated form. I paid little attention to it, but one night I noticed it was about to begin, and, being particularly bored (not to mention very disappointed with the show that preceded it), I thought it might provide some mild amusement. To my surprise, I actually liked it, and wondered "Now why can't George Lucas make his movies like this?". Ahh, maybe I'm just too into Star Wars, and maybe the action scenes reminded me of Knights Of The Old Republic, but I thought the cartoon was done very well, and contained precisely the sort of atmosphere I felt the movies were lacking (although I thought the second one was an improvement on the first). I find myself somewhat distressed though, with the fear that the last movie, promising and enticing though it seems at the moment, will end up disappointing. What's worse is of course that it is the last, and there will be no more. Oh well, at least episodes IV-VI will always live on.