Archive for the ‘.NET’ Category

Testing framework is not just for writing… tests

Wednesday, January 18th, 2012

Quick ques­tion – from the top of your head, with­out run­ning the code, what is the result of:

var foo = -00.053200000m;
var result = foo.ToString("##.##");

Or a dif­fer­ent one:

var foo = "foo";
var bar = "bar";
var foobar = "foo" + "bar";
var concaternated = new StringBuilder(foo).Append(bar).ToString(); 

var result1 = AreEqual(foobar, concaternated);
var result2 = Equals(foobar, concaternated);

public static bool AreEqual(object one, object two)
{
    return one == two;
}

How about this one from NHibernate?

var parent = session.Get<Parent>(1); 

DoSomething(parent.Child.Id); 

var result = NHibernateUtil.IsInitialized(parent.Child);

The point being?

Well, if you can answer all of the above with­out run­ning the code, we’re hir­ing. I don’t, and I sus­pect most peo­ple don’t either. That’s fine. Ques­tion is – what are you going to do about it? What do you do when some 3rd party library, or part of stan­dard library exhibits unex­pected behav­iour? How do you go about learn­ing if what you think should hap­pen, is really what does happen?

Scratch­pad

I’ve seen peo­ple open up Visual Stu­dio, cre­ate ConsoleApplication38, write some code using the API in ques­tion includ­ing plenty of Console.WriteLine along the way (curse who­ever decided Client Pro­file should be the default for Con­sole appli­ca­tions, switch to full .NET pro­file) com­pile, run and dis­card the code. And then repeat the process with ConsoleApplication39 next time.

 

The solu­tion I’m using feels a bit more light­weight, and has worked for me well over the years. It is very sim­ple – I lever­age my exist­ing test frame­work and test run­ner. I cre­ate an empty test fix­ture called Scratchpad.

scratchpad

scratchpad_fixture

This class gets com­mit­ted to the VCS repos­i­tory. That way every mem­ber of the team gets their own scratch­pad to play with and val­i­date their the­o­ries, ideas and assump­tions. How­ever, as the name implies, this all is a one-off throw­away code. After all, you don’t really need to test the BCL. One would hope Microsoft already did a good job at that.

If you’re using git, you can eas­ily tell it not to track changes to the file, by run­ning the fol­low­ing com­mand (after you com­mit the file):

git update-index –assume-unchanged Scratchpad.cs

scratchpad_git

With this sim­ple set up you will have quick place to val­i­date your assump­tions (and answer ques­tions about API behav­iour) with lit­tle friction.

scratchpad_test

So there you have it, a new, use­ful tech­nique in your toolbelt.

Tests and issue trackers – how to manage the integration

Tuesday, March 1st, 2011

In highly dis­ci­plined teams when a bug is dis­cov­ered the fol­low­ing happens:

  • test (or most likely a set of tests) is writ­ten that fails expos­ing the bug
  • a ticket in issue track­ing sys­tem is created
  • a devel­oper fixes the bug, runs all the tests includ­ing the new ones and if every­thing is green, the ticket is resolved

My ques­tion is what if some time later (say sev­eral weeks after) a devel­oper wants to find which issue relates to the tests he’s look­ing at, or which tests were doc­u­ment­ing the bug he’s look­ing at. How do you man­age the link between the tests and issues in your tracker?

Solu­tion one – file per ticket

Quite com­mon solu­tion to this prob­lem is to have a spe­cial folder in your test project and for each ticket have a file named after the ticket’s ID like this:

Tests solution

That works and is quite easy to dis­cover. How­ever the down­side of this solu­tion is that it intro­duces frag­men­ta­tion. If a bug was found in a ship­ping mod­ule does it really make sense to keep some tests for ship­ping mod­ule in Ship­ping­Mod­uleTests class and some other in CRM_4 class merely because the lat­ter ones were dis­cov­ered by a tester and not orig­i­nal developer?

Solu­tion two – ticket id in method name

To alle­vi­ate that another solu­tion is often used. The tests end up in Ship­ping­Mod­uleTests bug the id of the issue is encoded in the name of the test method. like this:

[Test]
public void CRM_4_Gold_preferred_customer_should_have_his_bonus_applied_to_net_price()
{
   //some logic here
}

