Thursday, July 29, 2010

Moving on ...

It's that time again, I am moving on to a new blog host. I want to keep the old blog in place and start a new one. You can now find me at http://5pm.zwares.com If you listen to me on the RSS radio, there's no change. You will still find me on the same frequency http://feeds.feedburner.com/5pmPosts Thanks!

Wednesday, August 5, 2009

When not to use method overload

Method overloading is taught in OOP 101, but there are times when it should be avoided like plague. Let me explain ...

During a recent implementation of Caching solution, I had to create a CacheManager which could support the creation and management of different types of Cache types - say file system based, memory based, db based etc. Each cache type comes with different options. For example, file based system could monitor a file as a dependency, which the DB based cache does not.

A typical example of different overloads is as follows ...

interface ICache {
    void Add<T>(string key, T value)
    void Add<T>(string key, T value, string filename);
    void Add<T>(string key, T value, DateTime absoluteExpiration);
    void Add<T>(string key, T value, CacheDependency dependencies);
    void Add<T>(string key, T value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
    void Add<T>(string key, T value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
    CacheItemUpdateCallback onUpdateCallback);
    void Add<T>(string key, T value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
    CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
}

So how do we create an efficient caching solution without creating overloads for each supported options by a cache type.

The solution is to create a CacheOptions class that has a union of all options supported by all the cache types. So the example above can be rewritten as follows ...

interface ICache {
    void Add<T>(string key, T value, CacheOptions options);
}
class CacheOptions {
    public string FileName { get; set; }
    public DateTime AbsoluteExpiration { get; set; }
    public TimeSpan SlidingExpiration { get; set; }
    public CacheDependency Dependecy { get; set; }
    public CacheItemPriority priority { get; set; }
    public CacheItemUpdateCallback OnUpdateCallback { get; set; }
}

This does a few things for us ...

  1. Simplify ICache interface, let each implementation work with the CacheOptions
  2. Everytime we have a new Cache type, we don't have to modify the interface ICache, and thereby the other implementations
  3. We can also initialize (with non-null values) some properties which are optional parameters. This results in cleaner code ...

Before ...

SomeCahce.Add("key", new object(), new CacheOptions() { AbsoluteExpiration = DateTime.MaxValue });

After ...

class CacheOptions {
    public CacheOptions() {
        // Default values
        AbsoluteExpiration = DateTime.MaxValue;
    }
    ...
}
SomeCahce.Add("key", new object(), new CacheOptions());

Wednesday, July 29, 2009

Using Lambda Expressions for Events

In the past, we created events using the following format …

public delegate void EventListener(object src, object context);
public event EventListener SomeEvent;
public void Init()
{
SomeEvent += OnSomeEvent;
}
public void OnSomeEvent(object src, object context)
{
}

With lambda expressions, we can do the following …

public event Action<object, object> SomeEvent;
public void Init()
{
SomeEvent += (s,c) => {};
}

No doubt there are other ways to do it, but I find this simpler and more concise.

Friday, July 24, 2009

Interesting reading on Web Gardens

If you want to learn more about Web Gardens, and gains and issue you might face, read these articles.

Available right-clicking an application pool and choosing Properties in the IIS Management snap-in.

One good learning from these articles was "Web gardening can be a good way to see if your application will be able to function in a Web farm scenario on multiple servers reasonably well"

Monday, March 16, 2009

Hotmail R.I.P. (1996-2009)

Well not dead yet, but might as well be.

I have been using Hotmail since 1998, and have been using it till yesterday. As of today, I am proud to say that I am completely Hotmail-free. I am a pure GMail-er, all 100%.

There is no doubt that GMail has had far superior features as compared to Hotmail or Yahoo mail, two of the most popular free providers. So what took me so long to make the switch? Two things...

  • Old emails in Hotmail
  • Inability to mass update my email addresses without causing inconvenience, both to me and to others

I had previously posted (10 Indispensable 'GMail IMAP With Outlook' Hidden Features) a way to import files using GMail's IMAP service. But there was no way to import mails from Hotmail, well none until recently. Microsoft recently opened POP3 access to all Hotmail accounts (see gHacks).

So lets see what needs to be done for the move ...

  1. Open Settings > Accounts
  2. Click on 'Add a mail account you own'
  3. Enter the following details...
  4. Save

That's it, it should now import you Hotmail mail. Import one folder at a time and label them accordingly. Also GMail will let you reply to mail from Hotmail with either your Hotmail address or your GMail address. This ensures that people can continue sending mail to Hotmail or if you desire, to you GMail account from that point on.

Here are the settings in text form...

  • Incoming Server: pop3.live.com
  • Incoming Port: 995
  • SSL Encryption: yes

Not used for GMail, but rest of the settings ...

  • Outgoing Server: smtp.live.com
  • Outgoing port: 25 (use port 587 if the default port is not working)
  • Authentication: yes
  • TLS Or SSL: yes

Friday, March 13, 2009

Operator Overloading - Check for null value

Height implements the operators, here is the current implantation of == operator …

public static bool operator ==(Height a, Height b)
{
  return a.CMValue == b.CMValue;
}

#1 – What’s wrong with this code?

What happens if you want to do the following:
Height a = new Height(100);
Height b = null;
a == b

This will result in NullReferenceException … bad bad bad

Ok lets fix this …
public static bool operator ==(Height a, Height b)
{
  if( a == null && b == null) return true;
  if( a == null || b == null) return false;
  return a.CMValue == b.CMValue;
}

#2 – What’s wrong with this code?

Do you see the recursiveness here?
a == null will call the same implementation, resulting in NullReferenceException … bad bad bad

Ok lets fix this…
public static bool operator ==(Height a, Height b)
{
  object _a = (object)a, _b = (object)b;
  if (_a == null && _b == null) return true;
  if (_a == null || _b == null) return false;
  return a.CMValue == b.CMValue;
}

By converting a and b to objects, we are using object’s implementation of the == operator, thus avoiding a recursive call.

Tuesday, September 9, 2008

Get ready for CERN: Talk on Particle Physics

This is a follow up to the last video, to take a step further into particle physics. Get ready for the historic CERN supercollider's first beam on Sept 10th 2008. Watch Brian Cox explain the reason behind CERN.