Wednesday, 23 July 2008

On the road to Uganda

Tomorrow I'm flying to Uganda with eight other people on a mission trip to do building work for the Watoto orphanages.

Strangely, with all of the mad things going on here, I haven't really had a chance to stop and think too much about this trip.

It's no doubt going to be hard work, but an interesting and life changing thing to do. We don't yet actually know what kind of building work we'll be doing when we get out there; it could be house construction, or it could be essential infrastructure work. We'll see... Either way, I can't wait to make a difference in the lives of people out there.

I'll no doubt write about the whole experience in a couple of week's time when I'm back. I'm going to try to keep an audio diary of the trip, so hopefully there'll be something interesting to listen to, too.

My boss has asked me to kindly not contract malaria, or any other kind of life-threatening illness whilst I'm out there, so I'll have to bear that in mind...

I've not failed. I've just not succeeded yet.

I've not failed. I've just found 10,000 ways that won't work.
-- Thomas Edison.

I love programming - the act of creating a working piece of great code out of nothing.

Sometimes it's easy. No matter how hard the problem is you can see how to solve it, and you can intuitively construct a beautiful solution. It's like the problem just solves itself in front of you. Wonderful.

Often it's not that simple, and you have to work at the problem. That's when it gets really rewarding. You have to tax your mental faculties and stretch your abilities. There are some hard tradeoffs to make and some tricky design decisions. But, eventually you emerge out of the dark tunnel with a functioning piece of software and a warm fuzzy feeling. And you've learnt some interesting stuff on the journey, ready for next time.

However, sometimes it's not mere hard sailing. It's downright disastrous. The world conspires against you. You have to wrestle with problems outside of your control. Or, worse, multiple problems outside your control. It hurts. This might be rewarding work, too, but at the time it feels like your reward will be in heaven.

That's just where we've been recently. We've been fighting with a software release for a new piece of hardware. We didn't expect it to be a quick job, but we had no idea how nasty the release would turn out. We've been wrestling with non-functioning hardware, buggy drivers, bad operating system protocol stacks, you name it. Just when we thought we'd be really unlucky to hit another bug, along came another tedious (and hard to track) problem. Really, really painful stuff.

The interesting thing is that our main application software was blameless. We have rigourous unit tests and good coding practices that have ensured that it works pretty well. So unit tests are annoying: they remove all of the easy-to-fix problems, and force you to waste your time banging your head against really nasty things instead.

I hope that we finally have got it sorted now, as I'm off to Uganda tomorrow. But that's another story...

Thursday, 17 July 2008

Respect the Keytar!

This is a post that will satisfy synth geeks and very few others.

I recently bought a keytar, a Yamaha SHS-200 to be precise. It was a wonderful eBay purchase - bought for a song (pun intended) and in absolutely mint condition.

I really bought it for a laugh, but last Sunday at C3 we had a bassist crisis (they were all either playing guitar, busy, studying, dead, or something) and so we needed someone (a keyboardist) to fill in for our lack of bassists. Hang on, I've got just the right implement for that job... enter the keytar!

So there I was, on stage, playing an 80s retro throwback instrument. Genius! Despite my lurking doubts, the whole thing went really well. I was able to play some great bass parts, and doing so on the keytar was ace - I had room to move around, and wasn't constrained by being behind a large keyboard (which just doesn't make sense when you're trying to play bass parts). I normally move around quite a bit when playing keys, but there's a limit to how far you can move without a keytar.

The proof of the playability was in our opening song (Salvation is Here). It has a bass solo in the middle of it (check it out 2:20 in that video), which the keytar handled excellently. Much more fun to play than on my normal RD-700sx.

The Yamaha keytars are a bit limited compared the their obvious Roland counterparts, and it took a little getting used to. The keys are mini-sized things, and so are a bit fiddly to play, but once you're used to them you can play long bass runs without moving your hand too much. Useful.

