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.
Related
I have a basic problem with knowing which classes to import for a given application, renderer, AS package, mxml component, etc. There seems to be hundreds of classes (both mx and flash) and I'm never sure which one(s) to import... so I just keep adding import statements until the errors go away. Is there a reference somewhere that I don't know about? Or does this just come with experience? Also... does importing a load of classes actually make the file size larger or does Flex only import the classes used nregardless of what I specify? If it only uses what is needed, why wouldn't everyone just do: import mx.*;
I would suggest that if you find yourself bringing in tons of imports, you should ask yourself: Does this class do to much?
It is less of a technical issue, and more of problem of object-oriented design -- maintainability, testability and stability.
I do my best to limit my external dependencies. I try to conform to SOLID principles that tell me that classes should exist for one reason. If a class does too much, it is a "code smell" and an indication that you should split it up.
How much is too much? It is tough to have a specific litmus test or limit... I just ask myself "What does this class do"? If my answer contains an "and" in it, then I consider splitting it up.
I think your problem is a not a real problem if you use any half decent IDE. If you're not using one, you probably should (even if it's not stricly necessary and you can write and compile with notepad and the command line).
If you are using Flex/Flash Builder, it will add the imports automatically (and remove the unneeded ones as well). Also, you can use Ctrl + SPACE to prompt autocomplete, which should add the necessary imports.
Flash Develop also manages this for you (the shortcut was Ctrl + Shift + 1 if I recall correctly, but I haven't used FD for a while).
There are other IDEs out there that I haven't personally used but also have this very basic feature.
If you're using the Flash IDE, well, it really sucks for writting code, so you should probably consider writting your code in some other less brain-dead editor if you plan to do anything more than a couple of lines of code here and there (again, you can write code in the Flash IDE but why not taking advantage of better tools when they're available?).
When you get an error, look at the API Reference for the class, and then either import the whole package or just the class you want. Highlighting the class and hitting F1 should also work (but I never search help this way).
As for file size, see my answer on Is it possible to dynamically create an instance of user-defined Class in Action Script 3?
As Juan pointed out, use FlashDevelop, it is a great (and free) IDE.
If you're using FlashDevelop with the Flex Compiler, you can compile straight from FlashDevelop, and use the refactoring tools they offer to slim down your imports.
Aside from that though, if you're not referencing them, they don't get compiled, so it's not like your compiled swf is any bigger.
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.
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.
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.
In your object-oriented language, what guidelines do you follow for grouping classes into a single file? Do you always give each class a seperate file? Do you put tightly coupled classes together? Have you ever specified a couple of implementations of an interface in one file? Do you do it based on how many lines of code the implementation might be or how "cluttered" it might look to the user of the class? Or would the user prefer to have everything on one place?
Personally, I suggest one class per file unless the secondary classes are private to the primary class in the file. For example, a nested class in C# would remain in the parent classes file, but utility classes that might be useful elsewhere get broken into their own file or even namespace.
The key is to understand your environment and where people will look for things. If there is an established methodology in place, think carefully before you upset it. If your coworkers expect that related, tightly bound classes will be in a single document, having to search for them could be annoying (although with modern IDEs it shouldn't be a problem).
An additional reason for breaking things into more files rather than less is version control. If you make a small change, it should change only a small file where possible. If you make a sweeping change, it is obvious looking at the logs because of all the files (and indirectly, classes) that were affected are noted.
I think best practices in all OO languages I have ever used is to have one class in one file. I believe some languages may require this but I am not sure of that fact. But I would say that one class per file, and the name of the file matching the name of the class (as well as the directory structure matching the package structure for the most part) is best-practice.
1 class = 2 files. An .h and a .c, you kids are so lucky :)
There is no hard and fast rule that must always be followed (unless a particular language enforces it). There are good reasons for having just one class, or having multiple classes in a file. And it does depend on the language.
In C# and Java people tend to stick to one file per class.
I'd say in C++ though I often put multiple classes in one file. Often those classes are small and very related. Eg. One class for each message in some communications protocol. In this case a file for each would mean a lot of files and actually make maintenance and reading of the code more difficult than if they were in one file.
In C++ the implementation of a class is separate from the class definition, so each class { /body/ } is smaller than in other language and that means classes are more conveniently sized for grouping together in one file.
In C++ if you're writing a library (eg the standard template library) , you can put all the classes in one file. Users only need to include the one header file and they get all the classes then, so its easier for them to work with.
There's a balance. The answer is whatever is most easy to comprehend and maintain. By default it makes sense to have one class per file, but there are plenty of cases when it's more practical to work with a related set of classes if they are defined in one file.
I put classes into the same file if they belong together, either for techinical or aesthetic reasons. For example, in an application that provides a plugin interface, the classes Plugin (base class for plugins) and PluginManager I would usually put together in the same file. However, if the file grows too big for my taste, I would split them into separate file.
I note that I write code mostly in Python at the moment, and this influences my design. Python is very flexible in how I get to divide stuff into modules, and has good tools for managing the name spaces of things. For example, I usually put all the code for an application in a Python module (a directory with __init__.py) and have the module import specific names from sub-modules. The API is then something like applib.PluginManager rather than applib.pluginstuff.PluginManager.
This makes it easy to move things around, which also allows me to not be so fussy when I am creating the design: I can always fix things later.
One class = one file. Always. Apart from when one class = multiple files in C#, or a class contains inner classes etc of course ;)
One per a file is our standard. The only exception is that for a class and it's typed collection we put those together.
Over time I've come to relize, that "small class" always tend to grow. And then you'll want to split them up, confusing everyone else on the team (and your self).
I try to keep one class per file (like most of the above), unless they are small classes. If there are a lot of them, I may split them into subjects otherwise. But usually I just keep them all in one file with code-folding in editors. For my private hacks, it just isn't worth the (minimal) effort to me.
One class per file seems to be the standard. This is the way that I usually do it as well.
There have been a few times where I've strayed away from this. Particularly when a smaller class is a member of another class. For example, when designing a data structure, I would likely implement a "node" class within the same file as the "bigstructure" class.
In your object-oriented language, what guidelines do you follow for grouping classes into a single file?
It depends. In team work I try to follow team standards; in solo work I tend more towards whatever-I-please.
In solo work, then ...
Do you always give each class a seperate file? Do you put tightly coupled classes together? Have you ever specified a couple of implementations of an interface in one file?
No. Sometimes. Yes.
Do you do it based on how many lines of code the implementation might be or how "cluttered" it might look to the user of the class? Or would the user prefer to have everything on one place?
It's mostly based on:
How easy is it to navigate? A huge long source file, with many classes, is more difficult.
How easy is it to edit? When editing multiple, short, related classes, it may be easier if they're all in one source file with a splitter than if they're in several source files, given that I run my text editor maximized showing one source file at a time.
I prefer 1 to 1 for classes unless the inner class will be entirely private.
Even then I usually break it out for ease of finding it and tracking changes in SVN.