Book: The Art of Unit Testing: With Examples in .Net

I finished the reading of The Art of Unit Testing: With Examples in .Net book before a week. The book was categorized as an agile book so you can forget as the book is something like the bible of unit testing. It just summarizes the agile and pragmatic point of view to unit testing more then programmer’s manual how to write correctly unit tests.

First chapters define unit testing basis, point to differences between unit and integration testing. The problem is that such text doesn’t highlight those differences enough and the reader couldn’t understand why the isolation is so much necessary. It’s followed by the expression of stubs and mocks and the list of mock frameworks.

The rest of the content is sort of novel about importance of unit testing, the purpose of testing in the company. The most valuable chapter is almost last one which describes how to implement unit testing as a new feature in your company. Such text is applicable for any new technology you want to use in your business.

As I’ve already said, this book is sort of novel about unit testing, one text which can help you to form your point of view why and how to unit testing, that’s all. There is lack of facts important to understand how and why to use isolation frameworks, what can happen when you would write w/o that – which happen in my company anyway 🙂 – how you would “unit test” any class etc.

Just read the book to complete your glance of unit testing but not suitable for unit testing novices.

HD2: Windows Phone 7.5 vs Android

My HD2 cell phone gain the time. 4.3” 800×480 ppi display, 1GHz processor, all facilities like Wifi, GPS, SDHC cards are the properties of new cell phones but also the characteristic of my two years old HD2.

The best part of this cell phone is the set of operation systems which you can deploy to this device. Windows Mobil 6.5 was the original, Windows Phone 7.5 Mango is now available since yesterday as well as various version and customizations of Android. Note that you can also install MeeBo and Ubuntu to you HD2 but it doesn’t seem as a common use-case.

I’d love to share my feeling since I’ve used Windows Phone 7.5 Mango for a week as well as I’d like to compare it to HD2’s Android.

HD2 and Windows Phone 7.5 Mango

I’ve started to use famous Yuky Back to the future ROM having basic Windows Phone 7 system available to standard upgrade via Zune. Simple instalation followed by mentined updates brought my phone into pre-release Windows Phone 7.5 Mango.

First thing you have to appreciate is the time necessary to start the system. It takes one fifth of RafDroid, previous android system I have used. Next stuff you will see after PIN typing is tile system on which the whole system is based.

I really love it, very nice typography is done, the whole system is based on the same type and thats the reason why I’ll probably return to Windows Phone system later in the future. See screenshot. It’s new, very nice! All internal features are developed in the same style so the message client is based on very lightweight graphic template. I believe that’s the style into which all system, including Android, will tend to in the future.

The next deliciousness is the social integration. One screen contains the aggregation of all social sites you logged in, facebook, twitter, linkedin. You just read messages or tweets, click and reply, retweet etc. That’s the second but last surprise.

All the rest read is about cons. The battery is the worst issue I’ve encountered. The reason was probably HD2 cell phone, which means that another native WP7 devices needn’t suffer. The battery life is about 16-20 hours if you don’t use the phone! The half if you write a few messages, read the internet etc. Terrible.

The lack of application. As Java is famous because of community and third party frameworks, C# maybe beats Java at the language level but that’s all the stuff, Windows Phone versus Android is the same story. Microsoft’s market contain’s a few application, the quality is vary. Maps are good example. Microsoft maps are build-in, I’ve downloaded also google maps as well as application. None of those application was able to show me the way from my position to any other, it just showed target address and wanted to start GPS to navigate. Android’s google maps uses location feature to find your position and do that, WP’s maps have no such functionality or it was hidden for me.

The same is applicable to all apps, android ones are featured and tuned but Windows Phone apps contains only base functionality.

The other terrible feature is the full integration with bing. Bing is not proper search engine for me, but you have to jailbreak you windows phone to allow google searching – for search feature as well as when you want to change search provider within the browser.

I’m not also sure with build-in integration of facebook and twitter. Facebook is to expand it’s functionality. Android’s facebook is up-to-date but such build-in feature is not so much updatable. Note that standalone facebook application is almost half year stale.

