Schumi

Illustration

It seems that Michael Schumacher is back in F1 racing. While this is just temporary arragement until Felipe gets better, I will enjoy seeing him again.

I started following F1 while he was in Benetton (1994) and I enjoyed his driving since. While some of his competitive actions were doubtful at best, he was always one who made race worth watching.

I do hope that he will pass physical examinations and that I will have at least one more chance to see him in action.

HTC S740

Illustration

HTC S740 is mobile phone based on Windows mobile 6.1 Standard. I short, that designates phone that has number keypad and no touch screen.

Usually those smartphones are cheap and with weak hardware. This one is neither. Cost is around 350 € and for that you will get Qualcomm processor running at 528 MHz and 256 MB of both RAM and ROM. From hardware side there is also wireless connectivity (bluetooth 2.0 and Wi-Fi 802.11 b/g), GPS, nice 3.2 megapixel camera and slide-out keyboard.

There are two things you will notice when you take it into hands. First one is uneven background plate that looks like F-117 stealth aircraft (although manufacturer like to think about it as diamond edges). This is highly unusual design and it surely catches eye. Another thing that will not escape you is heaviness of this phone. With 140 g it is not phone for weak, but in defense, it does fit well within hand.

Screen itself is right behind plastic (most Smartphones put it a little further behind). This does great deal in enhancing quality of images on it. While I cannot say that this is best screen I saw, it is definitely the brightest one and readability is amazing even on direct sunlight.

Battery is quite good (1000 mAh) and it can last for a week of phone usage. As soon as you start Wi-Fi connections, be happy if it lasts for a day. Since Smartphone platform has always-on model some Pocket PC software can also drain battery (e.g. Skyfire).

Weak points

Slide keyboard design doesn’t cope well with left-hand use. It is just too easy to tilt it with your left thumb. Currently I only have troubles with pressing thin volume up/down buttons while keyboard swivels, but I have doubts that this is beneficial in long run.

Every touch leaves fingerprints on front surface. Although it did look great once you unpacked it, that is also last time it will be smudge free. That glossy finish seems to even attract dirt and make it stick.

Idiotic decisions

Designer of call and end keys obviously never used them on mobile phone. If he did, maybe he would take notice how difficult is to press those two keys. They are very thin and protrude less than a millimeter over other keys.

Placement of micro SD card is also story of it’s own. You cannot change it without turning off the phone - it is mounted right bellow SIM card. Every change of card will include shutting down the phone, removing SIM card, removing SD card, installing new SD card, installing SIM card and then powering up the phone. There is no possibility of doing this fast.

Conclusion

Although this article is somewhat focused on bad points, in reality it is a good phone. I like it better than HP iPAQ 514, especially hardware. If only designers had half of brain…

Windows Marketplace

Illustration

Windows Marketplace is new idea from Microsoft to collect all Windows Mobile applications in one place (like Apple’s App Store). Today it is starting to accept applications from vendors. While this is definitely a good thing I do not understand some road-blocks.

I, as developer from Croatia, cannot use this. Only developers from some twenty countries are worth enough to have their application distributed. If you happen to be outside of those countries - tough luck.

Annual subscription fee is set at $99. While this is not much, it is only a start. There is process of paid certification intent of which is to ensure applications are good enough for other users. Part of requirements to pass is to have proper code signing certificate and that doesn’t come cheap either. This is good platform for all companies that have nice little budget assigned, but it is definitely something that will “scare away” freeware developers. Applications that they will sell for free will now lose them money.

As you can guess, those two reasons were enough to drive me away from something that looks like a good idea.

GetHashCode

GetHashCode is approximate method for checking of equality that can end-up being used by other classes (most popular one is Hashtable).

.NET documentation gives us these rules:

  • Equal objects should return same hash code.
  • Same object should return same hash code every time until object is changed.
  • Distribution of hash codes should be random.

Equal objects should return same hash code

Sometimes checking for equality can be expensive and it may be few times faster to check whether their hash codes are equal. This will not give you definitive answer but it will allow you to remove quite a few objects from consideration. Hashtable does this through it’s bucket mechanism. If you insert object that doesn’t follow this rule, HashTable may be unable to return correct result (e.g. for ContainsKey method).

Note that this doesn’t mean that different objects must return different hash code.

Same object should return same hash code every time until object is changed

