A faster way to perform method argument checks
This is mostly just out of curiosity, and this is a potentially stupid question. :)
I have a way like this:
public void MyMethod(string arg1, string arg2, int arg3, string arg4, MyClass arg5)
{
// some magic here
}
None of the arguments can be null, and none of the string arguments can equal String.Empty
.
I have a big list instead:
if(arg1 == string.Empty || arg1 == null)
{
throw new ArgumentException("issue with arg1");
}
is there a faster way to just check all string arguments?
Apologies if my question is not clear.
Thanks!
a source to share
You can create or use frameworks to check the contract of your method, for example. Code contracts .
You can also create various utilities, such as ThrowIfNullOrEmpty
those that will encapsulate the argument validation logic.
a source to share
You can use String.IsNullOrEmpty:
if (String.IsNullOrEmpty(arg1) )
throw new ArgumentException("issue with arg1");
If you're working with Framework 4, there is also a String.IsNullOrWhiteSpace method (you might also want to throw if someone calls a method with a string containing only spaces).
You can make a utility method to validate an argument and cast, but that's the shortest notation you'll get. There is no syntax in C # that lets you tell the compiler that zeros are unacceptable and the language throws exceptions for you.
With framework 4, you also have Code Contracts - but the syntax for using it remains a method call.
a source to share
if (string.IsNullOrEmpty(arg1))
{
throw new ArgumentException("issue with arg1");
}
There is also a code contract in .NET 4.
a source to share
The real problem is your method has too many arguments. This makes you cumbersome to write validation code. Makes it cumbersome in client code. Consider writing a helper class in which the arguments you are currently passing are properties. Now your method can only take one argument. And the validation can be done in the class itself, not in your method.
The canonical example of this pattern in the Framework is the Process class. The ProcessStartInfo helper class allows you to use it.
a source to share
There is a nice library out there called CuttingEdge.Conditions that provides a fluent interface for this:
public void MyMethod(string arg1, string arg2, int arg3, string arg4, MyClass arg5)
{
Condition.Requires(arg1, "arg1").IsNotNull().IsNotEmpty().StartsWith("Foo");
}
It provides type safe conditions for all kinds of checks, including zero checks, range checks by numbers, string checks (including StartsWith above), etc. If you have multiple parameter checks, it can greatly shorten and simplify your check and, most importantly, make the argument and exception checking consistent across your application.
a source to share
Well, first of all, you can use if (!String.IsNullOrEmpty(arg1))
to shrink the if statement.
Second, do you need argument validation in your build version, or would arg validation in your debug build be sufficient? It depends on where the data is coming from. If this function is called by your own code and the arguments are handled by your calling code, it is probably sufficient to check the arguments only in your debug assemblies. In this case, use Debug.Assert(!String.IsNullOrEmpty(arg1), "msg");
etc. Assertions are stripped of release versions.
Third, check out DevLabs: Code Contracts
a source to share
As I understand your question, how about this:
static void Main(string[] args)
{
Test(null, "", "", "");
}
static void Test(MyClass arg1, params string[] args)
{
if (args.Count(a => String.IsNullOrEmpty(a)) == args.Length)
{
throw new Exception("All params are null or empty");
}
}
a source to share