2011-11-17 60 views
1

我有许多功能(funcOne, funcTwo, etc.),所有的人都在开始(我想那些块移动到一个单独的函数或东西,所以我不重复的代码共享检查相同的块,但问题是,我使用的回报。请仔细阅读)清洗重复使用返回代码

  • 如果这些检查失败,我输出特定的消息,这个特定的检查失败,并返回(因此函数的实际代码不执行)
  • 如果所有的检查通过,则功能继续功能的特定代码。

我想要做的是移动的检查,以一个单独的函数。但问题是,我使用return;这将返回出来的新功能,但不会从funcOne and funcTwo返回。有人可以帮助我重构此代码,因此我不必在使用它们的每个函数中重复执行重复检查。

protected function funcOne(event:MouseEvent):void 
{ 
    if(check 1 doesn't pass){ 
     Alert.show("error 1, returning); 
     return; 
    } 
    if(check 2 doesn't pass){ 
     Alert.show("error 2, returning); 
     return; 
    } 
    .... more checks here, all of them return specific messages 

    //if all checks pass 
    //execute the specific code of this funcOne 
} 
protected function funcTwo(event:MouseEvent):void 
{ 
    if(check 1 doesn't pass){ 
     Alert.show("error 1, returning); 
     return; 
    } 
    if(check 2 doesn't pass){ 
     Alert.show("error 2, returning); 
     return; 
    } 
    .... more checks here, all of them return specific messages 

    //if all checks pass 
    //execute the specific code of this funcTwo 
} 

回答

1

这里有一个快速的方法来做到这一点。如果您想在别处处理警报,您也可以返回实际的消息字符串。如果消息字符串为空,则没有错误。

protected function funcOne(event:MouseEvent):void 
{ 
    if(validate()) 
    { 
     //if all checks pass 
     //execute the specific code of this funcOne 
    } 
} 

protected function funcTwo(event:MouseEvent):void 
{ 
    if(validate()) 
    { 
     //if all checks pass 
     //execute the specific code of this funcOne 
    } 
} 

//returns false if not valid 
protected function validate():Boolean 
{ 
    var errorMessage:String = null; 

    if(check 1 doesn't pass) 
     errorMessage = "error 1, returning"; 
    else if(check 2 doesn't pass) 
     errorMessage = "error 2, returning"; 

    if(errorMessage) 
     Alert.show(errorMessage); 

    return !errorMessage as Boolean; //will return true if errorMessage is null 
} 
+1

必须要么让“验证”一个getter,这样就可以把它叫做没有括号或在每次调用写括号。否则,你会检查参考的价值,因为你的功能是“硬编码”,而不是动态的设置将始终返回true。所以你的例子不完整。 – LoremIpsum

+0

该死的,我完全忘了括号!我不想让它变成一个吸气器,不会有多大的意义。我编辑了我的帖子,对于错误感到抱歉。 – Exort

+0

我的意思是括号,而不是大括号。 – LoremIpsum

2

您可以在您的错误检查功能中创建一串错误,然后将该字符串返回给您的主函数。如果字符串包含内容,则显示它并打破您的程序;

protected function funcOne(event:MouseEvent):void 
{ 
    errors = checkForErrors(); 
    if(errors != null || errors != "") 
    { 
    Alert.show(errors); 
    return; 
    } 
} 

protected function checkForErrors():String 
{ 
    var errorString:String = ''; 

    if(check 1 doesn't pass){ 
     errorString +="error 1\n"; 
    } 
    if(check 2 doesn't pass){ 
     errorString +="error 1\n"; 
    { 

return errorString; 

} 
+0

这与Exort本质上是相同的解决方案,但这会导致您的funcOne()在错误发生后立即返回。 – eterps

3
protected function funcOne(event:MouseEvent):void 
{ 
    if(!checkAll(event)){ 
     return; 
    } 
    //if all checks pass 
    //execute the specific code of this funcOne 
} 
protected function funcTwo(event:MouseEvent):void 
{ 
    if(!checkAll(event)){ 
     return; 
    } 
    //if all checks pass 
    //execute the specific code of this funcTwo 
} 

private function checkAll(event:MouseEvent):Boolean 
{ 
    if(check 1 doesn't pass){ 
     Alert.show("error 1, returning); 
     return false; 
    } 
    if(check 2 doesn't pass){ 
     Alert.show("error 2, returning); 
     return false; 
    } 
    return true; 
}