I had a discussion today about refactoring this (#1)
public void MyFunc(object myArgument)
{
if(myArgument == null)
throw new ArgumentNullException("myArgument");
....
With this (#2)
//inside a shared assembly in a class called Guard
public static void AgainstArgumentNull(object obj, string message)
{
if (obj == null)
throw new ArgumentNullException(message);
}
public void MyFunc(object myArgument)
{
Guard.AgainstArgumentNull(myArgument, "myArgument");
....
My intuition was that #1 was better for the following reasons:
- #1 is simpler than #2 in the sense that it requires no knowledge of Util library, just basic c# knowledge
- #1 will not remove the resharper ability to rename the string passed to ArgumentNullException constructor.
- #2 will increase the dependencies for the code (must have access to the dll containing the dll)
- The stacktrace will not be the same for #2 as it would be for #1
My questions here are:
Is my intuition correct?
Could the fact that we are throwing the exception from another assembly not become trouble in some scenarios?
For this particular case, you shouldn’t be using
Guardanyway. You should be using Code Contracts.