Trick of the trade – set enum to not zero

July 5th, 2016

When defining enums set the first of the items to a non-zero value.
This way one knows whether one has forgotten to set the value.

using System;
public class Program
    private static int SomeNonInitiatedValueFromSomewhere = 0;
    public enum MyEnum
        AnItem = 1,
    public static void Main()
        var myEnum = MyEnum.AnotherItem;
        MyEnum myForgottenEnum = (MyEnum)SomeNonInitiatedValueFromSomewhere;
        Console.WriteLine("Now={0}", DateTime.Now.ToString("HH:mm"));
        Console.WriteLine("myEnum={0}", myEnum); // This enum is set.
        Console.WriteLine("myForgottenEnum={0}", myForgottenEnum); // This enum is not set.


Where the business logic resides in an application with O/RM

May 2nd, 2016

In an O/RM solution the business logic often ends up in the very queries. Don’t mock them if you want to test business logic.


Some hundred years ago when Microsoft was pushing their first Entity framework and all examples were PresentationLayer<->DataAccessLayer someone asked “When one is using an O/RM; where is the business logic?”

After all this time I have finally found out. In the very queries.

This effects the unit tests.
Because what you often want to test with unit tests is business logic and mock out everything else. The call to the O/RM is often quite simple. Then comes some hefty Linq statement that translates to EF or nHibernate. Then follows some simple handling of the result. The main logic is in the O/RM call.
So when one mocks out the O/RM one mocks out a big part of the logic.

One solution is to not mock out the O/RM but instead mock the data retrieval beneath. I have not figured out how to do this.

Another solution is to do an integration test all the way to the database. My first try at this was messy but worked out. My second try was equally messy but also worked out and has returned the investment many times. My third, and present, try uses helper methods to set up the data and contrary to the earlier tries it doesn’t look messy any more.
This is inspired by my present gig where the QA guy wrote a simple DSL with fluent syntax to set up test data. I have not managed to replicate his library but am, presently, happy with helper methods.

Change the font in the IDE

April 7th, 2016

I don’t think the standard type in Visual studio is the best one just because it is what is shipped.

Instead I test different and from 10+ years I have come to the conclusions:
1) Sans serif is better than serif.
2) Fixed width is not important – it is very seldom I need anything aligned by the count of characters.
3) Proportional width packs more characters per cm without losing readability.

Of all the fonts I have tested I still believe Arial is the best compromise.
Its drawbacks are that parenthesises are to narrow and that exclamation mark is too narrow and hard to read. I write mainly C# and Javascript and then ! is very important.
When I started out I believed that capital letter O looking similar to digit 0 would be a problem but have come to the conclusion that in reality it is not.

Fonts tested: (I have tested way more but just recently decided to write them down and cannot remember all)
Arial // yes
Arial narrow // Too dense to be readable on screen; for me at least.
Comics // Did not work for programming – too hard to read.
Consolas // Nope, too stiff.
Courier // Serif, does not work, too slow to read.
Leelawade UI // Works alrigh.
Malgun Gothic // A bit to wide for my taste. Also a bit “squarish”.
Microsoft new Tai Lue // Nice. A tad to wide for my taste.
Microsoft MHei // Lighter than Microsoft new Tai Lue. Maybe too light. About as wide.

Virtual machine – developing on a VM

April 6th, 2016

I have the host OSX for administrative tasks
and then one VM per Windows OS.

I would prefer to have one VM per customer but have not come around to it.
Setting up a new VM does not take that much time but installing Visual studio, Sqlserver, Chocolatey and configuring everything does. When Windows updates 7->8->8.1->10 I create new machine and copy stuff I need as I go; instead of updating the VM. This is my way of continuously cleaning my work space.

The only issue I have run into is multi monitor support that sometimes is a bit shaky to set up but it has stabilised every time. (as I write this my second monitor switches to green every time my screen saver has kicked in and I have to replug it)

I run Parallels which is not gratis and not even cheap. To add insult to injury they make sure to not support newer OSX and Windows versions as they come out. This means that the one time price (presently upgrade is 50€) has to be paid several times, like every other year or so.

