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:
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?
You can also benefit from #2 in standardizing your exception handling to some degree across multiple projects; the abstraction also enables the library to be enhanced at a latter time and redistributed e.g. error logging for instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With