Brendan Enrick's Blog

Daily Software Development.


Parameter Order Should Be Consistent

I was looking at a piece of code recently and noticed that someone was checking for a null parameter and throwing an ArgumentException, so I figured since ArgumentNullException inherits from ArgumentException I would just replace it with the more specific exception. Little did I know that Microsoft has a wee little bit of a problem with consistency on these.

With ArgumentExceptions it is important to have a message as well as specify which parameter is causing the trouble, so there are 2 string parameters in the ArgumentException constructor. What you might not know if you haven't switched one to the other is that they changed the order of those parameters. What I mean is that you call them like this.

throw new ArgumentException("The Message", "paramName");
throw new ArgumentNullException("paramName", "The Message");
throw new ArgumentOutOfRangeException("paramName", "The Message");

How can they possibly switch those?!?! If I am not mistaken the lower two exceptions actually inherit from the first one, so it is quite surprising that the ordering was not maintained in the second two. I need to keep an eye out for other such inconsistencies while I am working. They obviously can't easily fix it now, because too much code depends on the current ordering. I try to maintain consistence parameter ordering, but I am also not working on a framework.

kick it on DotNetKicks.com

Trackbacks & Pingbacks

Reflective Perspective - Chris Alcock » The Morning Brew #275 — 29 Jan 2009 6:20 AM

Pingback from Reflective Perspective - Chris Alcock » The Morning Brew #275


Parameter Order Should Be Consistent : Brendan Enrick's Blog — 29 Jan 2009 9:29 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout


Comments

 avatar

Patrik Hägne said on 28 Jan 2009 at 3:59 PM

I've noticed this one to, really weird.

 avatar

Kampanye Damai Pemilu Indonesia 2009 said on 13 Apr 2009 at 3:51 AM

i want to say very thank you for this great informations. now i understand about it.

Comments are closed.