The Yamaha beasts are not touch sensitive, which looked like it was going to be a real limitation. However, it turned out not to matter that much at all; in the majority of the songs you wouldn't have noticed anyway. This was only really a problem in quiet parts, when I had to modify what I'd play slightly. It did require some handling care, though: being small, light keys, it's also very easy to hit notes by mistake, or with your palm.

I only used the device as a MIDI master, and run the sounds from my normal rig. This is a serious retro instrument, and 80s FM bass honks would not have been appropriate!

All in all, it was great fun to play the thing live.

Keytars are going through a bit of a revival right now. I don't know why. Let's be honest, they're wrong in almost all important respects. They look silly. Any they make people squint at you like you're a lunatic. Using a keytar is just another way of asking for ridicule. But playing one is a very dirty kind of pleasure.

For those dull enough to care, more interesting keytar porn can be found here and here.

Friday, 27 June 2008

New workspace

I've moved 10 feet to the left in my office, and have occupied a new space. Since you spend so much time in a working environment, it's really important to ensure that it's as comfortable and productive an environment as you can make it.

In this instance, I wanted to make sure I had plenty of desk space to get two chairs in - to facilitate pair programming, to have enough space to access all the hardware devices I'd be working on, and to have good access to my keyboards and guitar - for when I get bored. (That happens fairly frequently).

A cute extra I got in the desk move was a pair of Samsung 214T 21" widescreen LCD monitors to replace my 17" ones. For any engineering manager reading this: it's really important to buy your developers decent screens. And it's also really important to ensure that they can have a dual head setup. Until you try working with two monitors, it's hard to appreciate just how useful it is. The amount of code, and associated control malarkey, that you can get on the screen at once is great. It is a real productivity aid.

These Samsung screens had a useful trick up their sleve - they can rotate. So I've set them up portrait (rather than landscape). Suddenly the amount of code I can view at once from a single file has almost doubled. It's awesome! Just don't ask me how much pain it was to persuade Linux to let me rotate the screens (when the net connection's down so you can't Google for the answer).

X is great. When it works.

For reference, here's my hit list for essential working environment features for productive code development:
  • A large desk space that you can get at least two chairs to easily for pair programming and working together.
  • High quality seating, so your back doesn't hurt after a day's work.
  • Decent specification computer(s) with at least two monitors.
  • Good monitors. Developers spend all day staring at these things. Make them good. And big.
  • Light: plenty of it. Natural light, desk lights, fairy lights.
  • A whiteboard to scribble ideas on. At every desk.
  • Ventilation. Seriously... Give me aircon, so I don't become drowsy though heat exhaustion, and give me oxygen so my brain can function.
  • The chance to personalise the space to make it feel "homely". I have several keyboards (of the musical variety), a guitar, and load blaring music (luckily I have a personal office space and understanding workmates).
  • Storage space to store books, possessions, and other kit.
  • Good coffee.
  • Toys. Seriously, developers are like kids - they like toys. Each developer should have plenty of cool toys (nice machines to work on, plus gadgets like Skype phones and the like to stroke their pleasure glands). And there should be nice R&R toys, too. Our kitchen area comes equipped with a pinball machine (this one) and air hockey.
I've also inherited a fish tank. I hope I don't kill the poor things!

Friday, 20 June 2008

Unit testing threads is hard (part 4)

In this next part of my occasional series on threading, we'll look at some of the classic problems that I've encountered when testing threaded code.

First, here are some facts:
  • Concurrent threads of code will execute differently on multiple CPU systems than on single CPUs with OS simulated multi-threading. Inter-thread synchronisation primitives will behave subtly differently causing the code to behave in different ways. This is a great way to expose subtle bugs when you least expect it.
  • CPU load (i.e. the number of processes running at one time, and the amount of work they are doing) can dramatically effect the performance of your process. What usually takes 10 microseconds could, sometimes, take 10 seconds, or even 100 seconds.
  • Without a rich set of thread primitives, and a good understanding of how to use them correctly, you will never be able to craft good unit tests for threaded code.
