Stop Asserting Arguments by Name
Asserting arguments is essentially a healthy practice. What I don’t like, is the fact that since day one ArgumentNullException used a string for argument name, and now with .NET 4.5 almost knocking on the door, there’s still only string option.
if (x == null) throw new ArgumentNullException("Value was null", "x");
Code Contracts introduced a while ago, has an implementation for Requires precondition that is good, yet still has the same issue.
Contract.Requires<ArgumentNullException>( x != null, "x" );
Many other frameworks still rely on string as a source of the argument name for assertions these frameworks make (Sitecore as a such that I have recently used).
Sitecore.Diagnostics.Assert.ArgumentNotNull(args, "args");
Why not to add an expression support and be done with it? This way, when you refactor code and “accidentally” rename your argument, assertion message is not going to lie. I’ve being using this code with assertions created by 3rd party:
public static string Get_Name(Expression<Func<object>> func) { return (func.Body as MemberExpression).Member.Name; }
And no worries about re-naming my arguments.