The Making of an Evil Genius

When I was around 9 or 10 years old, my school, as they would every year, put on a Christmas show. The younger children would re-enact the birth of Jesus of Nazareth and then the older children would perform a play. That year, we were performing Grimm’s Snow White.

I wasn’t performing on stage though. Instead, I was in charge of the music. We had a cassette of all the music and the children on stage would sing along. I would press play when its time to sing and after, position the cassette for the next song.

I wouldn’t know it at the time, but it would be this rather mundane task that taught me one of the most important lessons of my life.

A “technical hitch”

The music cassette had all the tunes with large gaps between each song. The cassette player was an early 1980s era device with a mechanical counter and a button to move all the dials back to zero. The number on the counter weren’t anything useful like seconds but how many times the motor had turned, or something like that. Fortunately for me, the teacher organising the play had gone to the trouble of writing down the counter position for each song and the order in which they are to be performed. What could go wrong?

Indeed, what could go wrong? A few days or so before the big performance,  we did a dress rehearsal and I demonstrated I could operate a cassette player without assistance. Everyone was happy.

Finally came the day of the show. Before the audience had arrived, the teacher told me to position the cassette for the first song and I did that exactly. Everything was ready. During the show, the first tune was played without a hitch and after the song was over, I dutifully positioned the cassette for the next song.

Time for the second song… disaster!

I had pressed play but instead of music, silence. I could hear the children on stage singing “We dig-dig-dig-dig-dig-dig-dig in a mine…” until they realised there was no music. The teacher duck her head behind the curtain and asked what was happening. I showed her the tape was running and I didn’t know what was wrong. She had to go up on stage and apologise for the “technical hitch”, to much laughter from the audience.

Truth is, I knew exactly what went wrong and I was already fixing it. What happened, was that when the younger children did their nativity play, their teacher was using the same cassette player for their music. When they finished, they put our cassette back in the player but the counter was all messed up. My first song was fine because the tape was already in place, but when I positioned the cassette for the second song, it was based on a counter that was in the wrong place. I had been playing one of the gaps between songs.

What’s more, I remember thinking that exactly this would happen while watching the earlier nativity play. I was sitting there thinking “I hope they put tape counter back when they finish.” But I said nothing, after all, the teachers were in control and they would have thought of this too and wouldn’t want me bothering them. I mean, they’re adults!

While I was rather mocked by the other children for spoiling an otherwise perfect performance, it turned out to be one of the most important moments of my childhood. It taught me possibly the most valuable lesson I’ve ever had.

People in authority can be wrong.

After that incident, I started looking at adults in a new light. Not just teachers, but my parents, politicians, bishops, experts, celebrities, anyone in authority. I finally saw them as the human beings we all are.

Picture credits:
“Samantha’s Christmas nativity play” by alecea on flickr.
“Soundesign tape deck” by kumar303 on flickr.
“Silence */” by circo de invierno on flickr.