I have not compared Parallels to their competitors Fusion and Virtualbox. AFAIK Fusion is about the same in both functionality and price. Virtualbox is free as in F/OSS.

The reason I started with Parallels instead of Virtualbox is twofold:
1) I was new to the host:OSX/vm:Win arena and wanted something someone said worked. (I did not want to buy a fancy pansy mac just to install windows on it)
2) By that time Virtualbox could not handle bootcamp.
My plan was to test to run Windows as a VM and if it wasn’t performant enough switch to bootcamp. Then I learned that to be able to run the vm as bootcamp it has to be configured as bootcamp to start with. I never did and I have never had the need.

(I would love to try to have the dev machine totally remote. Then I could have a phat machine without the fan noise.)

Upgrade problems Window 10 Surface 1

November 1st, 2015

Wireless/WLAN/Wifi did not work when I upgraded my Surface Pro Win8 to Win10.

My resolution was to go to the devices and remove a WLAN driver (Marvell Avastar 350N wireless network controller?) and right after that look for hardware changes in the same window. Unfortunately I did not take any screenshots and promptly forgot the accurate data.

Incremental vs completionist problem solving

October 21st, 2015

Do you solve the problem one step at a time, slowly ever going forward, but without the final goal?
or do you see the goal and work steadily towards it?

Of course you do both but might be biased.

Below is a readworth article about this.

Bra kommentar om teknisk skuld

October 5th, 2015

Den hyggligt korta artikeln delar upp teknisk skuld i flera delar: programmeringsskuld
, designskuld, testskuld och  kunskapsskuld. Jag tycker det var lite onödigt komplicerat; men samtidigt upplever jag det gav en möjlighet att diskutera. T.ex. vet jag ett projekt som klarar sig bra på allt utom kunskapsskulden. I och med att uppdelningen gav mig möjlighet att prata om, just, kunskapsskulden utan att blanda in all annan teknisk skuld tyckte jag att jag fick fram det jag ville säga.

Sedan undrar jag om man skall räkna in de andra skulderna i teknisk skuld.

Vidare undrar jag om “skuld” är rätt ord eftersom det är något man, förhoppningsvis, kan amortera bort. En teknisk skuld kommer aldrig försvinna för när man kommer närmare målet ökar man bara sin ambitionsnivå.

Ursprunglig artikel.

Designing a REST API

September 18th, 2015

Apigee has kindly written a book with simple illuminating examples on how to design a REST API.

It is short, concise and has good examples from the Real World like Google, Netflix and Twitter.

Logging all SQL from nHibernate

April 16th, 2015

There are several ways to see what queries are used by Nhibernate.

If you are doing serious development I recommend Nhprof. It costs some but it is second to none regarding overview and ease of use. One bug caught and it is paid for. The GUI is a bit clicky but does a good job of providing good S/N ratio.

Sniff the traffic. I have not done this seriously but once wrote an ADO sniffer since there wasn’t any profiler for MsAccess.

There there is tracing the RDB. Possibly gratis or even free, depending on database (I haven’t checked them all, only Microsoft’s one). In my experience it is too noisy to have running during the whole development cycle like one can do with Nhprof.

Then one can use Nhibernate itself to log. Check Nhibernate succinctly, chapter 12, for how to setup Nhibernate with Log4net. If you want to log through another framework the same author wrote an article.

Wrap the logging framework

April 15th, 2015

If you haven’t decided whether to use Log4Net, EnterpriseLibrary, nLog, home baked or any of the other logging frameworks – wrap it or abstract it.

Common.Logging is an “ordinary compiled lib”. It is strongly named with its pros and cons.

LibLog on the other hand is just a file (like Dapper). When developing a lib for public use this might be good to keep fewer dependencies.
The author of LibLog mentioned some pros for LibLog (vs Common.Logging) here. It uses dynamic and Tuple so it is Dotnet4+.

Simple Logging Façade is a compile lib, like Common.Logging.