开发者

Faster way to perform checks on method arguments

开发者 https://www.devze.com 2022-12-26 12:37 出处:网络
This is mostly just out of curiosity, and is potentially a silly question. :) I have a method like this:

This is mostly just out of curiosity, and is potentially a silly question. :)

I have a method 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.

Instead of me hav开发者_如何学运维ing a big list of:

if(arg1 == string.Empty || arg1 == null)
{
    throw new ArgumentException("issue with arg1");
}

is there a quicker way to just check all the string arguments?

Apologies if my question isn't clear.

Thanks!


You can create or use frameworks to check contract of your method, e.g. Code Contracts.

Also you can create various utility methods like ThrowIfNullOrEmpty who will encapsulate logic of checking arguments.


You can use String.IsNullOrEmpty:

if (String.IsNullOrEmpty(arg1) )
    throw new ArgumentException("issue with arg1");

If you're on Framework 4, there is also a String.IsNullOrWhiteSpace method (perhaps you also want to throw if someone calls the method with a string containing of spaces only).

You can make a utility method to check an argument and throw, but that is the shortest notation you will get. There is no syntax in C# that allows you to express to the compiler, that nulls are unacceptable, and have the language throw the exceptions for you.

With framework 4, you also have Code Contracts - but the syntax to use it, remains a method call.


if (string.IsNullOrEmpty(arg1))
{
    throw new ArgumentException("issue with arg1");
}

Also there are code contracts in .NET 4.


There's a nice library 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 sorts of validations, including null checks, range checking on numbers, string checking (including the StartsWith above), etc... If you have multiple checks for parameters, it can dramatically shorten and simplify your validation, and most importantly, make the argument checking and exceptions consistent throughout your application.


The real problem here is that your method has too many arguments. That makes it unwieldy for you to write the validation code. Makes it unwieldy on the client code as well. Consider writing a helper class where the arguments you now pass are properties. Your method could now take only one argument. And the validation can be done in the class itself instead of your method.

The canonical example of this pattern in the Framework is the Process class. The ProcessStartInfo helper class keeps it usable.


Well, first of all, you can use if (!String.IsNullOrEmpty(arg1)) to reduce the if expression.

Second, do you need argument checking in your release build, or would it suffice to perform arg checking in your debug build? It depends on where the data is coming from. If this function is being called by your own code and the args are processed by your calling code, it would probably suffice to check the args in your debug builds only. In that case, use Debug.Assert(!String.IsNullOrEmpty(arg1), "msg"); etc. Asserts are stripped out of release builds.

Third, check out DevLabs: Code Contracts


As far as I could understand your question, how about that:

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");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号