[Test]
public void CRM_4_Silver_preferred_customer_should_have_his_bonus_applied_to_net_price()
{
   //some logic here
}

That’s a step in the right direc­tion. It makes the link explicit and you can quickly nav­i­gate the rela­tion either direc­tion. How­ever I don’t like it very much, because most of the time I couldn’t care less about the fact that this test doc­u­ments a long fixed bug. Yet I am con­stantly reminded about it every time I run my tests.

tests

Solu­tion three – description

The solu­tion I found myself using the most recently is to lever­age the descrip­tion most test­ing frame­works let you asso­ciate with your tests.

[Test(Description = "CRM_4")]
public void Gold_preferred_customer_should_have_his_bonus_applied_to_net_price()
{
   //some logic here
}

[Test(Description = "CRM_4")]
public void Silver_preferred_customer_should_have_his_bonus_applied_to_net_price()
{
   //some logic here
}

This still makes the asso­ci­a­tion explicit and search­able, but doesn’t remind me of it con­stantly where I don’t care.

tests

What about you? What approach do you employ to man­age that?

Git tooling for .NET developers

Wednesday, January 26th, 2011

Many devel­op­ers work­ing on Win­dows stay away from Git. There are many rea­sons for this but from my obser­va­tions and dis­cus­sions I’ve had, the most com­mon can be sum­ma­rized by this tweet by my friend Paul Stovell:

 

can everyone please stop using Git? Mercurial has a better UI (TortoiseHG), I'm sick of Git UI's

I’m not get­ting into holy wars, and I’m not try­ing to con­vince any­one that Git is bet­ter than any other VCS. Instead I’ll walk you through the tool­ing I use to inter­act with Git on Win­dows, with Visual Studio.

Git Exten­sions

First thing you should be get­ting is Git Exten­sions.

Git Extensions context menu

With that, sim­i­lar to Tor­toi­seX fam­ily of tools you get nice con­text menu that gives you access to most com­mon oper­a­tions quickly, via GUI, and with no need to mem­o­rize com­mand line options if you want to avoid it. You can also launch Git com­mand line in the selected folder and then all power of git is at your disposal.

Visual Stu­dio

If you’re a .NET devel­oper, you’ll want to work from within Visual Stu­dio. I’m sure you’ll be happy to learn that Git Exten­sions has a really nice Visual Stu­dio inte­gra­tion as well.

git_extensions_visual_studio

You get two things – a menu with all the options that Explorer con­text menu gives you and more, includ­ing abil­ity to edit .git­ig­nore file (also the tool will gen­er­ate a new .git­ig­nore file for you with Visual Stu­dio spe­cific rules!) and to launch a Git bash. Also you get a Git tool­bar with most com­monly used com­mands: Com­mit, browse, push, pull, stash and access to settings.

The way I usu­ally work with it, is I use Git bash for most oper­a­tions. There’s one excep­tion to that rule though – committing.

git_extensions_commit_window

I think Git Extensions’s Com­mit win­dow is the best of all VCS I’ve worked with. It clearly sep­a­rates files you want to com­mit (in your Index) and files you leave out for now. It clearly shows you sta­tus of each file (new, deleted, mod­i­fied) with dis­tinc­tive, large icons, also it shows you an on-the-fly diff of what changed in any given file, and it’s blaz­ing fast. Mostly the read­abil­ity ben­e­fits are why I stick to the UI for this operation.

Visual Stu­dio Git Source Con­trol Provider

In addi­tion to Git Exten­sions I use another tool called Git Source Con­trol Provider which plugs into stan­dard Visual Stu­dio VCS provider mech­a­nism to give me some addi­tional func­tion­al­ity (you can get the tool via Visual Stu­dio exten­sion manager).

git_source_control_provider

There are a few use­ful capa­bil­i­ties pro­vided by this tool that I tend to rely on quite often (there are more than that as you can read on the tool’s page):

  1. Over­lay icons show­ing you sta­tus of each file in Solu­tion Explorer.
  2. It shows you name of the cur­rent branch in the Solu­tion Explorer bar at the top (see “(mas­ter)” on the screen­shot below), and you will work a lot with branches in Git.
  3. It gives some addi­tional options in the con­text menu.

