validation - What is the clean way to validate few method's parameters? -


let's have function:

void sayhello(string name, string surname); 

and want sure both parameters valid. i'm doing way:

if (name == null)     throw new exception("name invalid"); if (surname == null)     throw new exception("surname invalid"); 

when i'm invoking sayhello(null, null); i'm getting information invalid name.

is there clean way warned 2 errors @ once? clean, mean without 2 flags (or more in case of more arguments) , many if statements?

the snippets in java i'm rather asking generic design solution.

i suggest build error message each validation throw once i.e. after validations done.

eg:

void somefunction(string param1, string param2, string param3) {   string error = "";    if (invalid(param1)) {     error += "param1 invalid";   }   if (invalid(param2)) {     error += "param2 invalid";   }   if (invalid(param3)) {     error += "param3 invalid";   }   if (!error.equals("")) {     throw new exception(error);   } } 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -