HybridDictionaries – Careful!

I was refactoring some code the other day, and I noticed that at one point, there was code that created a foreach loop, but it was keeping track of an index inside, by manually incrementing at the end of the foreach loop. Something like this:

int index = 0;

foreach (Widget widget in WidgetBasket)

{

// Do stuff…

index += 1;

}

Well, being the foolhardy coder that I was, I said let’s just change that foreach loop to a for loop, so we can keep track of the index as a part of the loop construct. I’ve done this a thousand times, so I didn’t think much of it. I also didn’t take a minute to look at the data type of Widget, not that it would have helped at that point.

So, I threw my code into test, and lo and behold I started getting null reference exceptions. What the heck – all I did was change a little loop?!?

Well, I dove under the covers, and it only took me a few minutes to find the issue. The Widget object was of type HybridDictionary. If you’re not familiar with a HybridDictionary, it’s one of those built-for-speed collection types, behaving like aListDictionary when it’s small, and switching over to a HashTable when it gets larger. Like all collections I’m aware of, it has an array-style indexer, which enables it’s use in looping constructs.

The rub with HybridDictionaries is that they don’t behave like 0-based indexed array – a foreach will keep returning the next found item until it’s done, but a for loop doesn’t behave like that – the 0th element was null, and so was the 1st, and so on. So, obviously, it’s not a good idea to go with a for loop – they just don’t work.

Thankfully, there is a way to have the HybridDictionary behave normally, but it comes at a performance cost that probably destroys the value of this type of collection in many common uses. You’d just call the CopyTo method, which will copy the dictionary to a one-dimensional, zero-based Array. That’s not all that intuitive, and it’s a trap I bet a lot of people fall into when starting to work with the HybridDictionary.

Given all this mess, I’m not a big fan of the HybridDictionary – give me a generic list any day!

.91 BAC – Let’s do the Math

I was reading on the innertubes the other day, that a man was once found to have a blood alcohol content (BAC) of .91, which is apparently my share of alcohol, and your share, and a dozen of our friends… all by himself. Now that’s a feat – a feat of stupidity, but a feat none the less. So, that got me thinking, exactly how much alcohol do you have to drink to get that drunk?

Blood alcohol is measured in grams per liter of blood, where 1 gram per liter corresponds to a BAC of .01. A BAC of .91 means that you would have 91 grams of alcohol present per liter of blood you contain. This table details it all, if you want to be an anal bastard and check my figures.

So, either way, how much blood is in the human body? Well, if you’re in a slasher flick, it’s like 18 gallons, but in reality, it’s more like 5 liters. Using our last number, we get:

91 grams/liter x 5 liters = 455 grams

So, effectively, this inebriated gent had a measured level of pure alcohol consistent with consuming roughly 450 grams of 200 proof booze. The next obvious question is how much actual liquor is that?

Well, the short answer is a lot. Seriously, though, to figure that out we need a conversion from grams to milliliters. According to this chart, there are about 785 kilograms of alcohol per cubic meter, which is also conveniently 785 grams per liter. So effectively, if you were drinking pure alcohol, 455 grams is equivalent to .57 liters of the stuff.

Let’s assume this gentleman was drinking the strongest liquor commonly available, Bacardi 151 (also chosen for the easy math). Now, Bacardi is 151 proof, which in only arcane proof-math, is actually about 75% pure alcohol. So, effectively to drink .57 liters of alcohol, you have to consume about .76 liters of Bacardi.

How much of a bottle of Bacardi is that? Most liquor is sold by the milliliter, with the most common bottle size being 750 milliliters. Conveniently, 750 milliliters almost exactly equals .76 liters. So, the short answer is that this is THE ENTIRE BOTTLE. You sir, I salute you. I can’t drink more than two shots of that stuff without falling out of my seat, and you downed the entire bottle (not to mention the alcohol that was metabolized before the test was administered). I would think this is an experiment not to try at home (especially considering the LD50 of alcohol is about .40 BAC in humans).

Windows Vista is the New Windows ME

I’ve been thinking a lot about exactly where Microsoft has put their operating system lately. It’s obvious to everyone other than pure MS loyalists and apologists that there has been a lot of useless junk (amongst a few hits) coming out of their operating system division. OS development is arguably the core of Microsoft – without it, a lot of their other initiatives would fall flat.