git_source_control_provider_screenshot

This (plus com­mand line) makes the job very, very sim­ple and quick, and that’s what I stick to on my machine.

There’s one more thing that makes work­ing with Git a plea­sure (espe­cially if you’re work­ing on a team that’s not com­pletely co-located).

Github

I love Github. It has a very clean, sim­ple inter­face that makes going through project his­tory, diff­ing com­mits and code reviews a very sim­ple and fric­tion­less process.

 

Sum­mary

Yes, per­haps those tools lack some eye candy that other tools have but frankly – I don’t care, and nei­ther should you. They are more than enough to let you quickly do what­ever you need to do with your code and don’t stand in your way. And that’s what a good VCS and tool­ing around it should be – some­thing you don’t really have to think about and you can rely on to keep track of what is hap­pen­ing with your code with con­fi­dence. And that’s pre­cisely what Git is – so if you’ve been held back, go ahead, install those tools and give Git a shot – you won’t look back.

Lock-free thread safe initialisation in C#

Wednesday, January 5th, 2011

I used to do quite a lot of con­cur­rent pro­gram­ming early in my career. Recently I've been work­ing again on opti­mis­ing a large piece of code to behave bet­ter when ran in con­cur­rent envi­ron­ment and I must share a secret with you — I had a great fun. It all started with a semi-serious chal­lenge from a friend, and ended up con­sum­ing big part of my Christmas-new year break.

One of the prob­lems I found, was that a group of objects that was being used on mul­ti­ple threads had to be ini­tialised in a thread safe man­ner — that is the ini­tial­i­sa­tion code had to be ran just once, atom­i­cally, on a sin­gle thread. How­ever after­wards it could be safely exe­cuted con­cur­rently on mul­ti­ple threads. This was being accom­plished by using code sim­i­lar to following:

public void Foo GetInitializedFoo()
{
   if(this.fooIsInitialized) return foo;
   lock(lockObject)
   {
      if(this.fooIsInitialized) return foo;

      InitializeFoo();
      return foo;
   }
}

As you can see the code usu­ally works lock-free, and only the first time when foo is not ini­tialised it has to lock to per­form the ini­tial­i­sa­tion algo­rithm. The rea­son that was sub­op­ti­mal is that in heav­ily multi-threaded sce­nar­ios it yielded sub­op­ti­mal per­for­mance when mul­ti­ple threads were try­ing to ini­tialise foo.

Imag­ine there are four threads. First of them suc­ceeds in obtain­ing the lock, and goes ahead to per­form the ini­tial­i­sa­tion, while the other two go into the wait­ing queue and sleep there wait­ing for the lock to be freed. The first thread fin­ishes the ini­tial­i­sa­tion pretty soon, but by the time the first thread wakes up already quite a lot of time is wasted. Then the sec­ond thread obtains the lock, but the remain­ing two, even though the vari­able is already ini­tialised and there­fore safe to be read con­cur­rently, still wait in the lock queue. The three remain­ing threads could exe­cute all at once now, yet they will be woken up and exe­cuted each in turn.

Mad sci­en­tist solution

The prob­lem was that using lock proved to be sub­op­ti­mal for the prob­lem at hand. To try to address this (and to make my inner geek happy) I decided to look at mak­ing this code per­form better.

While .NET frame­work library has lots of syn­chro­ni­sa­tion prim­i­tives none of them really seemed to be any bet­ter suited for the job then good old Mon­i­tor. I didn't want to put wait­ing threads to sleep imme­di­ately, as this is quite an expen­sive oper­a­tion (and so is wak­ing them up back again), and the ini­tial­i­sa­tion would usu­ally be per­formed quite quickly. I also wanted all the wait­ing threads to be able to exe­cute con­cur­rently with no syn­chro­ni­sa­tion after the first one of them fin­ished ini­tial­i­sa­tion. To accom­plish this I came up with the fol­low­ing solution:

public sealed class ThreadSafeInit
{
	// We are free to use negative values here, as ManagedTreadId is guaranteed to be always > 0
	// http://www.netframeworkdev.com/net-base-class-library/threadmanagedthreadid-18626.shtml
	// the ids can be recycled as mentioned, but that does not affect us since at any given point in time
	// there can be no two threads with the same managed id, and that's all we care about
	private const int Initialized = int.MinValue + 1;
	private const int NotInitialized = int.MinValue;
	private int state = NotInitialized;

