开发者

Overloading, optional arguments, or something else?

开发者 https://www.devze.com 2023-01-20 19:58 出处:网络
I need to validate a field\'s value according to two different validation rules: An object of my own which contains a regex and a range (for string it\'s length range).

I need to validate a field's value according to two different validation rules:

  1. An object of my own which contains a regex and a range (for string it's length range).
  2. A list of possible values.

So I can do this:

public static bool Validate(string fieldValue, string fieldType, ValidationParameters validationParameters)
{
...
}    
public static bool Validate(string fieldValue, string fieldType, string[] possibleValues)
{
...
}  

But that requires the user to ungracefully if.

I can also do this:

public static bool Validate(string fieldValue, string fieldType, 开发者_运维百科ValidationParameters validationParameters=null,string[] possibleValues=null)
{
...
}  

Now the user can just send he's data, without redundant if, But I can't make sure validationParameters or possibleValues (one of them) got a value.

Is there a third option, one in which the user won't have to check which method he uses, but I won't have to worry that he doesn't send any of the two fields (in code, not documentation)?

If not, which way of the two above is better (less error-prone and more elegant)?

Thanks.


I personally would do two overloaded methods. The main reasoning for this is:

But I can't make sure validationParameters or possibleValues (one of them) got a value.

Optional arguments, in my opinion, should be exactly that: optional. Making two optional arguments where one is required seems like a problematic design choice.

Your main reasoning for avoiding this seems to be:

But that requires the user to ungracefully if.

However, I don't see the problem. The user is going to have to construct either the string collection (which, I personally, would either make IEnumerable<string> instead of string[], or do params string[] possibleValues) or the ValidationParameters. If they already need to construct the arguments to pass, adding an if conditional seems unnecessary (each path could just call the validation).


If they are validating by different concepts, they should probably be named differently. Say, ValidateByValidationParameters and ValidateByPossibleValues. Now the question is, why wouldn't you want the caller to carefully select which one he is using?


Because you can't require that one of them is sent, I'd recommend the former option, but I don't understand what you mean by the fact that you don't like that the user has to "ungracefully if".


You can overload the methods - as you said - or do something like this:

       enum TYPE { VAL_1, VAL_2 }

       void function(TYPE validateType, object o)
       {
        swich(validateType)
        { 
           case VAL_1:
              List<string> par = o List<String>;
              if(par != null)
              {
                 //...
              }
           break;

           case VAL_2:
              List<MyObject> par = o as List<MyObject>;
              if(par != null)
              {
                 //...
              }
          break;
        }
  }

Maybe it's not very typesafe, but somteimes useful.

At last - you can prepare single delegate method for every validation case and call those methods (single or many) in one place.

0

精彩评论

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

关注公众号