Microsoft is definitely good at a few things. Their .NET platform is amazingly easy and productive to program for. Their XBox 360 is a profit leader finally, and a damn good console to boot. They have a lot great software products – I actually prefer Outlook to a lot of alternatives I’ve used in the past, including Lotus Notes, the most overgrown, craptacular product ever. IIS is solid (though I’m not a fan of 7.0 yet), Powerpoint and Visio are startlingly powerful in the right hands, and there’s some potential to Silverlight. They haven’t exactly shaken things up in the media player division, but their Zunes aren’t all that bad. However, then there’s the trainwreck that is Vista.

Almost every major Vista-related news item has something about how the OS was a stumble for Microsoft. Sooner or later, Microsoft is going to start admitting how much of a stumble it is. It took longer to develop than any previous OS, it’s a resource hog, it’s still unstable, and it introduces a load of features that nobody really needs or wants. On top of that, it’s still a security nightmare – one of the many things Vista promised to fix.

I’m going out on a limb now and calling Vista the next iteration of Windows ME. ME is widely regarded as the weakest Microsoft OS of recent memory. After ME, the Microsoft OS division pulled off one of their wisest moves in a long time – they brought their superior NT kernel down to the consumer level to become XP. XP stands as the most positively reviewed Microsoft consumer OS since Windows 95. It’s very stable in it’s current version, it’s not at all a resource hog on current spec machines, and it does all the things an OS needs to without trying to force you to work a certain way.

So, being an opportunist, where does that leave us? It’s not like Microsoft can pull another theft-job from their server division – they’re tapped out. Windows is in obvious need of an overhaul – it’s security model is intrinsically flawed, it can stand to lose a lot of weight, and the type of environment it was built for doesn’t really exist today. Now it’s all about how many cores you can cram onto a chip, instead of how fast you can pump content through a single pipeline.

Why not have it be a time for a rewrite? Why not of just the OS, but the push for a new direction in hardware as well? Wouldn’t it be great if Microsoft said that they intend to have thir next OS be a ground up rewrite focused wholeheartedly on parallel processing through many cores, that was targeted towards solid state memory, and included things like RAID redundancy out of the box? What about targeting a new type of interface, like how the iPhone has revolutionized the phone interface? If they took a stand today, they could get all the hardware in line for three years down the road when they would actually have the product ready.

They once were revolutionaries in the OS field, and they can be yet again. There hasn’t been a better chance in years – building on top of Vista is not a good idea. In the long run, their market share will dry up as their competitors stay ahead of the curve. It’s time for change (haha, election joke!).

Happy 1st Birthday(ish), FerretArmy.com

Sometime this month FerretArmy.com will become 1 year old. It’s high time to talk about the rather mundane history of the site, I guess.

I bought FerretArmy.com and FerretRevolution.com last year as my personal website. FerretRevolution is just another url that I bought in case I wanted it at some point for any other projects – as of now it still redirects to FerretArmy.com, but I’ll keep it around in case I do find a use for it. About the name – I didn’t have any really good ideas on what I wanted to name the site, so I just threw some more or less random words together. I’ve never been particularly partial to ferrets – I’ve never owned one, but they’re kinda cute I guess. I was planning on changing the name someday, but it’s grown on me, so it stays for now.

The site spent about a month in various states of ugliness until I settled on WordPress as my blog platform of choice. Before that I tried a few other engines, none of which I was very fond of. It crossed my mind to write the site from the ground up, but that would have been a very long-term project, one I was loathe to endeavor upon. I’ve been very happy with WordPress over the past year – I recommend it highly as a blogging platform and as a light content management system if you’re crafty.

My goal is to try to write a post a week – actually, I’d love to hit two posts or more a week, but there’s only so many hours in the day. I write about a pretty eclectic range of subjects. There’s no real rhyme or reason to my system, though the most common themes are programming, music, and video games. There have been about 50 posts in the past year, and I suspect many more to follow this one. If you’ve got any suggestions on how to improve the site or articles, feel free to send in a comment – I’m happy to listen.