	public void EndThreadSafeOnceSection()
	{
		if (state == Initialized)
		{
			return;
		}
		if (state == Thread.CurrentThread.ManagedThreadId)
		{
			state = Initialized;
		}
	}

	public bool ExecuteThreadSafeOnce()
	{
		if (state == Initialized)
		{
			return false;
		}
		var inProgressByThisThread = Thread.CurrentThread.ManagedThreadId;
		var preexistingState = Interlocked.CompareExchange(ref state, inProgressByThisThread, NotInitialized);
		if (preexistingState == NotInitialized)
		{
			return true;
		}
		if (preexistingState == Initialized || preexistingState == inProgressByThisThread)
		{
			return false;
		}

		var spinWait = new SpinWait();
		while (state != Initialized)
		{
			spinWait.SpinOnce();
		}
		return false;
	}
}

The Thread­SafeInit class man­ages lock-free access to part of code­base. It has a state int flag which starts off in a non-initialised state. Then as Exe­cuteThread­SafeOnce method, which marks the begin­ning of the ini­tial­i­sa­tion sec­tion is being called, it uses Interlocked.CompareExchange to set the flag in a lock-free thread safe manned to the id of cur­rent thread. The thread that suc­ceeds returns true and is free to go ahead and do the ini­tial­i­sa­tion. Any other thread waits for the first one to fin­ish, but instead of going imme­di­ately to sleep it spins for a while, effec­tively wast­ing cycles, and then once the first thread fin­ishes, and calls End­Thread­SafeOn­ce­Sec­tion method, all the other threads can pro­ceed in parallel.

Usage pat­tern looks some­thing like this:

public void Foo GetInitializedFoo()
{
   if(this.fooIsInitialized) return foo;
   var initialising = false;
   try
   {
      initializing = init.ExecuteThreadSafeOnce();
      if(this.fooIsInitialized) return foo;

      InitializeFoo();
      return foo;
   }
   finally
   {
      if(initialising)
      {
         init.EndThreadSafeOnceSection();
      }
   }
}

Using this code I was able to mea­sure over two-fold improve­ment over the old solu­tion in this part of code. Sure, it's a micro-optimisation and in 99.9% of cases lock would be just fine, but as I said, it was a chal­lenge, and I had a blast work­ing on this, and that's all I care about. Well, not all per­haps. If you see any flaws in this code, ways to improve it, or I just repli­cated some­thing that some­one else did much bet­ter long time ago, let me know.

C# dynamic and generics dispatch

Wednesday, December 15th, 2010

I remem­ber when C# 3 came out, I imme­di­ately started using pretty much all of the new fea­tures in it and I loved it. Imag­ine devel­op­ing with­out var, auto­matic prop­er­ties, lamb­das, exten­sion meth­ods includ­ing those in .Linq namespace.

With C# 4 it's been dif­fer­ent. Even though it's been sev­eral months since I started using Visual Stu­dio 2010 and .NET 4, except for generic vari­ance and occa­sional use of named argu­ments I hardly use any of the new fea­tures. Espe­cially, I couldn't find a good use case for the most hyped new fea­ture — the dynamic keyword.

Until today, that is.

Short intro­duc­tion to the problem

The appli­ca­tion I'm work­ing on right now, has forms (you wouldn't expect that, would you?). Each of them col­lects some data in steps and based on answers from pre­vi­ous step, its own state etc, it decides whether or not it needs to col­lect more info or "does stuff".

So the C# 3 version…

Each form (ques­tion­naire) imple­ments a com­mon inter­face and var­i­ous (mul­ti­ple!) closed ver­sion of IHandleData<TData> inter­face for each type of data it col­lects from the user. The solu­tion is very loosely generic, so the code that wires those things together does not know any details about the types it's work­ing with. All it knows is that it gets two objects — one of them is the data type, and the other imple­ments generic IHan­dle­Data closed over the for­mer type. It then has to some­how invoke the right one (of many) Han­dle method on the first object, pass­ing the sec­ond one as the argument.

