Are there any benefits when using final in AS3?

2020-08-11 10:20发布

问题:

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.

回答1:

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



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

Final is seldomly used and with reason. However sometimes it's use is recommended though. As said by the others it can prevent extending a class and/or overriding a method. Here's a number of answers on the use of final, but they all focus on the security aspects: How could not using "final" be a security issue?

In my experience the only really useful instances of using final are when creating a public lib and you specifically want to prevent the overriding of specific methods and/or extension of classes. This has nothing to do with security though, but with encapsulation of behaviour. It's a bit hard to think of real examples, but the easiest would be with singletons for example. If you've created a class that is meant to be accessed as a singleton, it makes no sense (I know people DO it, but they really shouldn't) to allow a user of your class to be able to extend that class. And also, it will lead them to a world of pain. Marking the class as final is definitely appropriate in that case.

--EDIT

disclaimer: I'd really want to discourage the use of the singleton pattern for various reasons:

  1. it negates polymorphism

  2. it disallows inheritance

  3. it violates SRP

But since it's such a popular pattern, I might as well try to encourage people to make it a bit more secure by using the keyword final