Actionscript 3.0: Scope - actionscript-3

Tutorials usually don't deal with scope in Actionscript. Can you point me to some documentation and/or explain what should I know about it. I want to avoid problems arising from certain classes are not visible at certain places.

These should help.
Function scope:
http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_21.html
Packaging and namespace:
http://livedocs.adobe.com/flex/3/html/03_Language_and_Syntax_04.html#119303

You're a bit vague, but hopefully I'm getting you ;)
Scope for classes are generally pretty easy to handle, it mostly comes down to packages.
Packages are created in a simple tree structure, and in ActionScript3 the filestructre has to follow the namespaces. Which makes it even easier.
You can access any class from anywhere, but if it's in another package you will need to "import" the class. This is done by writing an import statement in the beginning of class or interface where you need to use it. Like so:
import flash.display.MovieClip;
There is an exception to this rule, a class can be declared with the internal keyword, in which case the class will only be available within that package. This is mostly used for helper classes.
Basicly you should not worry about classes not being available.
NB:
You create package with the package keyword.

Related

Origin of hierarchical structuring

Why are libraries located behind com/ or net/ directory structures?
This is agnostic to Flash, Flex or any language. It's been used for a long time in general software development. I believe it stemmed from the Java package structure, but I'm not sure. It's used because it's now a standard on how to do things and helps split up projects in a fairly unique way.
It normally goes like <domain extension>/<domain>/<project name>/<sub component>/<whatever>.
This format/structure is called the reverse domain name structure. This structure is used for the package namespace for your classes.
Here is a good article on The Classpath Demystified by Jody Hall
If you're talking about class packages the point is every package should be unique. Imagine you wrote a class named MyGreatClass. Without any package or within some simple package test.MyGreatClass (this is called fully qualified class name). In this project you've decided to use some library where somebody wrote another test.MyGreatClass class (he/she didn't realize you have another one). So you'll have a conflict of two classes.
To avoid that situation there is a convention to start classes with author's site name in reverse order. Taking in mind every domain name is unique. Following this convention you can be sure you class won't conflict with others.
As far as com and net are most common domains you can see com.example (for http://example.com/) and net.example (for http://example.net/) very often.
Advantages of OOP
Inheritance
maintainability
Re-usability
A class is considered an object.
Having a package structure allows for all the advantages of OOP
Having a standard folder "com" where all your custom classes are allows you to reuse those classes with ease.
All libraries that I did not create, I make sure goes into my com folder. So when I make a new project I just have to point the project settings to that folder, then I can access those libraries with just having to do an import statement.
For example The AS3crypto library I have in the com folder.

Minimize number of imports in AS3

Just wondering, is it possible to limit number of 'imports' in AS3 code by defining them in parent class or in any other way?
I wouldn't recommend it but you can use #include to include a standard set of imports (or any AS3 code).
Also you can use import com.whatever.* to import by package instead of class.
I would say that you shouldn't worry about it! It doesn't matter how much import statements you have. If you need to use the classes, then you need to import them.
A good IDE (e.g. FlashDevelop) will manage your imports anyway, so you don't even need to think about it.
I personally think (and I am sure that a lot of programmers would agree) that you should hide/obscure how a system works from yourself. It should be obvious what is going on, and not "weird". Stick to the best practices, which is having your imports at the top of your class.

Is a Class special when it has no members?

I just realize some of my classes have no members. It has several public functions and maybe private functions and everything is passes through params. I realize functional programmers do this all the time but is this class considered special if it access nothing outside of the class and only uses its params for reading (except for out params and return values)?
I know this class can be static but static classes can modify outside variables. I want to know if this is a technique or maybe if a language may give additional benefits for writing it this way and etc.
-edit- This is looking like a wiki so lets make it one.
It is just called "stateless". Nothing really special about it.
There is nothing wrong with a class that has no members; controllers do this very frequently.
Yes, you could go static, but by not being static you allow for inheritance from your memberless class, which could add members along the way, if you so desired.
If there are no members, then there is no need for a class other than having a namespace. If you were programming in Python, then you would just put those methods into a module and don't bother with a class. If you are working in Java, then a a class is a must. If you are working in C++, it's up to you - but maybe you should consider using just a namespace, instead of a class, to make it less confusing.
Yes, this makes the class completely thread safe and thus no locking is required. To me, that is a fantastic attribute.
Sane programming languages allow you to define functions outside of classes when the functions do not require any persistent data - if you can define it in an appropriate namespace, all the better. If your language does not allow that, at least define them in a static class so it's clear that no state is accessed or mutated. Overall, though, this reminds me of an excellent article on the illogical abuse of classes in the name of "pure OOP".
I'd make the class static. I don't see what advantage it would give to keep it non-static. But then again - I'm a .NET developer, not Java. The languages are close, but there are many subtle differences I'm not aware of.
Remember that all methods of your class (even stateless) have one special variable -- pointer/reference to object - this (self, whatever) for which they are applied to.
Thus it is perfect sense in such stateless class if its methods could be overridden: in this case you have to have class to provide dispatching by this.
(Of course it's just emulating of first-class functions, so if your language already has ones it's no sense in this technique.)
At the moment I can't imagine why would you need stateless class without any virtual method.
Unless you want to have nice auto-complete in your IDE by typing objectName and dot :)
its simple class there is no specialty in it.
But i dont understand what is the use of having public or private methods when there in no member in it. because member methods are those which acts on particular instance's state.
Yes you can have static methods in it.

Package name for a loop class

I wrote a class that performs an asynchronous loop. It needs a package name. I already have a util package, but feel resistant to put half of my classes in that package. If it really belongs there, I'll put it there, but I'd feel much better if I can find a more appropriate/specific package. What do you think?
peach for parallel each.
Shamelessly stolen from the Ruby project with the same name.
A package is normally created for more than one class. If your class uses some helper classes, then it should go in a separate package; differently, you should use the generic package you already have.
I ended up going with "timer" as the package name after finding many similarities with the Timer class that sits in the timer package.

Actionscript 3 import package.* vs import package.Class

In Actionscript 3, is there any reel overhead between importing a full package versus importing independant classes?
E.g.: import flash.display.* vs. import flash.display.Sprite
I know it's a good practice to import only the needed classes from a package to avoid conflicts, but I've often been told it also has a cost in term of the compiled file size if I import full packages in many different classes that only use some of the classes from those packages.
I wonder if one class will be imported once for all for the whole project, or if imports are multiplied among the classes that use them.
Resulting compiled file size and runtime performance are two different aspects this question embraces.
The only hit should be compile time, but rday writes that there is apperently a small hit. But that should be something that Adobe will fix in the future.
The import statements should not really be looked on as actual imports, it is merely a way for the compiler to know which classes you are refering to.
eg. If you made you own Point class and it was being used in another package, the compiler needs to know if you are refering to your own Point class or the Adobe Point class.
The alternative would be to write the fully qualified name evertime you refered to a class.
eg. var mySprite:flash.display.Sprite = new flash.display.Sprite();
As pointed out by Juan Pablo Califano in the comment, this does not actually work with the compiler (though I think it might work with AS2). I merely meant to point out why we have import statement to begin with.
In any case it should not effect the compiled file if you import a whole package (though it apperently does). It will how ever effect compile time, since you are giving the compiler more stuff it needs to look through.
As for "importing" the same class more than once. It will not make a difference. The compiler will only include the same class once. Else the compiled file size would quickly spin out of control, since most classes refer to many classes which again refer to others etc. But again, Adobe might have optimizing to do there.
Bottom line is you should only import the stuff you need, there is no real advantage to importing a whole package. Just use a proper coding tool like FlashDevelop (it is free) and you do not even have to write the import statements yourself.
On a side note, if you are compiling a library (where a class not refered to are also included), im not sure if importing a external package might include that in you compiled file. That might have an actual impact; though hopefully Adobe did not screw up there ;)
Addressing ryanday's points, I can't explain the extra 3 bytes, but a few notes...
The ActionScript Design Patterns book also discourages this due to excess baggage
Yep, on page 115, but I think it is wrong and submitted errata to that effect.
The ActionScript 3 spec says all public names from the package will imported if you use the '*'. So there is a hit,
It kind of does, but I disagree re the interpretation and hit. It says: "The names of package members are made visible..." (in full). In this context, it is referring to making names of members visible to the compiler and editor tools, not visible within the compiled SWF. i.e. does not mean the classes get compiled into the SWF - unless they are actually used (a variable declared of that type).
Another way of looking at this, you could manually import flash.display.MovieClip. But if you don't create any instance of MovieClip, the MovieClip class will not get compiled into the final SWF.
To satisfy myself, I compiled the following helloworld in 3 ways, outputting link-report as suggested by #secoif...
package
{
import flash.display.Sprite;
import flash.text.TextField;
public class ASHelloWorld extends Sprite
{
public function ASHelloWorld()
{
var tf:TextField = new TextField();
tf.text = "Hello World!";
addChild( tf );
}
}
}
First, as written, link report:
<report>
<scripts>
<script name="~/Documents/eclipse3.5carbonFbPlugin-FX4-LS10/ASHelloWorld/src/ASHelloWorld.as" mod="1278415735000" size="682" optimizedsize="344">
<def id="ASHelloWorld" />
<pre id="flash.display:Sprite" />
<dep id="AS3" />
<dep id="flash.text:TextField" />
</script>
</scripts>
<external-defs>
<ext id="AS3" />
<ext id="flash.text:TextField" />
<ext id="flash.display:Sprite" />
</external-defs>
</report>
Second, delete the link report file and change imports to:
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.text.TextField;
Clean build, and the link report looks exactly the same. Same size, same optimizesize, same linked classes.
Third, delete the link report file and change imports to:
import flash.display.*;
import flash.text.*;
Clean build, and the link report looks exactly the same. Same size, same optimizesize, same linked classes.
Only Sprite and TextField classes make it to the SWF in each case.
Looking at the actual SWF file size on disk, there does seem to be a slight (1 or 2 byte) variation over the 3 versions. No worse than for the larger SWF referred to in ryanday's post.
The ActionScript 3 spec says all public names from the package will imported if you use the '*'. So there is a hit, although it may not be a large one depending on the package size. The ActionScript Design Patterns book also discourages this due to excess baggage, as well as some Adobe ActionScript tips.
That being said, I took one as component in an app I wrote and
import mx.containers.*;
import mx.events.*;
import mx.managers.*;
Instead of the single class names. My size increased by 3 bytes. Now, the entire app is 935kB, so I probably have those classes imported elsewhere, and the hit wasn't very big. I bet the smaller your application is, the larger the impact on your compile size will be(percentage wise).
There is absolutely no difference in the compiled code whether you import the entire package or just the classes you are using. Imports are just important for the compiler to find where the classes are.
You can try to decompile or look at the bytecode before and after.
You can check exactly what classes are being imported by using the 'link-report' compiler option
The compiler may take longer to sort out what to include and what not to include, but if you check out your link reports, you'll notice it only includes what it uses. :)
As with most languages there is little or no performance overhead related to importing entire packages rather than individual classes.
However it is more a good practice since it gives a much more concise veiw of dependencies for your class
good practice is in general to have code that is readable ... having a class start with 200 import statements thus would be rather bad practice ...
in as3, an import statement only adds a new scope to the compiler's identifier resolution ... what is compiled into an SWF and what is not, is not determined by the import statements, but by actual dependancies, i.e. code from class A using class B ...
so at runtime it does not make any difference, how you imported your classes ...
greetz
back2dos
I discovered than for AS3, just merely using import statements will include the classes in your output, regardless of whether those classes are referenced in actual code. Contrast this against Java which will only include classes if they are actually used, not just mentioned in import statements. But I found this useful when designing a Flash API, just mention those classes in import statements and they will be included.