Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any benefits when using final in AS3?

I've tried to make a habit recently of using the final keyword wherever logical, like any classes that should never be extended.

I've done this purely because I like to maintain a strict and concise coding style.

Aside from the obvious advantages in cases such as developing application/game foundations for other parties or when working with others, is there any actual benefit when using final? i.e. performance.

like image 765
Marty Avatar asked Jan 17 '12 05:01

Marty


4 Answers

According to this extensive test, the final keyword does not make the slightest bit of difference performance-wise.

The way final is used in ActionScript, this comes as no surprise: The final keyword marks only classes and class methods, and thus it affects only inheritance. I see no logical reason why calling a final method should be any faster than calling any other method: The runtime will still need to lookup a function pointer and execute the call.
The same is true for methods and classes in Java. However, in Java, final also marks references as immutable. And this is a big advantage over its use in ActionScript, because it allows the compiler to "inline" values (i.e. replace all references to the variable in byte code with its actual value, eliminating look-up procedures), which - among other benefits - can speed up performance a bit. Perhaps we will see some use of this in a future version of ActionScript - it would be a good idea for better code clarity and prevention of side-effects, as well.

And for those reasons, I would still consider it good practice to use final, even when you are not working with other programmers, in cases where the intended behavior requires methods or classes to disallow overrides: It clarifies intent and prevents undesired effects. It will, therefore, speed up development, and that's not such a bad thing, either ;)

like image 197
weltraumpirat Avatar answered Oct 14 '22 19:10

weltraumpirat


Important update a year after this was asked --

ActionScript Compiler 2.0 added support for function inlining under certain conditions, one of which is that the function is static or final. See: http://www.bytearray.org/?p=4789

Note also that if you're going to use inlining, you must decorate your functions with the [inline] metadata tag, and use the -inline compiler argument.

like image 37
mziwisky Avatar answered Oct 14 '22 20:10

mziwisky


I have seen no evidence, either on google or from experience that final classes or methods run any faster than non-final ones, and even if they do, the difference would be negligible.

The only use I see for it is to seal classes and stop overrides.

like image 2
annonymously Avatar answered Oct 14 '22 20:10

annonymously


To prevent a class from being extended or a method from being overridden, we precede that class or method definition with the final attribute.

How it works?
To Final a class:

final public class A
{
    //
}

To Final a method:

public class A     
{
    final public function A
    {
        //
    }
}

The final attribute is used for two reasons in ActionScript:

In some situations, final method execute faster than non-final methods. If you are looking to improve your application’s performance in every possible way, try making its method final.

Methods that are final help hide a class’s internal detail. Making a class or a method final prevents other programmers from extending the class or overriding the method for the purpose of examining the class’s internal structure. Such prevention is considered one of the ways to safeguard an application from being maliciously exploited.

like image 1
Swati Singh Avatar answered Oct 14 '22 19:10

Swati Singh