Archive for the ‘Code and Development’ Category

When code reviewing – do not forget to give appraisal too

September 29th, 2016

Albeit code reviewing is for the greater good and we all adhere to the idea of common ownage of code we are still critisising someone else’s labour.

Psychologically we cannot neglect that.

Ergo: remember to tell about the good solutions and code you find.

Unfortunately TFS does not have a (good) solution for differentiating between comments and praise and critisism.

AutoMapper does not play well with multi tier architecture

September 27th, 2016

I have found out that Automapper needs to be set up once and only once.
This means that one place has to know of all the classes that have a mapping. In a multi tier application this is not possible without treading on someone’s toes.

I have seen a solution which loops through all loaded assemblies and maps once and for all. But this solution means we have one such point – like when a web app is loading. So if you have automatic tests, that setup has to be called from there. And any kind of automatic loading of assemblies cannot use Automapper. Well… if one reinitialises all mappings one can.

I might see another solution where one has one assembly that knows of all assemblies (with mapping) and makes sure Automapper is initialised. My experimentation with this lead to circular reference problem though.

When I am at it – the documentation is short and straight to the point (good) but misses the point that the code in the documentation should be usable (not good).

The API was not explorable, which means that there is no natural way to start mapping and find out how the API works as one goes. Instead I had to google and find a bit here and a bit there.

With that said – it still solved a problem for me. And that is considered good.

Resharper and Visual studio short cuts

August 30th, 2016

There are some I use that might be useful for my readers too:

Resharper has 3 main combinations.
shift-alt-enter – Somewhat resembles Visual studio’s ctrl-..
ctrl-shift-r – Refactoring. Resharper’s raison d’être.
alt-ins – Inserting stuff by templates. E.g. a constructor taking all properties as arguments or Equal and GetHashCode methods.

ctrl-alt-q – Quickview instead of shift-f9

ctrl-w – Mark whatever you’re at. It can be set to recognise Pascal/camel cased words too as one often wants to edit just a part of a variable name.
Here’s a nice touch: continue press ctrl-w and the mark extends. A super way to mark a method.

ctrl-f12 – Go to implementation (instead of declaration). Often ctrl-f12 is enough.

alt-f12 – Sneak peak of whatever you are standing on. A good way to look at a called method body without having to  open yet a window, or move in an existing.
Here’s a nice touch: the opened sneak peak is a full fledged editor so go ahead and navigate, edit, debug and edit-and-continue as you are used to.

Above the method name (VS2015) there is a row with information about callers, editors, tests and whatnot. Hover over them with the mouse pointer and you get the shortcuts. They are typically ctrl-alt-1, ctrl-alt-2 und so weiter.

ctrl– and ctrl-+ – Navigate back and forward in history. On pre VS2015 this did not necessarily work with a non-us keyboard layout.

ctr-esc – Close the active tool window.

ctrl-shift-f12 – Go to the next item in a list, typically compile error but some other lists work too. Try compile with some errors. Then you can step through the errors with this shortcut.
TODO:Check it really is shift-alt-f12 as my muscle memory seems to elude me.

ctrl-shift-l – Delete current row. This is not as good as can be. Ctrl-l should be shorter but it is bound to cutting the row. Unnecessary really since ctrl-x does this if nothing is marked. I have earlier bound deleting a row to ctrl-l but nowadays don’t bother; i press ctrl-shift instead.

ctrl-x and ctrl-c – When nothing is marked they cut respectively copy the current row.

ctrl-shift-v – Cycle through Visual studio’s multi clipboard. A multi clipboard is a must. I use Ditto instead so I get a system wide multi clipboard with search capability and power off persistance.

shift-alt-enter – Maximises the window even more. There is no need for the top bar which says “Your project name – Microsoft Visual studio” and takes up the whole top of the screen. In the VB6 IDE one could even get rid of the menu; it is seldom needed to be visible – one just pressed alt or moved the mouse to the top and it showed again.

ctrl-r-t – Run the test method the caret is positioned in. If the caret is outside of a method is runs the whole class. If it is outside the class… – I haven’t checked, you tell me. Both Visual studio and resharper lacks some basic functionality like debug last method; as it stands now there is only run last method. I have the need to switch back and forth between running and debugging. If you are deep into writing tests – check Ncrunch which does a fantastic job of running tests and visualising what is tested and what is not. It also has the handy tool run any test that brings me to where the caret presently is. (r#)

ctrl-r-ctrl-t – Debug the test method… see ctrl-r-t. (r#)

One can have two windows side by side. Tests on the left and testee on the right. Or HTML on the left and Javascript on the right. Or client code on the left and server code on the right. One can have three windows side by side. Four.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
                   
public class Program
{
    private static int SomeNonInitiatedValueFromSomewhere = 0;
   
    public enum MyEnum
    {
        AnItem = 1,
        AnotherItem
    }
   
    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.
    }
}

from https://dotnetfiddle.net/17tQMB

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.

TL;DR

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. When I do I change and then change back.
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.
Bahnschrift light // The colon and semi colon are too light. Almost invisible at my setting.
Calibri
Comics // Did not work for programming – too hard to read.
Consolas // Nope, too stiff.
Courier // Serif, does not work, too slow to read.
Kalinga
Leelawade UI // Works alright.
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.
Microsoft JhengHei UI Light // Too wide for my taste. Reading was not fluent. One/L/Pipe differs. O/Zero differs. Discreet parenthesis. Thin !.
Microsoft YaHei // Could be more narrow. Easy to read.
Nirmala UI Semilight //  Pixly.
Segoe UI // Nice. One/L/Pipe differs. O/Zero differs. Discreet parenthesis. Normal !.
Tahoma // Possibly too fat. One/L differs. Pipe only small difference. O/Zero differs. Normal parenthesis and !.
Yo Gothic // Works alright but is a wee thin on the laptop but ok on my bigger monitors.
Yo Gothic Medium // Slightly fatter than Yo Gothic. Ok. Different O0 and 1l but discreet !.
Yo Gothic UI // What I am currently testing.

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.)

https://www.parallels.com/eu/products/desktop/
https://www.vmware.com/products/fusion
https://www.virtualbox.org

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.

http://apigee.com/about/resources/ebooks/web-api-design

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.