Blogs

Paul Kimmel's Blog

For Fun and Profit (Well, Profit Maybe Not So Much)

     

I suspect that out there in Internet-land I have many as yet unmet kindred spirits. By this I mean programmers that I haven't met that bend and twist what a language can do just for fun. (You know the kind of activity that might cause a normal person to tell you to get a life.) Some people drink wine, some watch Adam Lambert on American Idol (hey his spin on Dilana Smith's spin on Johnny Cash's Ring of Fire is awesome if you like Nine Inch Nails), some fly planes, write poetry, cook, play instruments, take TaeKwonDo lessons with Master Shinn, and some, like me, like to take the language out for a spin and see what she'll do. So, if you are really serious--of the "programming is about solving problems" set or you have lost your inner child on the path of life--then you can skip this blog entry because it suffers from an excess of frivolity.

A month ago a reader responded to an article I wrote on calculating prime numbers--http://www.codeguru.com/columns/vb/article.php/c6575. In essence his first missive can be Cliff-noted as you are an idiot and your algorithm sucks! Of course, if I had wanted to start a digital bar fight, I could have called him a Nazi. Since I am only an Internet white belt I sent back a pleasant:  thank you for your feedback and thank you for taking your valuable time to improve on my grossly inferior article. Diffused he wrote back and apologized. The reader indicated that he had been refining the Sieve of Eratosthenes for 40 years--that's 4-0 years--and felt partial to it. (What I wanted to write back is if you can't do a better job than me after 40 years of effort then you are complete idiot as well as a jerk. I didn't. I didn't because I obsess about algorithms in the same way. I work and re-work algorithms dozens, if not hundreds of times. I like doing it. My girlfriend has a name for it that sounds like "asp" and "burger" . My girlfriend means "obsessing", and when I do it take it as intense interest.

One such obsessive algorithm for me is an object state dumper. I write so much code and so many tests that it is intrinsically useful to me to know what state my objects are in. (Hey, I got the idea from Dave Thielen's No Bugs!: Delivering Error-Free Code in C and C++and so did everyone else after Dave.) Consequently I have written (and published) dozens of things on an object's state dumper. (Of course, Microsoft came out with the ObjectDumper and killed my claim to fame.) And, I have re-written the state dumper several times. In this blog I thought I'd try writing a state dumper with LINQ. (I know it took forever to get to the point but writing and coding all day sometimes causes meanderitis.) Listing 1 contains an object state dumper written in LINQ. I hope you have fun with it.

Listing 1: An object state dumper courtesy the lone way around of Dave Thielen and Jeffrey Kenton.

public static void LinqDump<T>(IEnumerable<T> list, TextWriter writer)
{
  string mask = "{0} : {1}";

  var results = from obj in list
                     let properties = typeof(T).GetProperties()
                       from prop in properties
                       select
                         prop.GetValue(obj, null) != null ?
                           string.Format(mask, prop.Name, prop.GetValue(obj, null)) :
                           string.Format(mask, prop.Name, "unk.");

  Array.ForEach(results.ToArray(), s => writer.WriteLine(s));
}

The code essentially uses a nested from to iterate the list of incoming objects and then the properties to return an enumerable collection of strings stating the property name and value. The Array.ForEach sends the results out to whatever TextWriter happens to be (like Console.Out). The first drawback--among several, including esoterrorism--is that the function uses a second pass to send the results to the TextWriter. Listing 2 contains a side-effects version that uses a generic function type to display the results while iterating.

Listing 2: Using side-effects to send the value to the stream while iterating.

public static void LinqDump<T>(IEnumerable<T> list, TextWriter writer)
{
  string mask = "{0} : {1}";

    Func<string, string> sideEffects = obj =>
    {
      writer.WriteLine(obj);
      return obj;
    };

    var results = from obj in list
                       let properties = typeof(T).GetProperties()
                         from prop in properties
                         select
                           prop.GetValue(obj, null) != null ?
                           sideEffects(string.Format(mask, prop.Name, prop.GetValue(obj, null))) :
                           sideEffects(string.Format(mask, prop.Name, "unk."));

}

In Listing 2 results is wasted, but you avoid iterating over the data a second time.

OK. Time to fess up. What has any of this to do with Developer Express's controls? Nothing directly. My boss Ray said blog about LINQ because you wrote a book--LINQ Unleashed for C#--and it should be easy for you to whip out some blogs on LINQ, so I wrote about LINQ stuff. It is also worth noting that Developer Express is a great place to work, the people are smart--like Oliver Sturm, Mehul Harry, Mark Miller and others--and I have witnessed first hand how they expend a lot of energy figuring out what would make you guys more productive, do a better job, and have more fun. Since, its after 6PM I thought I would pitch in on the fun factor bit.

If you can improve on the code above--make it smaller, faster, or cooler then respond to the blog. If you are going to the Central Ohio Day of .NET in Columbus on April 18th or TechEd in LA in May then stop by the Developer Express booth and say hello.

Published Apr 01 2009, 01:27 AM by Paul Kimmel (DevExpress)
Filed under: ,
Technorati tags: LINQ, Object Dumper
Bookmark and Share

Comments

 

Mehul Harry (DevExpress) said:

Paul,

You're a smart guy and we're glad to have you on the team. And don't be hurt by that comment from Julian. He's always picking on my algorithms too. Smile

April 1, 2009 2:58 AM
 

Gabor Szabo_1 said:

Hey, you R so funny guys there in Devex team, really ;)

And nice to see some (almost :P) useful code that motivates other "cubeheads" to shovel bits. Anyway, for me as a .Net beginner, these lines help to understand some advanced technique of using LINQ and other stuff, so I'm quite interested in this kind of blog. Greetings to the great Devex team!

April 1, 2009 10:28 AM
 

Paul Kimmel (DevExpress) said:

I didn't see Julian's comment. I hope it was mercillesly cruel--keeps me honest and humble.

April 1, 2009 10:55 AM
 

Paul Kimmel (DevExpress) said:

Gabor_Szabo:

Glad you got something out of it. Thanks for reading.

April 1, 2009 1:48 PM
 

Julian Bucknall (DevExpress) said:

Mehul: Commenting about algorithms, moi? I'm shocked, shocked I tell you, that there's someone else out there who'll refine an algorithm for 40 years too, for Paul's commenter was not me.

Cheers, Julian

April 1, 2009 6:49 PM
 

Elaina said:

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Elaina

www.craigslistmaster.info

April 9, 2009 9:23 AM
 

Paul Kimmel (DevExpress) said:

Elaina: Glad to hear it and welcome.

April 9, 2009 10:03 AM
 

Sarah said:

I recently came across your blog and have been reading along. I thought I

would leave my first comment. I don't know what to say except that I have

enjoyed reading. Nice blog. I will keep visiting this blog very often.

Sarah

www.craigslistposter.info

April 11, 2009 5:25 AM
 

ytowers said:

Your bog is really high. I will ofen visit later.

April 22, 2010 5:34 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.