Reputation: 47
I see some java code does a cleaner way to exit as shown in this code.
if(a!=1) {System.exit();}
//If equal 1, execute the code below
Is there any similar keyword for Actionscript to stop a function (not exit a loop or application) as alternative to break
keyword?
private function isInt():void {
if(a!=1) { /* stop executing the code after these */ }
b=a;
}
Upvotes: 0
Views: 3875
Reputation: 22604
Using a conditional return statement may seem clean and beautiful at first, but it can become a problem when refactoring, because it doesn't show its intent clearly.
What you are trying to say is this:
if my conditions are met, execute the next statement.
But what you are saying is:
if my conditions are not met, get out of here.
Example #1
Say you have a complex method with two or three nested if statements:
function myComplexMethod ( param1 : int, param2 : int, param3 : String ) : String {
var ret:String = null;
if (param3 != null) {
ret = param3;
if (param2 != 0) {
ret += param2;
if (param3 != 0) {
ret += param1 + param2;
}
}
}
return ret;
}
Example #2
Of course, the same method would look better using conditional return statements:
function myComplexMethod ( param1 : int, param2: int, param3:String ) : String {
if (!param3) return null;
var ret : String = null;
ret += param3;
if (param2 == 0) return ret;
ret += param2;
if (param1 == 0) return ret;
ret += param1 + param2;
return ret;
}
But at first glance, can you really tell what the output of this method is going to be?
Example #3
What if you were going to really clean up your code? If you refactored the code to reflect the intent of each block, the complex method would become a simple method:
function mySimpleMethod ( param1 : int, param2: int, param3:String ) : String {
return param3 != null ?
outputOfAllParams (param1, param2, param3 ) : null;
}
function outputOfAllParams ( param1 : int, param2: int, param3:String ) : String {
return param2 != 0 ?
param3 + combinedOutputOfIntParams ( param1, param2 ) : param3;
}
function combinedOutputOfIntParams ( param1 : int, param2: int ) : String {
return param1 != 0 ?
param2 + "" + sumOfIntParams( param1, param2) : "" + param2;
}
function sumOfIntParams( param1 : int, param2: int ) : int {
return param1 + param2;
}
In this last example, the code is easily readable, and you can see what it is supposed to do by just looking at it.
The thing is: You could easily use refactoring to get from example #1 to example #3 - it is obvious that each of the conditional blocks will be refactored to a new method. With the conditional return statements, it is far less intuitive: You'll have to check very carefully, where the desired code block ends, and when to return what.
Of course, if you're dealing with simple tasks like this, the benefits are not as obvious, but if you had real production code, you definitely want the intent as obvious as possible, and even if you were coming back to your own code in a couple of months, it would take you much longer to understand what it is supposed to do, if you used example #2.
Upvotes: 1
Reputation: 4865
The Java code you quote exits the entire process - it stops executing. Flash runs inside a host process such as a web browser which should not be arbitrarily shut down - what if the user had content on other tabs?
If you specifically wanted to send a signal to the hosting browser, you should use fscommand or the newer mechanisms and let the browser/web page decide what to do.
If you just want to prevent any more execution of your function, just use 'return;' to leave the function you're in without doing any more processing.
Upvotes: 2