Brendan Enrick

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.

Comments (4) -

  • 1/28/2009 3:59:15 PM | Reply

    I've noticed this one to, really weird.


  • 1/29/2009 6:20:59 AM | Reply

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


  • 1/29/2009 9:29:27 AM | Reply

    Thank you for submitting this cool story - Trackback from DotNetShoutout


  • 4/13/2009 3:51:06 AM | Reply

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


Loading