So, based on these, here are some classic mistakes in threaded unit tests:
  • Problem: Testing regularly occurring events that happen every N seconds by waiting Nx10 seconds and checking 10 events were fired. Result: A test that often gets the wrong number of events. Why: Asking a thread to wait for a period of time does not guarantee that it will wake up after that time exactly. You're at the mercy of the OS as to when your thread wakes up. If the computer is heavily loaded it might be a long time after you expected. More events may have fired by then.
  • Problem: Testing for a result that should be returned asynchronously by waiting for the event to occur, or for 'N' seconds to expire, because the result will "never" take N seconds to come back, right? Result: A test that fails whenever the test computer is heavily loaded. Why: If the computer is heavily loaded then its entirely possible that the background thread will take a long time to calculate the answer. Longer than N. The timeout will one day time out. I've seen this happen frequently on a machine with two or more builds running simultaneously.
  • Problem: Thread creation is not adequately managed in the code. There are races in construction/destruction of threaded objects. Result: Unit tests occasionally crash. Why: You can't safely construct test scaffolding around the threaded object unless you can control when and how the thread runs.
  • Problem: Tests that don't honour thread requirements. Calls that should only be made on a certain background thread are made on the foreground thread in the test because it should work OK. Result: Occasional crashes. Why: You did the Wrong Thing. If you shoot yourself in the foot, you should expect to hobble.
  • Problem: When objects are deleted in the unit test, background threads still exist that reference these objects. Result: Bang. Why: If the background thread fires the event after callback objects are destructed then you're effectively dereferencing a dangling pointer. Things go horribly wrong. This often is a subtle problem: frequently the unit tests run successfully, and the cleanup is fine. But if your test fails, the unit test framework signals this by throwing an exception. The exception causes objects to be destroyed as the stack unwinds. If the background thread does not get stopped as the stack unwinds, one unit test failure may manifest as a bizarre runtime crash. Ouch.
  • Problem: Testing threaded components as if they were not threaded. We abstract the threadedness out to make the tests easy and desterministic. It's what I'm preaching about in these posts! Result: The tests are easy to write, and never go wrong. Hurrah! However. the software component is still buggy as you have not tested it in the way it is normally run. Why: Go figure.
So we can see that when unit tests for threaded code are written badly, the tests can:
  • Behave erratically, sometimes working, sometimes not working.
  • Occasionally crash (segfault on Linux, for example)
  • Not actually test the threaded code in the way it is used in Real Life

Speaking in LA

I'll be speaking at an Numark/Akai/Alesis Engineering Summit in LA in late July. The talk I'm giving is "A Tale of Two Systems" and it should be great fun.

The amusing thing about this speaking trip is that I get back to the UK one day before I fly to Uganda. That'll be fun...

Monday, 16 June 2008

New article: Write less code!

The latest article in my "Professionalism in Programming" column is out now in C Vu magazine. It's called "Write Less Code", which does seem vaguely heretical for a software developer.

Here's a teaser:
It's a sad fact in our modern world that there's just too much code. I can cope with the fact that my car engine is controlled by a computer, there's obviously software cooking the food in my microwave, and it wouldn't surprise me if my genetically modified cucumbers had an embedded micro controller in them. That's all fine; its not what I'm obsessing about. I'm worried about all the unnecessary code out there.

There's simply too much unnecessary code kicking around. Like weeds, these evil lines of code clog up our precious byes of storage, obfuscate our revision control histories, stubbornly get in the way of our development, and use up precious code space, choking the good code around them.

Why is there so much unnecessary code? Perhaps it's due to genetic flaws. Some people like the sound of their own voice. You've met them; you just can't shut them up. They're the kind of people you don't want to get stuck with at parties. Yada yada yada. Other people like their own code too much. They like it so much they write reams of it. { yada->yada.yada(); } Or perhaps they're the programmers with misguided managers who judge progress by how many thousands of lines of code have been written a day.

Join the excellent ACCU to get C Vu and read the rest of that reveting article! And yes - that is my barnet on the cover :-)