Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to check the parameters of the method? [closed]

I know two ways to check parameters of the method and throw exceptions when it is needed.

1) Check one each parameter and throw an exception when it is wrong:

public void Method(object parameter1, object parameter2)
{
    if (parameter1 == null)
    {
        throw new ArgumentNullException("parameter1");
    }

    if (parameter2 == null)
    {
        throw new ArgumentNullException("parameter2");
    }

    ...
}

2) Check all parameters at once and throw same exception for all:

public void Method(object parameter1, object parameter2)
{
    if (parameter1 == null || parameter2 == null)
    {
        throw new ArgumentNullException();
    }

    ...
}

The first approach is, in my opinion, better and cleaner, but also cover a lot of lines. For example, a method that actually do the 2 lines of code - in this way code will increase by 4 rows (including blank line) for each parameter.

I am interested in is the approach used by experienced programmers. Are there better ways than these two?

like image 417
mveith Avatar asked Mar 31 '12 09:03

mveith


People also ask

What is the method of parameter?

Parameters are variables defined in the method declaration after the method name, inside the parentheses. This includes primitive types such as int, float, boolean, etc, and non-primitive or object types such as an array, String, etc. You can pass values(Argument) to the method parameters, at the method call.

What is a method parameter modifier?

They are few keywords that are use to control how arguments are passed to the method. (None): If parameter is not marked with a parameter modifier,it is assumed to be passed by value, meaning the called method receives a copy of the original data. static int Add(int x,int y){

How many types of parameters method can have?

It provides an ease for user because sometimes we may have 10-20 parameters in a method.

How do you validate an argument in C#?

1) Check one each parameter and throw an exception when it is wrong: public void Method(object parameter1, object parameter2) { if (parameter1 == null) { throw new ArgumentNullException("parameter1"); } if (parameter2 == null) { throw new ArgumentNullException("parameter2"); } ... }


2 Answers

Updated July 2020

Check out this blog post on how you can achieve a similar approach to Code Contracts.

https://enterprisecraftsmanship.com/posts/code-contracts-vs-input-validation/

Original answer provided below

—-

If you are using .NET framework 4, check out Code Contracts, which simplifies it down to a single line of code

public string Reverse(string text)
{
   Contract.Requires<ArgumentNullException>(text!=null, "ParAmeter cannot be null.");
  
   .....
}

The reason you would use this is because you can now get automated tools like Pex to tell you what unit tests to apply to this method. It also gives you feedback at compile time if this method would throw an exception based on how you are calling it. Like

String text = null;
String reversedString = Reverse(text);

The compiler will warn you that this will throw an exception.

Note Code Contracts needs an add-in to be installed, but it is free.

like image 90
Dominic Zukiewicz Avatar answered Sep 19 '22 13:09

Dominic Zukiewicz


Use method attribute to cleanly check your parameters. i was wrote a framework for parameter validating in python.the c# best practice is here

like image 32
pylover Avatar answered Sep 20 '22 13:09

pylover