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?