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)
- If any of these checks fail, I output a specific message that this specific check failed, and return (so the actual code of the function doesn’t execute)
- If all checks pass, the function continues to the specific code of the function.
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 }
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.