This one seems logical since it follows same reasoning as first rule. Whenever property that can make your Equals or Compare methods return different result changes value, you should recalculate your hash code. Small changes to object (something that doesn’t affect these methods) should not generate new hash code (they can, but there is no reason to).

Distribution of hash codes should be random.

This one is introduced to help classes that use buckets for divide-and-conquer (HashTable is our example again). This ensures that every bucket is filled to approximately same level so that any search doesn’t need to check every object.

Worst case scenario is every object returning same hash code value (e.g. return 0). While this follows rule one and two, performance-wise it is awful since every check will need to take all objects into consideration.

This is important rule, but you should not go through too much effort for this. Since GetHashCode method could get called a lot during search through collection, you should try to make it as fast as possible. Fast GetHashCode with less than optimal distribution will often out-perform elaborate and slow code.

What happens if I don’t override GetHashCode?

You will get default hash code that is based on memory address of your object in memory (in future implementations this may change). While this does work good enough, it may fail to recognize two objects as being same if they are created differently (e.g. returning same data row two times). In most of cases it will work, but it is generally bad idea to use it with collections (most of them use buckets) and it can lead to bugs that are difficult to find.

How to generate it then?

If you intend to use class with collection, you have probably already overriden Equals method (or you implemented some of compare interfaces e.g. IComparer). Whatever you have there to check for equality, use it in GetHashCode also. E.g. if your application uses property named Key for Equals, write:

public override int GetHashCode() {
    return this.Key.GetHashCode();
}

This makes it both simple and fast (if type of Key is one of .NET types) while following all rules.

Slightly more complicated situation is when you check against more than one property. One path you could take is to return GetHashCode based on element that changes more frequently. This will cause few collisions with hash codes (different objects will have same hash code) but it will not cause bugs. Depending on how many properties you have, it may not even have big hit on performance.

Other approach is combining two hash codes into one. E.g.:

public override int GetHashCode() {
    return this.Key.GetHashCode() ^ this.Key2.GetHashCode();
}

If you go that way, always measure speed. In more than one case you will find GetHashCode method that takes all elements into consideration is slower than one that has collisions. It all depends on objects you will use. From my experience, I would recommend avoiding calculating hash code on more than two properties.

Caching?

While caching may sound like a good idea, there is no need for it if you use GetHashCode of .NET Framework’s classes (as we did in examples above). Those classes either already have caching in place or they are using operation that is fast enough so that caching is not needed.

Only if you have your own hashing mechanism, you should consider caching results. Do not forget to update hash code also if object is changed.

Is it worth it?

If you are using something from Collection namespace, answer is yes. Almost anything there is either already using GetHashCode or it may use it in future. Even simplest of all hash codes will help performance.

Single Parent

Quite a few applications have both menu (MenuStrip control) and toolbar (ToolStrip control). Both of these controls have their menu containers. In case of MenuStrip this is ToolStripMenuItem and in case of ToolStrip we can use ToolStripSplitButton to get same effect. Both of those controls share DropDownItems property and with this you could make one ToolStripMenuItem and add it to both:

var newItem = new ToolStripMenuItem("Test");
newItem.Click += new EventHandler(newItem_Click);
toolStripMenuItem1.DropDownItems.Add(newItem);
toolStripSplitButton1.DropDownItems.Add(newItem);

This code looks nice but it does not work. In this case we will get Test added to toolStripSplitButton1 only.

Culprit for this is in SetOwner method (as seen with Reflector):

private void SetOwner(ToolStripItem item) {
    if (this.itemsCollection && (item != null)) {
        if (item.Owner != null) {
            item.Owner.Items.Remove(item);
        }
        item.SetOwner(this.owner);
        if (item.Renderer != null) {
            item.Renderer.InitializeItem(item);
        }
    }
}

As you can see, if item already has an owner, that owner is removed, and only than new owner is set.

Only solution is to create two new items and assign each to their own parent control:

var newItem1 = new ToolStripMenuItem("Test");
newItem1.Click += new EventHandler(newItem_Click);
toolStripMenuItem1.DropDownItems.Add(newItem1);

var newItem2 = new ToolStripMenuItem("Test");
newItem2.Click += new EventHandler(newItem_Click);
toolStripSplitButton1.DropDownItems.Add(newItem2);

While this means that you have two pieces of same code, you can find consolidation in fact that event handler methods can be reused.