sameold
sameold

Reputation: 19252

cleaning repeated code that uses return

I have many functions (funcOne, funcTwo, etc.), all of them share the same block of checks at the beginning (I want to move those blocks to a separate function or something so I'm not repeating code, but the problem is that I use return. Please read on)

What I want to do is move those checks to a separate function. But the problem is that I'm using return; which would return out of the new function, but wouldn't return from funcOne and funcTwo. Can someone help me refactor this code so I don't have to repeat the duplicate checks in every function that uses them.

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
}

Upvotes: 1

Views: 62

Answers (3)

Exort
Exort

Reputation: 1075

Here's a quick way to do it. You could also return the actual message string if you want to handle the alert elsewhere. If the message string is null, then there's no error.

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
}

Upvotes: 1

pioSko
pioSko

Reputation: 551

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;
}

Upvotes: 3

eterps
eterps

Reputation: 14208

You can build a string of errors in your error checking function, then return that string to your main function. If the string has contents, display it and break your program;

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;

}

Upvotes: 2

Related Questions