is, then as (C#)

The C# language has a pair of related operators, is and as. Say you have an object, x, and you want to know if its really type Form underneath.

(x is Form) will be true if it is, or false if it isn’t. The as operator will actually perform the conversion, returning null if it can’t be converted to that type.

One common piece of advice to C# programmers is to avoid using the is operator. Here’s how both of these operators are typically used together.

    if (x is Form)
{
Form frm = x as Form;
/* Use frm. */
}

While the code works, all the is operator is doing is performing an as operation, and checking if the result is null or not. In other words, (x is Form) is equivalent to (x as Form != null). This means the code above is really saying…

    if (x as Form != null)
{
Form frm = x as Form;
/* Use frm. */
}

See that. The same as operation was done twice. What a waste! So, the standard advice is to re-write the code to use just one as operation.

    Form frm = x as Form;
if (frm != null)
{    
/* Use frm. */
}

Simple. So what’s the point of this article? Why use is at all?

The problem for me is that is just looks so darn nice. Beginners very quickly understand what’s going on when they see it. The re-written code that checks for null just doesn’t look as pretty and isn’t as obvious what’s going on.

You may be wondering what the cost of this additional operation is. I knocked together a quick test which looped is-then-as and then looped the optimized version the same number of times and reported the difference in time. When I increased the count to 100,000,000, I saw approx one second difference build up. That’s a lot of looping.

But hey, that cost is non-zero, and I’m wondering why. Compilers are pretty good these days. I recall advice from long ago that its better to use an XOR operation to check if two values are equal, or to use shift operations instead of multiplying. These days, best practice to write what you mean and leave micro-optimizations to the compiler. So why doesn’t Microsoft’s C# compiler optimize is-then-as into a single operation?

I don’t know. It could be that its not worth the development team’s time, or maybe there’s a subtle reason that escapes me right now. I wish I knew. But if we could eliminate the repeated operation and use is freely, code would be lot nicer to look at.

Picture credit: Fruit balance by Pickersgill Reef.
Many thanks to Eric Lippert and the commentators to his article that inspired this piece.
Update 18/Jan/2010: Introduction modified to be better understood by non C# programmers. (Thanks Andrew.)

Computers are fast!

Killer Sudoku is a popular variant on the perennial Sudoku game found in British newspapers. The same rules about the numbers 1 to 9 appearing only once in row, column and 3×3 box still apply, but unlike Sudoku, the puzzle doesn’t come with any numbers filled in. Instead, cells are groups with dotted lines and the total of all those cells are written in.

Here’s part of a puzzle. The two cells grouped by a 10 could be 1+9, 2+8, 3+7 or 4+6. (It can’t be 5+5 because that would mean two 5s in same box.) Not a lot to work on, but also look at the three cells grouped by a 23 in the same 3×3 box; The only possible fit is 6+8+9. This means the only possible fit for the 10 group is 3+7, as all the possibilities would clash with the 6,8 or 9 required to make the 23.

I could go on. If you want to complete the puzzle, its the Guardian‘s puzzle number 166. The point of this article isn’t to solve the puzzle, but something I observed along the way.

Am I sure there’s no other way to split 23 into three parts, 1 to 9, without duplicates? I confess, mental arithmetic is not my forte. Part of the puzzle solving process for me is going through each dotted group and building a crib sheet on all the possible ways to make the total, so I can cross them off later.

This isn’t a part of the puzzle I particularly enjoy, so I decided to automate it. (Is that cheating? I don’t think so in my circumstances, but that’s another topic of conversation.)

So I sat down and designed an algorithm…

  • Input: n (the number of cells, 2 to 9) and m (the total, 3 to 45).
  • Output: All solutions to SUM(n integers 1-9 without duplicates) = m.

Only problem, as with so many mathematical endeavors, life got in the way. By the time I had worked out a basic algorithm, I noticed it was 2AM and suddenly felt rather tired, but I didn’t want to stop, so I simplified my plan.

Loop, starting with (1,1,1,1,1,1) then (1,1,1,1,1,2), ending with (9,9,9,9,9,9). For each combination, if they add up to the target, have no duplicates, and is in order, add it to the list. It was a very inefficient algorithm, but it would work and I wouldn’t have to expend much thought on building it.

Then something surprising happened. While testing with 2 or 3 digit targets, it was near instantly fast, but I had expected 6 digit targets to take something like a few minutes. In fact, it took 2 seconds.

2 seconds!!!!! I had learnt to write programs on my early 80s BBC Micro and I was used to leaving it running overnight calculating something like this. Computers are fast!

(If all that seems too much like hard work. Wikipedia have built the tables for you.)

About This Blog

Jawohl! I am Wilhelm von Hackensplat, software developer and evil genius. This is my blog.

I’ll be writing on software development, otherwise known as that-which-pays-the-bills. I’ve been doing this for most of my life, ever since mother Hackensplat bought a Sinclair ZX81 all those years ago.

I have tried blogging before, but I found myself really dissatisfied with the structure of a blog, specifically the normal view of showing the most recent posts first. To me, that’s not the most important thing. I want a casual visitor to bask in my unaccountable brilliance and see my best stuff.

This is my main beef with blogging software, that it doesn’t make for a very good document publishing system. There is a layout for blogs, and thou shalt not deviate from the norm. What I’d like is a document management system that includes a capable blogging sub-system. I would write my own perfect system, but time is short and life is hard. So back to blogging, here I am.

GenieƟen!


Picture credits:
Laboratory by tk-link of flickr