To han­dle this, we came up with code resem­bling the fol­low­ing (this code is sim­pli­fied — the actual solu­tion is a tad more elaborate):

var viewType = data.GetType();
var closedType = typeof(IHandleData<>).MakeGenericType(viewType);
var handle = closedType.GetMethod("Handle");
handle.Invoke(questionnaire, new[] { data });

There's quite a lot of cer­e­mony here, to work around the fact that at com­pile time we don't have enough type infor­ma­tion (and that type infor­ma­tion are dif­fer­ent from invo­ca­tion to invo­ca­tion) to sat­isfy the compiler.

Then again in C# 4…

In sit­u­a­tions like this, involv­ing gener­ics and dynamic dis­patch, you can lever­age the new C# 4 capa­bil­i­ties, to make the code much neater and much more concise:

dynamic data = GetData();
dynamic questionaire = GetQuestionaire();
questionaire.Handle(data);

The addi­tional ben­e­fit to vastly improved read­abil­ity of the code is bet­ter per­for­mance. A use­ful thing to keep on the back of your head.

To IntelliSense or not to IntelliSense, is that even a question?

Saturday, October 23rd, 2010

UPDATE (tl;dr version):

As it appears peo­ple seem not to read the post before com­ment­ing. I’m not ques­tion­ing Intellisense’s value. I’m ques­tion­ing why .NET devel­op­ers get panic attack when some­one men­tions “let’s use X” where there’s no Intel­liSense sup­port for X.

As ASP.NET MVC team lately pushes towards third release of their frame­work includ­ing new out of the box view engine, more and more I hear peo­ple com­plain­ing about the cur­rent pre­view release of it, for one sim­ple rea­son – it does not have Intel­liSense (yet).

This reminds me of sim­i­lar approach peo­ple had for some other view engines like nhaml, NVe­loc­ity, BRail both in ASP.NET MVC and Mono­rail. This can be also extended to not using Boo which is an awe­some .NET lan­guage that should get every­one excited, but it’s not – because there’s not good tool­ing story around it Visual Stu­dio (yes, I know about the efforts to bring that to some degree).

So what’s the problem?

I ask you. Ruby com­mu­nity, which seems to be the fastest grow­ing devel­op­ment com­mu­nity at the moment uses what most .NET or Java devel­op­ers would call “prim­i­tive tools”, with hardly even syn­tax col­or­ing, yet what they seem to be empha­siz­ing the most when they com­pare Ruby to other plat­forms is pro­duc­tiv­ity gains.

So is refusal to use some­thing unless it pro­vides Intel­liSense just an inner fear in .NET devel­op­ers? I would envi­sion it to expe­ri­ence chil­dren have when they learnt how to ride a bike with addi­tional wheels on each side, and now the addi­tional wheels were taken away.

Isn’t this behav­ior as irrational?

 

