Object Dumper: An Invaluable Tool for Writing Code in the Functional Programming Style

When developing C# programs in the functional programming style, you often need to dump out a collection to the console.  Object dumper is a great tool to use for this.  It is a sample that far too few developers know about.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOCA functional transform typically takes the form of successive transformations: transform collection a => collection b => collection c => final collection.  This is one of the main points of this topic in my Functional Programming Tutorial.

When I commence developing such a transform, I first develop a model in my mind of these transforms.  Sometimes (particularly when writing ad-hoc transformations) I don’t fully design each transform before starting coding.  For instance, when the source collection is particularly messy, I first write a transform to simply clean up the data and transform it into a more usable form for subsequent transformations.  Sometimes I do a transform to an anonymous type, and sometimes I know straight away that I’m going to want to transform into a named type.  Regardless of which approach I take, I validate that my transform is as I want it before coding up the next transform.  The object dumper is my preferred tool for this.

One note: I typically don’t use the debugger very much when writing these transformations.  I write a transform, validate the results, write the next transform, validate again, and so on.  Due the lazy evaluation, using the debugger is necessarily not always convenient.  So I don’t worry about it, and just use Object Dumper.

To use it, just add ObjectDumper.cs to your project.

For the simplest use, you can simply pass any object to ObjectDumper:

var z = new
{
A = "1",
B = "2"
};

ObjectDumper.Write(z);

This produces the following output:

A=1 B=2

Sometimes you have nested collections:

var z = new
{
Aaa = "Hello",
Bbb = "There",
Ccc = new[] { 1, 2, 3 }
};

ObjectDumper.Write(z);

This outputs:

Aaa=Hello Bbb=There Ccc=...

In this case, it would be useful to see the nested collection, so you can pass an additional argument to ObjectDumper that tells it to dump the first level of the nested collection:

var z = new
{
Aaa = "Hello",
Bbb = "There",
Ccc = new[] { 1, 2, 3 }
};

ObjectDumper.Write(z, 1);

This produces the following:

Aaa=Hello Bbb=There Ccc=...
Ccc: 1
Ccc: 2
Ccc: 3

You can find Object Dumper in the CSharpSamples.zip file that is installed with Visual Studio 2008.

C:Program FilesMicrosoft Visual Studio 9.0Samples1033CSharpSamples.zip

Because I do a lot with SharePoint and Hyper-V, I run the 64 bit version of Windows Server 2008 on my development machine, so in my case, CSharpSamples.zip is at:

C:Program Files (x86)Microsoft Visual Studio 9.0Samples1033CSharpSamples.zip