ArgumentNullException and refactoring
I hate using strings to represent program elements! One of the big problems is that it hinders automated refactoring. Strings don’t have any semantic meaning, so refactoring tools don’t know which references to the string need to be updated.
While playing with expression trees, a while ago I came up with this:
|
You can use this for argument validation like this:
|
This looks a little strange, but has some interesting properties:
- There are no strings, which means that refactoring tools “Just Work” and update all references.
- The majority of the reflection code happens only in the exception code, which means that this has significantly better performance than my first attempt (which took just an “Expression<Func<object>>”
On the other hand it has some drawbacks too:
- The calling side looks a little strange with “() => () =>'”
- Using this approach will lead to a LOT of compiler generated display classes to store the parameter references.
I’m not sure I would use this in production code, but it was an interesting exercise to come up with a way of validating parameters that doesn’t involve strings.
What do you think?
Comments
Anonymous
October 27, 2008
We use a similar trick for our INotifyPropertyChanged implementations for mostly the same reasons (refactoring, plus it's so easy to mistype a property name when it's a string, and the compiler doesn't check).Anonymous
October 27, 2008
If you're willing to take a small performance hit, you can skip the first 'value' argument, and replace 'value == null' with 'arg.Compile()()() == null'. This results in a cleaner usage / DRY: Argument.NotNull(() => () => args); The perf hit is the Compile() call. For me, avoiding the bug where I copy/paste one call to create other calls, and forget to replace one of the parameters is worth the tradeoff most of the time. I think I'd keep both as overloads (sharing some implementation). I'd use the 1-parameter version until performance analysis revealed the bottleneck in my code, and there switch to the other. Kevin knows all of this, of course: I'm trying to be useful to the other readers.Anonymous
October 29, 2008
The comment has been removedAnonymous
November 09, 2008
It would be nice (and much easier than the suggested code) if the compiler would support a nameof operator similar to the typeof operator. static void Main(string[] args) { if (arges == null) throw new ArgumentNullException(nameof(args)); } The nameof operator should support as many types of identifiers as possible and return the name of the identifier as a string.