c# - For Good coding practice, do we still have to validate data again in method2 if we already validated in method1 & method1 passes that data to method2? -


let have

public void method1(){     string s1="";     string s1=gettext();     if(myvalidation.isok(s1)){        dosomethingwith s1 here               method2(s1);     } }  public void method1(string s1){     if(myvalidation.isok(s1)){ // need line of code??         //do     } } 

for coding practice,

do still have validate data again in method2 if validated in method1 & method1 passes data method2?

you should refactor code isolate internal methods assume data exposed public methods execute validation on external inputs.
of course, if screw data in internal methods problem

public void method1(){     string s1="";     string s1=gettext();     if(myvalidation.isok(s1)){        runsomethinginternalformethod1(s1);        // or        // if(runsomethinginternalformethod1(s1))        //     runsomethinginternalformethod2(s1);     } }  public void method2(string s1){     if(myvalidation.isok(s1)){          runsomethinginternalformethod2(s1);     } }  // private here ... no way call code external class private void runsomethinginternalformethod1(string s1){     .....     // call additional internal code here, or add      // call after public method1, change return value     // of method , call second 1 if 1 successful     runsomethinginternalformethod2(s1); } private void runsomethinginternalformethod2(string s1){  } 

another approach, cannot recommend because leads complex state through use of global class level boolean variable contains result of validation. repeat myself, in case validations complex , expensive repeat (a simple null check not expensive operation)

public class test {      private bool _validatedok = false;       public void method1()      {           if(!_validatedok)                _validatedok = myvalidation.isok(s1);          if{_validatedok)          {               ......               method2();          }      }      public void method2()      {           if(!_validatedok)                _validatedok = myvalidation.isok(s1);          if{_validatedok)          {               .....          }      } } 

as can see approach doesn't repeat validation same instance of class.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -