Always importing too many classes... I think - actionscript-3

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.

Related

Minify ceylon-sdk and ceylon-language when compiling to javascript

For an in-browser application written in ceylon-js it would be desirable to reduce the size of the ceylon.language-1.2.0.js file to only that what is actually needed.
This question was answered already.
How to use ceylon js (also with google closure compiler)
But the given solution involves manually editing javascript code resulting from compilation. This is not desirable since a compiler should produce code that hasnĀ“t to be edited manually after compilation (abstraction).
And it is not clear to me if google closure compiler can cope with the ceylon flavour of it in a reliable way.
Is it instead a solution to copy ceylon.language source in ceylon into the project and import only those parts of ceylon.language into the project that are required by it? Then compile to javascript. And then leave away ceylon.language-1.2.0.js at all from the client / in-browser application.
Now my questions:
What parts are needed in the most simple browser application? I think of something like Array(String) and the like.
Has that solution a chance to work absolutely reliable?
Will there be a better solution coming from the authors of ceylon that make this attempt obsolete?
The compilation of the language module to JS is a tricky process, because of the native stuff involved and because there are a couple of declarations that have to be in a certain order for things to work.
Minification is still pending, we are going to do it but it's not the highest priority right now, and we have to determine the best way to solve this problem; one option that has been discussed is to have a version of the language module without any metamodel info, for example.

How to deobfuscate AS3 source code?

Is there a way to de-obfuscate AS3? With de-obfuscation I mean making it compilable again. At this moment I lost some source code and only have the Flash file left. The problem I now face is that the compiler is not able to compile it as the class names contain illegal characters.
I could start manually naming all classes again however I'm not even certain if the de-compiler worked.
To compile the SWF again I use flash develop and the FlashSDK.
Regards.
This depends on how the code was obfuscated to begin with. If it was simply obfuscated by changing class/variable/function names, then you could use an IDE such as FlashDevelop which allows you to Refactor->Rename by right clicking the instance name which would rename it across the entire project. Or just do a simple find/replace using a text editor.
Knowing how the code was obfuscated is the most important thing though. Whichever program you used to obfuscate is probably going to provide you with the most information.
I would suggest, however, doing the bare minimum by removing all illegal characters first to see if it will compile.

Can I make a swc file with alchemy that will work for AS3 programmers that do not use alchemy?

I am very new to Alchemy and not an expert in using swc to store code(Although I am well aware that it worked for me so far). I want to make sure I understand if Alchemy is the right tool for me in this situation.
I want to make a simple script in Alchemy to help make certain 'heavy lifting' in the code lighter and less time consuming and ooze out more performance. The trick is that my colleagues will likely be daunted by the installation process(even though there is a guide). So my question is, if I do take the time to work with Alchemey, can I make my code available to other people that do not use it, for our project?
Hope that makes sense, my instincts (gut feeling) says yes. I simply want to make sure.
Yes, this is exactly what SWCs are for, you can compile your C++ code with FlasCC and expose a simple AS3 API that other people can use by linking your SWC into their project. Download the FlasCC beta and check out the tutorials it comes with that explain this in more detail: http://gaming.adobe.com/technologies/flascc/
Shortly, the answer is yes.
The point is you can't recompile the SWC into an SWF if you don't use alchemy. What you can do, is download at runtime (using Loader.load with ApplicationDomain.currentDomain) the SWF contained into the SWF (SWC is a simple zip, extract it).
Then, you can freely use the class that's inside, using ApplicationDomain.getDefinition("OneOfYourClass).
More about application domain
Since it's at runtime, you won't have to compile Alchemy, thus it should work fine for anyone.

Runtime Class creation in actionscript-why and for what purpose?

Hi
Recently in actionscript it has been made possible to create Classes at runtime. Thi seems quite cool, but I am perplexed thinking of a situation in which this might be useful. Anyone have any ideas?
First of all, the uses for this in ActionScript is limited. So start of understanding what it actually is, and how other languages use it.
See Self-modifying code on wiki:
http://en.m.wikipedia.org/wiki/Self-modifying_code
Also see Reflection:
http://en.m.wikipedia.org/wiki/Reflection_(computer_science)
As an example of how it might be useful, I'm currently working with genetic algorithms to modify code at runtime. This way I can test every permutation (varying initial values and methods) without having to create classes for them, with the added bonus of exporting a .swf only containing the winning permutation.

How to compile the generated AS

hope you can help me with this question.
So, I've been working for a while with Flex, and had the crazy idea to create pure AS project.
If I compile a Flex app with the -keep flag, the generated actionscript gets generated.
Do you guys know of a way to make it compile, without going trough the code and gluing it all together?
Thanks.
Generated ActionScript is really only provided for reference; it's not really intended to be repurposed in that sense. Indeed, if you even have any, you've most likely compiled your project already anyway (unless you got it from somewhere else), so one might ask why you'd want to compile the generated stuff rather than your own source -- but nonetheless, although I haven't actually tried it, you should be able to point the Flex compiler mxmlc at your generated source to compile it, provided you're able to get all your dependencies to line up (which may be what you mean by "gluing it all together").
Just a thought, although again, I haven't actually tried it, so your results may vary. What is it you're trying to do, though? Just curious. :)
Like Christian mentioned, the generated AS code is pretty much there to serve as a reference. Just for fun, I tried compiling it from the command line using mxmlc, but part of the problem with doing that is that the class files generated don't match the classes they represent. So, you'd need to start renaming a bunch of your class files so that compiling those could even work. Many of the other classes in Flex won't be compiled in either, for the same reason.
As an example, the project I tested this with, had only one mxml file of source code in it. The generated output was 45 files. I'm sure that if you really wanted to do this, and were willing to invest the time in cleaning things up you could. But I'm curious too - why would you want to do this?
Actually it was just mere curiosity. I wanted to understand how the framework works and see how it merges it all together to make the final application, as I thought that the generated actionscript files were just a step in between the final SWF file, so I tought there should be a way for it to grab those files and generate the final application.
Eventually I would have tried to create a pure actionscript project just for the heck of it to see how complicated it was and what errors could happen when developing other stuff.
I know it sounds crazy, but I had some spare time, ha ha ha.