The last thing I hate is SD card locking. As you can suppose I’ve decided to remove Windows Phone 7.5 Mango and deploy Android system. I’ve just take SDHC card out of my phone and try to format in my notebook. Windows 7 doesn’t find this card and i’ve to start to read tutorial how to make my card accessible, my own card. That’s outrageous. Windows Phone just lock your card, I’ve read forum that a few guys said that they throw their cards into bin and just buy new ones! I had to install unlock application into my phone followed by using Magldr into reformat card’s MBR and use special program to format the card. Huh, the last think to kick the whole system.

HD2 and Android

I’ve planned to make a cell phone from my HD2, not tablet with 16 hours life on one battery charge. First version of Android I’ve used was Rafdroid. It’s very nice system which uses HTC Sense. It provides nice base screen I loved already in my Windows Mobile 6.5.

That’s the problem: it starts almost two minutes and it’s very slow. Instead of this build, I’ve just use clear and simple package based on Nexus Android. It was right step.

I’ve find out that I do not want base HTC Sense screen anymore because I don’t want to patch up the base screen by giant hour widget and there is a lot of weather applications anyway. Because I’m living in Czech Republic, there is beauty one for my country with text forcast.

I’m using all comfortable applications android market offers. I’ve installed Go Launcher EX to make the syste more beauty, I use best Gmail application, very nice maps, not falling evernote apps as well as Dolphin Browser with tunned feature set, translator with non-paid text reading and more and more applications. My SDHC is not locked and encrypted.

And the last thing: a few sms, internet browsing a few minutes call: 3 days per one charge!

Using of GUID as primary key in MS SQL is antipattern

We’ve decided to persist our domain model into database probably a year ago so beside the xml persistence our product uses any database system as a primary storage. The next step was key failure because we’ve designed the database schema according to current state of our domain model. Why not as NHibernate is so powerful that you are able to map almost every relation type, every scope of accessibility etc.

Beside slow performance, I’ve already written those articles before, which we’ve fixed yet the another major issue appeared before a few months. The performance of MS SQL database during continuous insertion of medium-large rows slows down until the system appears almost dead. Why?

As I’ve already written above, we’ve decided to not adjust the model according common database recommendations and just persist it. The lifecycle of our domain model needn’t be usually coupled with database persistence. So all primary keys in all domain entities were absolutely unique GUIDs.

It doesn’t sound so dramatic, we’ve hoped that it would only occupied more space than simple integer and maybe slightly slows the performance but now I know that choose GUID as primary key is terrible decision.

GUID as primary key decreases performance a lot

I’ve made PoC how to increase the performance of a few complicated selects statements. They were no “join hell” but they were slow according to our acceptance criteria. I’ve discover clustered index as very powerful feature which can improve performance itself a lot. I’ve made further inquiry and found that simple select above sorted table using clustered index is 35% faster with using int as indexed key with respect to previous GUID. Very nice!

Notice that the index was already ordered, for one milion of rows it took almost four minutes so it was useless for us anyway.

GUID as primary key kills the insertion performance

I believe that you can tune the performance for the all time so 35% up was great but expected when you start to play with all those nice profiler apps. The worst issue we’ve met was that already referenced above.

The primary key was unique GUID generated during the model creation. The consequent inserts just use those IDs and use them in the particular insert, no big deal. The blocker problem was absolutely heavy decreasing performance of those inserts. Lets look at real numbers: first thousand of inserts took approximately one second, when the database table contains almost one million of rows, one thousand of additional inserts took almost 20 seconds!

The database was dead, processor was not doing anything, hard drive as well. No dead-lock was found, it just slept. The first suspicious was cluster primary key, it’s cut-off only postponed the problem. After a few tests we’ve discovered that the clue is primary key. Cut-off the primary key notation returns the performance into original and requested numbers. Unfortunately our system also reads during these insertions so we are unable to switch all keys off.

The current solution is to rewrite model to use auto-generated integer primary keys which also clarified our view to domain driven design’s value object to avoid the meaningless use of entity type of object everywhere it’s possible.

Do not use GUID within database

I know that there are certainly places where GUID approach is usable but be aware of use them as any key. It:

  • takes significantly more space (4 bytes vs. 38 bytes), it can be serious issue when you use database with space limit, e.g. MS SQL Express
  • kills the database during the insertion
  • slows the performance of select clause, 35% down with respect to int
  • slows joins to tables 
  • almost exclude your application from the use of clustered index