debugcsharpMinor
Guard statement extension methods for common object guards
Viewed 0 times
statementextensionmethodsguardforobjectcommonguards
Problem
I wrote a couple of extension methods for common guard statements I write a lot. These are extension methods for Object, and allow me to quickly guard against common scenarios.
I'm using .Net 3.5 (Unity3D) so sadly I don't have access to Code Contracts in this project.
My main concern with this code is there's a lot of duplication (primarily so that I can support custom messages for when it is suitable to do so). Is there a good way I can reduce this duplication and still support optional custom messages? I intend to add more common guard methods for the
Any other comments on the code or tests are also welcome.
```
public static class ObjectExtensions
{
public static void ThrowIfNull(this object value, string argumentName)
{
if (value == null)
throw new ArgumentNullException(argumentName);
}
public static void ThrowIfNotEqual(this object value, string argumentName, object expectedValue)
{
if (!value.Equals(expectedValue))
throw new ArgumentException(argumentName, string.Format("Argument must not equal {0}", expectedValue));
}
public static void ThrowIfEqual(this object value, string argumentName, object expectedValue)
{
if (value.Equals(expectedValue))
throw new ArgumentException(argumentName, string.Format("Argument must equal {0}", expectedValue));
}
public static void ThrowIfNull(this object value, string argumentName, string customMessage)
{
if (value == null)
throw new ArgumentNullException(argumentName, customMessage);
}
public static void ThrowIfNotEqual(this object value, string argumentName, object expectedValue, string customMessage)
{
if (!value.Equals(expectedValue))
throw new ArgumentException(argumentName, customMessage);
}
public static void ThrowIfEqual(this object value, string argume
I'm using .Net 3.5 (Unity3D) so sadly I don't have access to Code Contracts in this project.
My main concern with this code is there's a lot of duplication (primarily so that I can support custom messages for when it is suitable to do so). Is there a good way I can reduce this duplication and still support optional custom messages? I intend to add more common guard methods for the
float, string and int classes too, but would rather have a good methodology in place Any other comments on the code or tests are also welcome.
```
public static class ObjectExtensions
{
public static void ThrowIfNull(this object value, string argumentName)
{
if (value == null)
throw new ArgumentNullException(argumentName);
}
public static void ThrowIfNotEqual(this object value, string argumentName, object expectedValue)
{
if (!value.Equals(expectedValue))
throw new ArgumentException(argumentName, string.Format("Argument must not equal {0}", expectedValue));
}
public static void ThrowIfEqual(this object value, string argumentName, object expectedValue)
{
if (value.Equals(expectedValue))
throw new ArgumentException(argumentName, string.Format("Argument must equal {0}", expectedValue));
}
public static void ThrowIfNull(this object value, string argumentName, string customMessage)
{
if (value == null)
throw new ArgumentNullException(argumentName, customMessage);
}
public static void ThrowIfNotEqual(this object value, string argumentName, object expectedValue, string customMessage)
{
if (!value.Equals(expectedValue))
throw new ArgumentException(argumentName, customMessage);
}
public static void ThrowIfEqual(this object value, string argume
Solution
In my opinion, the extension methods are not adding a lot of value. You'd probably be better off without them.
Let's take a look at an example:
vs
In both cases you have about the same amount of information. The programmer needs to decide:
The only real benefit to the extension method is the condition in the if block (== null), which they could theoretically stuff up and create a bug.
However, there are also some downsides to using the extension methods:
Don't take my word for it. I read this stuff in a blog post once.
http://blog.ploeh.dk/2014/08/07/why-dry/
Let's take a look at an example:
public void MyMethod(string value)
{
if (value == null) throw new ArgumentNullException("value", "custom message");
}vs
public void MyMethod(string value)
{
ThrowIfNull(value, "value", "custom message");
}In both cases you have about the same amount of information. The programmer needs to decide:
- which method to call (ThrowIfNull) vs which exception to throw (ArgumentNullException)
- which parameter to deal with ("value")
- what the custom message looks like ("custom message")
The only real benefit to the extension method is the condition in the if block (== null), which they could theoretically stuff up and create a bug.
However, there are also some downsides to using the extension methods:
- another programmer reading the code now has to deal with the cognitive load of learning what the extension methods are
- when the exception is thrown, the debugger will break inside the extension method, not the method that caused the exception.
- you've created a lot more work for yourself, for very little benefit.
Don't take my word for it. I read this stuff in a blog post once.
http://blog.ploeh.dk/2014/08/07/why-dry/
Code Snippets
public void MyMethod(string value)
{
if (value == null) throw new ArgumentNullException("value", "custom message");
}public void MyMethod(string value)
{
ThrowIfNull(value, "value", "custom message");
}Context
StackExchange Code Review Q#67124, answer score: 4
Revisions (0)
No revisions yet.