Noocyte’s Weblog

August 27, 2009

DRY in the real world

Filed under: Work — Tags: , , — noocyte @ 9:24

In programming there are many, many principles. One of the more useful ones that I’ve discovered lately is DRY: Don’t Repeat Yourself. It’s simple really, never ever copy code and don’t write the same code twice. In the real world it might not be as easy though…

Today I was about to check in some code and I went through my regular check-in routine (you have one as well I presume?) and discovered that I was violating DRY! At least if you’re being anal about it… The code in question was a little like this: (slightly altered to protect the innocents)

   1: using (SPSite site = new SPSite(url)) {

   2:     using (SPWeb web = site.OpenWeb()) {

   3:         lang = web.Language;

   4:     }

   5: }

(I didn’t really return the language, but no need to include all of the source code)

So I was doing the double using in 4-5 places; a clear violation of DRY in my book. So what I did was this:

   1: private T OpenSiteWeb<T>(string url, Func<SPSite, SPWeb, T> implementingMember) {

   2:    using (SPSite site = new SPSite(url)) {

   3:        using (SPWeb web = site.OpenWeb()) {

   4:            return implementingMember(site, web);

   5:        }

   6:    }

   7: }

This method I can use like this:

   1: lang = OpenSiteWeb<uint>(url, (s, w) =>

   2: {

   3:     return w.Language;

   4: }

Pretty neat, no? It might be overkill, but fun code to write anyway!

February 18, 2008

Double Key Dictionary

Filed under: Personal — Tags: , — noocyte @ 23:16

Double Key Dictionary, or; how to make it really simple to use a dictionary with two keys.

In a matter of just weeks I found myself forced to temporarily store values with two unique identifying keys. As an example think about a customer in a web shop who has bought some books. Key number one would be the customer ID and then key number two would be the books ISBN number. The value stored could be the books name. Why you would want to do this I have no idea, but it’s an easy to understand example and it illustrates the problem I had so that everybody can understand it. Its just that I read values from, literally, thousands of Xml logs stored in a database… Anyway!

So what you could do is something like this:

Dictionary<string, Dictionary<string, string>> bookList = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> innerList = new…
innerList.Add(“11-22-33”, “Lord of the Rings”);
bookList.Add(“James”, innerList);

That works and would perhaps be good enough. But what happens if you want to compare two lists of books? And it’s not unthinkable that you might end up trying to add the same book, for the same person, more than once. How do you handle that? Lots of manual checks? More on my solution later on…

Even if I could make the problems outlined above go away (as in create little helper methods to sort them out) I just didn’t like the syntax. It felt just so wrong. What I wanted would be something like this:

DoubleKeyDictionary<string, string, string> bookListEx = new DoubleKeyDictionary<string, string, string>();
bookListEx.Add(“James”, “11-22-33”, “Lord of …”);

(Abbreviated for clarity)

So I implemented the DoubleKeyDictionary, as a two key generic dictionary. My implementation also includes IEnumerable<T>, IEquatable<T> and I added an index. All this enables me to use this syntax:

DoubleKeyDictionary<string, string, string> bookListEx = new DoubleKeyDictionary<string, string, string>();
bookListEx.Add(“James”, “11-22-33”, “Lord of the Rings”);

DoubleKeyDictionary<string, string, string> bookList = new DoubleKeyDictionary<string, string, string>();
bookList.Add(“James”, “10-20-30”, “Narnia”);

if (bookList.Equals(bookListEx))
    Console.WriteLine(“They are equal”);

foreach (DoubleKeyPairValue<string, string, string> books in bookListEx)
    Console.WriteLine(books.ToString());

Sorry about the bad formatting. Please download the actual code file [new window]. Please feel free to use the code if you like it. If not; tell me why!

November 9, 2007

NDepend

Filed under: Link, Work — Tags: , — noocyte @ 15:24

 Looks really cool! I’ll have to try it out someday

Link to NDepend

Create a free website or blog at WordPress.com.