Test-Driven Development for Games

Ξ August 24th, 2007 | → 0 Comments | ∇ Software Development |

Jamie Fristrom has some opinions on Test-Driven Development after using it for a year, and it seems that they’re mostly negative. It’s quite interesting to compare this with his experiences with TDD after only a few weeks.

Some interesting observations:

After a few weeks:

TDD is forcing me to pay more attention to the code, and I’ve actually found bugs just from studying it.

After a year:

Frankly, I’m disappointed. I was hoping for a bigger, more obvious win.

The number of “false positives” - tests that break because my assumptions changed - greatly outnumbers the number of real bugs really caught.

Also, they say it improves your code quality. Frankly, my code quality is worse, as I frequently expose things I wouldn’t have exposed so I can get them under test more easily - and other abuses.

Quite a different story after a year, and I’m not really sure I agree with his final word. Code quality is subjective of course, but in my experience TDD often results in more clearly defined interfaces. Sure, sometimes one needs to expose methods and functions that could have been strictly internal, but they are often more well-defined and….well…functional, with a clearly defined purpose.

After a few weeks:

TDD seems very well suited to gameplay and AI programming. It’s hard to TDD graphics and sound, but in the virtual world that the graphics and sound are just a representation of it’s a good fit.

After a year:

And we still end up introducing lots of bugs into the code. Partly because we’re not covering everything (not only are we not covering cosmetic features, “is this sprite/menu item/whatever in the right place?”, we’re not covering experiments, “Will it be more fun this way?” even if those experiments end up sticking) and partly because you just can’t cover all the cases of all the functions, and partly because the tests don’t catch multithread problems.

Oooh… To me it seems like the engine/framework code was suitable for TDD, but when it comes to the artistic side things get worse. I agree completely with the latter sentiment: there’s no simple way to test these things using unit tests. I think the reason is hinted in the last part. Multithreading is a problem, but so is user input and everything that concerns subjectivity. They’re haphazard and volatile and other cool words that indicate that they’re inherently problematic because the system complexity increases in orders of magnitudes when you introduce these things.

I agree with his final decision that TDD is not for him; it might be useful for some parts of game development, but the biggest - and most critical - parts cannot be easily tested. At least I know of no simple ways to do so. However, TDD itself isn’t bad. It forces a functional structure; it captures stupid memory leaks and such; but mostly, it’s extremely useful for projects with multiple members.

Jamie mentions the fact that his understanding of the code has increased due to TDD, and this is iiiincredibly useful in some projects. “How did they intend this function to be used? I know - let’s check out the unit test!” Another benefit of TDD comes when you’re working with other people’s code, even if they have no unit tests for you to glance at: if you’re using valgrind or other tools of that crowd you’ll easily detect whenever you’re using a library incorrectly. Trust me, it’s not always obvious what exactly needs to be deallocated…but if you’re running a unit test for a specific function, you’ll know instantly when something was done incorrectly.

On a final note: have you noticed that I’m only mentioning unit tests and their benefits? There’s a simple reason for this: I like unit tests, but I hate TDD itself. Writing the tests first is cumbersome and a waste of time in my view. Maybe I’ll change my ways as I gain more experience, but my current (non-game development*) approach looks more like XP, with rapid iterations:

  • Write a quick prototype
  • Write unit tests for the most obvious functions
  • Add more functionality and refactor if necessary
  • Unit test the new functionality
  • Repeat refactoring and unit tests until it’s good enough

I’m still undecided whether this means that I’m too unstructured and inexperienced, or if it’s actually a benefit to let code grow like this. Either way, I definitely think that unit tests have benefited in my work.

* The game development iterations look more like this:

  • Write a quick prototype
  • Sigh and re-write the prototype
  • Add some graphics
  • Re-design the game and re-write the prototype
  • Sigh and re-design the game because the gameplay is dull
  • Go back to the original design, modify it, and re-write the prototype
  • Repeat for a while until all of the sudden I realize that the prototype is starting to look like a proper game
  • Add music and graphics and make a final version
  • Test the game
  • Scream in frustration when I realize that the game sucks
  • Re-design the game, add features, re-write parts, and repeat ad nauseum

 

We Can Make Lesbians

Ξ August 7th, 2007 | → 1 Comments | ∇ Science |

We have the technology; we can make them stronger, faster…homosexual. What on earth am I babbling about? Read this article and get back to me afterwards. Or if you want instant gratification (or at least instant information) here are a few quotes:

The line between male and female just got blurrier. New research with mice indicates that both sexes have male gender circuitry in their brains. What makes females act like females, according to the study, is the ability to turn off this circuitry. [...] The findings challenge widely held theories about the nature of sexual identity. Many researchers believe that hormones shape the brain’s gender circuitry during development. But the current study indicates that the female brain may be wired the same way as the male brain and that some mice disable this wiring to become female.

I find this amazingly interesting for several reasons. First of all, this goes against the intuitive feeling that the default gender is female. (Since XX chromosomes are female and XY are male. Somehow the homogametic structure of the female gender feels more “orderly” - more intuitive.) But this study shows that such assumptions are ridiculous and rooted in superstition rather than genetics; gender is a very complex thing, and inter-gender relations even more so. One can’t really talk about default genders in a species that requires both for procreation.

Secondly, I’ve just finished a Teaching Company audio book on Neurobiology, and one of the things the lecturer brought up was ridiculous initial attempts to explain homosexuality. He finished his lecture by commenting that it’s very likely that scientists will discover the real neurological reason for homosexuality within a few years. Guess he was right! (Or at least, they’re on their way to finding out - the study above is in no way a definite answer to anything.)

…But the TTC lecturer also mentioned something more interesting: the consequences of such a discovery. He started with analysis; once brain scanning methods are improved we might be able to know if a person is homosexual or heterosexual merely by analysing the brain. Do we want to know if a baby is hetero or homo? And what about people who are happily married, thinking that they’re hetero - what will they do once they discover that they’re really standing way back in the closet? “Ohhh, this explains why I have a hard time getting it up, honey! So… Eh… What now? And what about our fifteen kids?”

Of course, the next step is to modify this behaviour. Sure, we can make lesbians…but we can also “cure” homosexuals. That opens up a whole bag of nasty business. The lecturer went on to - as people always tend to do, to drive the point home - make a Nazi reference, which was completely unwarranted and irrelevant. But the main point is very solid: what will happen if people can choose to be hetero or homo?

“Don’t be ridiculous. People won’t be able to choose that! In the study they genetically modified mice to lose the TRPC2 gene - we can’t do that on a grown person. In best case, it could just be done on an embryo,” is a very good retort to that. But I can bet you [insert a huge amount of bandoolas here] that if this study proves to be correct and relevant for human sexuality, then there will eventually come ways to artificially create the effect of the TRPC2 gene…or block it.

 

About

    Pretentious! Miro Karjalainen is a pretentious bastard with a background in punk rock, computer science, linguistics, embedded systems, game development and the noble art of drinking beer. E-mail: info@karjasoft.com

Admin Stuff