对于良好的编码实践,如果我们已经在method1&中验证了,那么我们是否还必须在方法2中再次验证数据? method1将该数据传递给method2?

时间:2014-02-01 09:06:02

标签: c# java php c++ validation

我说我有

public void method1(){
    String s1="";
    String s1=getText();
    if(MyValidation.isOk(s1)){
       dosomethingWith s1 here
       then
       method2(s1);
    }
}

public void method1(String s1){
    if(MyValidation.isOk(s1)){ // do we need this line of code??
        //do something
    }
}

对于良好的编码实践,

如果我们已经在method1&中验证了数据,我们是否还需要在方法2中再次验证数据? method1将该数据传递给method2?

4 个答案:

答案 0 :(得分:5)

因为它是一个公共方法,所以不能保证method1(string)只能从method1()调用吗?

答案 1 :(得分:2)

您应该重构代码,以隔离假定数据良好的内部方法,这些方法来自在外部输入上执行验证的公开方法。
当然,如果您在内部方法中搞砸了数据,那么这就是您的问题

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 TO CALL THIS FROM CODE EXTERNAL TO THIS CLASS
private void RunSomethingInternalForMethod1(string s1){
    .....
    // You could call the additional internal code here, or add this 
    // call after the public method1, you could even change the return value
    // of this method and call the second one only if this one is successful
    RunSomethingInternalForMethod2(s1);
}
private void RunSomethingInternalForMethod2(string s1){

}

另一种方法,但我真的不能推荐它,因为它导致复杂状态是通过使用包含验证结果的全局类级布尔变量。我重复一遍,以防您的验证复杂且重复成本高(简单的空检查不是一项昂贵的操作)

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)
         {
              .....
         }
     }
}

正如您所看到的,此方法不会重复同一个类实例的验证。

答案 2 :(得分:0)

如果您的method2将通过其他方式调用怎么办?

通常,当您创建某个方法public时,您永远不能确定您的方法只会被某些调用者调用,并且仅对调用者进行验证。

总而言之,你需要在里面进行验证,以确保任何调用你的方法的人都不会得到异常。

答案 3 :(得分:0)

如果您已经验证了method1中的数据,并且在方法2的调用之间无法修改数据, 那么就没有必要再次验证它了。

根据编辑过的问题,如果您在method1验证后修改数据,则必须在特殊情况下在method2中再次验证


                案例1

  public void method1(string s)
  {
        if(s!=null)
        {
            // do something with s here. 
            // but not make it null
            method2(s) ; 
        }
  }
  public void method2(string s)
  {
          // do something with s
  }

在上述情况下,即使您不检查s中的method2,除非从其他方法调用method2,否则不会产生任何问题

<强>情况2

     public void method1(string s)
     {
        if(s!=null)
        {
            // do something with s here. 
             String temp = s ; 
             s = null ; 
             method2(s) ; 
        }
  }
      public void method2(string s)
      {
            // do something with s
       }

在case2中,如果不检查method2中的条件,则可能会有危险。

在任何情况下,我的建议是仅在需要检查条件的情况下检查两种方法中的条件,即有可能使用来自其他来源的无效输入调用method2