AS2 variable type
- Started
- Last post
- 13 Responses
- CyBrainX
With AS2 commands are generally documented in this format:
var image_mcl:MovieClipLoader = new MovieClipLoader();
with a description of the variable as MovieClip Loader. I know the script window will recognize it already because of the _mcl in the name.
What is the benefit of using this? The scripts work fine without it. I'm missing something.
- joyride0
It just preparing you for strict/strong programming. Your setting the variable and the only thing it can be is a MovieClipLoader. if you tried to set image_mcl down the road to a string or something, you will get an error. It just good programing.
- kinetic0
it's cleaner and in proper programming its better to type your vars
also, if you type shit like
blahblah_mc
or blahblah_btnthose two extensions (and many more other proper ones) will bring up a tool tip drop down type thing with the proper methods of that type of object
- CyBrainX0
Thanks for the quick response. I was getting the impression it was just to make the documentation easier to follow.
- unfittoprint0
Strict Data Typing it's good code policy.
ie. use a single type for each variable [boolean, string, number, etc...]
the same for function: Void [when no var return's required], Boolean/Number/String etc.... for everything else.
it really makes you code easier to read/understand and easier to target specific bugs.
some reading on this subject:
http://www.stevenhargrove.com/st…
http://livedocs.macromedia.com/f…if you really need a multiple value type of variable you may use the Object var type.
- CyBrainX0
I believe it's good practice but I would sooner shoot myself than use the same variable for more than one type of data. Thanks for the links.
- joyride0
I agree Cy... but when a project gets so big and you've got multiple people working on it crazy things happen. so if you keep it strict then you can debug things a bit easier. flash is just training everyone for as3 and its evolution towards a real language. Soon we won't need asp/php/java etc. IMO
- unfittoprint0
I find it most useful for when I have hundreds of lines of code and variables on a specific file that hasn't been opened in months and can quickly understand the structure by seeing the data type of some specific elements.
ie:
var content:XMLis very different from
var content:Array
you can check wich functions deal with XML, and how that specific var is used, without second guessing its purpose.
- ornj0
Comming from a C background it drove me nuts that my variables used to just be whatever I felt like them being at any given time.
Always specify, everyone is right, it's a better practice. I go as far as to comment all my variables to the point where its like an instruction manual when you open one of my .as files if I'm not rushing it.
- FanaticalKiller0
unfittoprint has got a point but even in large applications i dont use the underscore identifier i simply use it in the variable name such as myBtn or someTxt. this is also due to the fact that i dont use flash's actionscript panel i use an external editor or notepad so no drop-down methods for me. what is the best actionscript editor out these days
- skel0
I have to weigh in on the other camp on this one. It bugs me to no end to see variables suffixed with an underscore and then the variable type.
Of the advocates the stronger argument would be that it makes code more readable. I would counter that not only does it not add to readability, it makes it more tedious to type in, and encourages generic names for variables. If need be, I admit that I'm biased because I prefer camel, i.e. "myBtn" but I try to be more specific with my variable names.
The weaker of these is the statement that you might code up as if something were a string when it was really an array, etc. But that's what the ":String" does for you without the underscore riffraff - you're compiler will fail because you've strong typed during its instantiation.
And then there's those folks that love the underscore so much they prefix private vars with them... and *then* there's the folks that put *two* underscores before...
grr
- hotroddy0
"....the same for function: Void [when no var return's required], Boolean/Number/Strin g etc.... for everything else."
What does this mean? When is a variable return required? I've been seeing [function :void] a great deal but don't really understand it.
- skel0
I use the ":Void" treatment (well... ":void" in AS3) But that may be only because I think it helps declare the purpose of the function better as a non-returning function.
I suppose it helps the compiler help you... if you make like this:
function blah() {
return;
}var foo:String = blah();
Does the compiler throw an error without the ":Void" ?
*too lazy to try*
- hotroddy0
what will the line "return;" do when you include it in a function?