2011-11-17 83 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; 
}