[#Beginning of Shooting Data Section]
Nikon D2Xs
2009/07/03 11:30:57.3
Tiff-RGB (8-bit)
Image Size: Large (4288 x 2848)
Color
Lens: 24-70 mm F/2.8 G
Focal Length: 58 mm
Exposure Mode: Manual
Metering Mode: Multi-Pattern
1/180 s - F/11
Exposure Comp.: 0 EV
Sensitivity: ISO 100
Optimize Image:
White Balance: Color Temp. (4800K)
Focus Mode: AF-S
VR Control: OFF
Long Exposure NR: OFF
High ISO NR: Off
Color Mode: Mode II (Adobe RGB)
Tone Comp.: Normal
Hue Adjustment: 0¡
Saturation: Normal
Sharpening: Medium high
Flash Mode:  
Flash Exposure Comp.:  
Flash Sync Mode:  
Image Authentication: OFF
Image Comment: (C)2008 HALL IMAGE PHOTOGRAPHY      
[#End of Shooting Data Section]

ConcurrentDictionary in .NET 4, not what you would expect

Friday, August 6th, 2010

.NET 4 brings some awe­some sup­port for mul­ti­threaded pro­gram­ming as part of Task Par­al­lel Library, Con­cur­rent col­lec­tions and few other types that live now in the framework.

Not all behav­iour they expose is… not unex­pected thought. What would be the result of the fol­low­ing code:

private static void ConcurrentDictionary()
{
	var dict = new ConcurrentDictionary<int, string>();
	ThreadPool.QueueUserWorkItem(LongGetOrAdd(dict, 1));
	ThreadPool.QueueUserWorkItem(LongGetOrAdd(dict, 1));
}

private static WaitCallback LongGetOrAdd(ConcurrentDictionary<int, string> dict, int index)
{
	return o => dict.GetOrAdd(index,i =>
										{
											Console.WriteLine("Adding!");
											Thread.SpinWait(1000);

											return i.ToString();
										}
					);
}

We call GetOrAdd method from Con­cur­rent­Dic­tionary<, > on two threads try­ing to con­cur­rently obtain an item from the dic­tio­nary, cre­at­ing it using pro­vided del­e­gate in case it’s not yet present in the col­lec­tion. What would be the result of run­ning the code?

Sur­pris­ingly both del­e­gates will be invoked, so that we’ll see the mes­sage “Adding!” being printed twice, not once. This unfor­tu­nately makes the class unus­able for a whole range of sce­nar­ios where you must ensure that cre­ation logic is only ran once. One sce­nario where I wished I could use this class was Cas­tle Dynamic Proxy and its proxy type caching. Cur­rently it uses coarse grained dou­ble checked lock­ing. The code could ben­e­fit from more ele­gant API like the one exposed by Con­cur­rent­Dic­tionary and fine grained locking.

How­ever looks like Con­cur­rent­Dic­tionary is not an option, not unless cou­pled with Lazy<>, which while I sup­pose would work, is far less ele­gant and would not per­form so good, since both classes would use their own, sep­a­rate lock­ing, so we’d end up using two locks instead of one, and hav­ing to go through addi­tional, unnec­es­sary layer of indirection.

So the mes­sage of the day is – new con­cur­rent classes in .NET 4 are awe­some but watch out for issues anyway.

Select is broken? (.NET 4)

Thursday, April 15th, 2010

Daniel pinged me today that he stum­bled upon odd issue while try­ing to update Moq to use Cas­tle Dynam­icProxy 2.2. I inves­ti­gate a bit more and it appears to be one of these this-cannot-be-happening-select-is-broken situations.

When Dynam­icProxy tries to repli­cate an attribute on one method, its Cus­tom­At­trib­ute­Data con­tains con­tra­dic­tory infor­ma­tion. It hap­pens only when run­ning on .NET 4 (the method in ques­tion does not have the attribute in pre­vi­ous ver­sions of BCL)

Select is broken

That’s the method in ques­tion in Reflector:

Reflector

Here’s sim­pli­fied code sam­ple that repro­duces the issue:

var method = typeof(HtmlInputText).GetMethod("GetDesignModeState", BindingFlags.Instance | BindingFlags.NonPublic);

Debug.Assert(method != null, "method != null");

var attributes = CustomAttributeData.GetCustomAttributes(method);

Debug.Assert(attributes.Count == 1, "attributes.Length == 1");

var attribute = attributes[0];

Debug.Assert(attribute.Constructor.GetParameters().Length == attribute.ConstructorArguments.Count, "This fails");

 

Now – what am I miss­ing? Is this really bug in BCL v4 or am I doing some­thing wrong here?

Validate your conventions

Sunday, March 21st, 2010

I'm a big pro­po­nent of the whole Con­ven­tion over con­fig­u­ra­tion idea. I give up some metic­u­lous con­trol over plumb­ing of my code, and by virtue of adher­ing to con­ven­tions the code just finds its way into the right place. With great power comes great respon­si­bil­ity, as some­one once said. You may give up direct con­trol over the process, but you should still some­how val­i­date your code adheres to the con­ven­tions, to avoid often sub­tle bugs.

So what is it about?

Say you have web, MVC appli­ca­tion, with con­trollers, views and such. You say con­trollers have a layer super­type, com­mon base name­space and com­mon suf­fix in class name. Great, but what hap­pens if you (or the other guy on your team), fail to con­form to these rules? There's no com­piler to raise a warn­ing, since the com­piler has no idea about the rules you, (or some­one else) came up with. What hap­pens is, most likely your IoC con­tainer if you use one, or per­haps rout­ing engine of your appli­ca­tion will fail to prop­erly locate the type, and the appli­ca­tion will blow into your face dur­ing a demo for very impor­tant cus­tomer.
Take another sce­nario. Say you're using AutoMap­per, and you came up with con­ven­tion that enti­ties in name­space Foo.Entities are mapped to DTOs in name­space Foo.Dtos, so that Foo.Entities.Bar gets its map­ping to Foo.Dtos.BarDto reg­is­tered auto­mat­i­cally. Again — it won't pick map­ping to Fizzbuz­zDto when you acci­den­tally put Fizzbuzz entity in Foo.Services. Depend­ing on your test cov­er­age you will either find out sooner or later.

These are usu­ally a hard rules, and both AutoMap­per and most IoC frame­works will be able to fig­ure out some­thing is miss­ing and notify you (with an excep­tion). This is a good thing, since you fail fast, and quickly get to fix your rules. You won’t be that lucky all the time though. Say you’re writ­ing an MVVM frame­work, sim­i­lar to one Rob built, and cre­ate  a con­ven­tion wiring enable state of some con­trols bound to method Exe­cute, with prop­erty CanEx­e­cute on your view model (if you have no idea what the heck I’m talk­ing about, go see Rob’s pre­sen­ta­tion from Mix10). When you fail to con­form to this rule, no excep­tion will occur, world will not end. Merely a  but­ton will be click­able all the time, instead of only when some valid­ity rule is met. This is a bug though, just one harder to spot, which means usu­ally it won’t be you who finds it – your angry users will.

So I have a problem?

So what to do? You can loosen your con­ven­tions. If you had an issue due to name­space mis­match, per­haps don't require name­spaces to match, just type names? Or if you for­got to put a Con­troller suf­fix on your con­troller, just ditch that require­ment and it will work just fine, right?

Yes it will (prob­a­bly), or at least you find your­self break­ing some other require­ment of your con­ven­tion and feel the need to shed it as well. This how­ever does not fix the prob­lem — it merely replaces it with another — you will have rot­ten code.

Prob­lem is not that your con­ven­tion was too rigid (although some­times this will indeed be the case as well), you not adher­ing to the con­ven­tion was the real prob­lem. Con­ven­tions are like com­piler rules — fol­low them or don't con­sider your code 'work­ing'. To get compiler-like behav­ior you need to take this one step fur­ther and along with find­ing code adher­ing to your con­ven­tions, find the code that does not.

Take the AutoMap­per exam­ple again. We have des­ig­nated name­space for our con­ven­tion — Foo.Dtos. For each type in this name­space we require that a match­ing type in Foo.Entities name­space exists. You should check if an unmatched dto type exists and send some noti­fi­ca­tion in case there is. Also since you require DTOs and only DTOs to live in that name­space you should express that require­ment in code — check for types with Dto suf­fix in other name­spaces, and types with­out this suf­fix in Foo.Dtos name­space, and in each case send an appro­pri­ate noti­fi­ca­tion as well.

This is a form of exe­cutable spec­i­fi­ca­tion. When a new guy/gal comes to your team and starts work­ing on the code base you don't have to worry about them break­ing the code because they don't know the con­ven­tions you fol­low. The code will tell them when some­thing is wrong, and what to do to make it right. It will also help you keep your code clean and struc­tured. Improv­ing its read­abil­ity and in the long run, also decreas­ing pos­si­bil­ity of code rot.

What to do?

I still haven't touched upon one impor­tant thing — how and where do you imple­ment the con­ven­tion val­i­da­tion rules. There are three places I would put con­ven­tion validation.

  • The boot­strap­ping code itself. It often makes sense to take advan­tage of the fact you're scan­ning for con­ven­tions to per­form the con­ven­tions boot­strap­ping and do check for misses as well. Espe­cially when the frame­work you're work­ing with does not have any sup­port for con­ven­tions built in and you're doing it all manually.
  • Unit tests. Add tests that call AutoMapper’s ‘Assert­Con­fig­u­ra­tionIs­Valid’ or in case of home­grown frame­work, reflect over your model and look for devi­a­tions from your con­ven­tions. It really is very lit­tle work upfront com­pared to mas­sive return it gives you.
  • NDe­pend. NDepend's CQL rules lend them­selves quite nicely to this task. They work in a sim­i­lar man­ner to unit tests, but they are exter­nal to your code. Espe­cially with very nice Visual Stu­dio inte­gra­tion in ver­sion 3, it has very low fric­tion, and pro­vides imme­di­ate feed­back. In addi­tion they are pretty easy to read, lend­ing them­selves to the task of being exe­cutable documentation.

Solving a programming puzzle

Monday, August 10th, 2009

My fel­low devlicio.us blog­ger Tim Barcz posted an inter­est­ing prob­lem to solve. I think it's fun, so I decided to give it a try. Now, I don't know enough con­text to solve it for just about any case (and I don't think that's even pos­si­ble), so I made a few assumptions.

  • The strings we're gonna be han­dling are not too long. Oth­er­wise I'd prob­a­bly use a StringReader/Writer.
  • I'm look­ing at min­i­mal solu­tion. YAGNI — I could do some opti­miza­tions prob­a­bly, but I want to keep it sim­ple, because the sam­ple prob­lem (just a hand­ful of char­ac­ters) is simple.
  • Solu­tion is not con­text spe­cific. In other words, if you want to swap char­ac­ters dif­fer­ently based on con­text, it won't do it. Again, I'm keep­ing it simple.
  • I'm using a fac­tory here, to cre­ate the cleaner and feed it with map­ping but based on the needs and tools avail­able, I'd prob­a­bly use an IoC con­tainer to get the instance, and if needed get the map­ping from an exter­nal file, where it's more read­able, and can be eas­ily changed.

Now, here's the code:

public string CleanInput( string input )
{
    StringCleaner cleaner = CleanerFactory.BuildCleaner( input );
    return cleaner.Clean( input );
}

The Clean­er­Fac­tory is not impor­tant. All it does is con­struct an instance of a String­Cleaner and feed it with the map­ping. How­ever, I'll post it for completeness:

public static class CleanerFactory
{
    public static StringCleaner BuildCleaner( string input )
    {
 
        var cleaner = new StringCleaner();
        BuildMapping( cleaner );
        return cleaner;
    }
 
    private static void BuildMapping( StringCleaner cleaner )
    {
        /*
         *  Character         Correct ASCII Value     Bad Values
            Hyphen             45                         8208, 8211, 8212, 8722, 173, 8209, 8259
            Single Quote     39                         96, 8216, 8217, 8242, 769, 768
            Double Quote     34                         8220, 8221, 8243, 12291
            Space             32                         160, 8195, 8194
         */
        cleaner.AddMapping( (char) 45, (char) 8208, (char) 8211, (char) 8212, (char) 8722, (char) 173, (char) 8209, (char) 8259 );
        cleaner.AddMapping( (char) 39, (char) 96, (char) 8216, (char) 8217, (char) 8242, (char) 769, (char) 768 );
        cleaner.AddMapping( (char) 34, (char) 8220, (char) 8221, (char) 8243, (char) 12291 );
        cleaner.AddMapping( (char) 32, (char) 160, (char) 8195, (char) 8194 );
    }
}

The cleaner itself looks like this:

public class StringCleaner
{
        private IDictionary<char,char> mappings = new Dictionary<char, char>();
 
        public string Clean( string input )
        {
            if( string.IsNullOrEmpty( input ) )
            {
                return string.Empty;
            }
 
            var buffer = new StringBuilder( input );
            for( int i = 0; i < buffer.Length; i++ )
            {
                var character = buffer[i];
                char validCharacter;
                if( this.mappings.TryGetValue( character, out validCharacter ) )
                {
                    buffer[ i ] = validCharacter;
                }
            }
 
            return buffer.ToString();
        }
 
        public void AddMapping( char validValue, params char[] invalidValues)
        {
            if( invalidValues == null )
            {
                throw new ArgumentNullException( "invalidValues" );
            }
 
            foreach( var invalidValue in invalidValues )
            {
                this.mappings[ invalidValue ] = validValue;
            }
        }
}

Since strings are immutable, I'm using a String­Builder to iter­ate over all the char­ac­ters in the string, and replace these I want. I'm using Dic­tio­nary for the map­ping, because it has a read­able API, and O(1) search time.