I came across an interesting scenario recently. I am implementing a cache of sorts in some code.

As an aside, this is sort of a wild-west code base - shoot first, ask questions later type of thing. The kind of scenario I've tried to stay away from but business is a funny thing.  Anyway, I mention that because the caching system basically tried to find an entry in the cache and if it doesn't find one it returns null or otherwise the thing it found. 

Now that we've been firing a few rounds from the hip and the fights are now starting to die down. This code started to backfire like that banged up fender-bender you used to hear coughing past you as a kid: Code that expected cache entries to get fetched and retrieved would blow up when they somewhere down the track a null was returned from the cache. That type of thing. 

Actually, now that I come to think about, I don't think nulls were actually ever returned at all. In fact, it was something like we'd always get something from the cache (even if it was an incomplete cache entry which represented a previous cache-miss - I know crazy, what can I say.

So anyway, the problem was when code tried to work on incomplete cache entries, things started to 'blowed-up' (I like this expression -  heard a colleague say that in a stand-up this one time). The solution, well if you want to call it a solution, was to return a nullable cache entry when the entry was not found and stop putting incomplete cache entries in to begin with (that was a stupid idea). 

This meant that the caller looking for a cached item would need to check if the item was found or if the item was null(instead of assuming it would also get something all the time even if it was incomplete). I've already talked about how stupid that was but I digress...

So now, you get an item or you get a null, that means if its a null, the item was not found in the cache. The problem is that most of the code that used the cache now had to check if the item was null if it wasn't then use it, otherwise do something else. So the return type was Nullable<CacheEntry> and you found this a lot:

if (cacheEntry.HasValue) ...

Which is boring after the 3rd time of typing. So while we're in the wild west, I thought I'd made my life easier.

Make an extension method on any nullable type like this to prevent the repetitive carpel-syndrome-inducing code:

 public static class Extensions
    {
        public static bool Is<T>(this T? type, bool expected) where T : struct
        {
            if (type.HasValue && type.Value.Equals(expected)) { return true; }
            return false;
        }
    }

I explained what extension methods were here: Understanding how the Linq query syntax works

So now you can do this even if the data behind the expression was null:

if (Helper.GetInstance().IsPortfolio(statementRetrievalContext.Portfolio).Is(true)
{
...

Now, it's not the smartest code in the west, but heck out here anything goes!

Yee-ha!


Comments powered by CComment

Twitter