Why Strict Data Typing is Good
I must admit, my first introduction to strict data typing was in JSP and at the time I HATED it. But it wasn't until I got into strict-typing in actionscript that I realized "hey hey.....wait a minute" - trust me when I say it has more then one purpose then cluttering the code.
In fact, when utilized to it's full extent, you can minimize your code by preventing common code problems like unnessecarily making 2 or more variables of something that could just be the same, along with other common coding "typo's".
Take for example this simple "hello world!" code:
-
myVar = "Hello World!";
-
trace(myVar);
Simple? Yes, it is. And if this is all the code you had to write and this were a perfect world, then I see no harm in keeping it that way. However lets say that was only 2 of about 100-300 lines of code, well sure its still readable, but if you used strict typing on everything, well its kind of like having road signs on the highway, it gives you direction, it labels a variable, array or whatever your contraption may be without you having to dive through your code to find what the hell the variable hahaYouDontKnowWhatIAm is for.
Here is the above code using strict data typing:
-
var myVar:String = "Hello World!";
-
trace(myVar);
Alone it doesn't look like much, I know, but try using it in your next endeavor in actionscript and you will see what I mean.
Another great use of strict data typing is with functions, especially when you are passing parameters that should be strict, although it is usually a rare event. Here is an example of using strict data typing with a function:
-
function myFunction (myParam1:String, myParam2:Boolean) :Void
-
{
-
if(myParam2)
-
{
-
trace(myParam1);
-
}
-
}
Notice the ":Void" after defining the function. This specifies that there is no return from this function. However if you wished to return something you could simply put the type of data you wish to return, such as Boolean, String, Number, Array and so on.
Strict typing is also the ideal coding to debug, for example lets take something like this:
-
var myNumber:Number = "Blah Blah";
-
trace(myNumber);
This will throw an error, however if you didnt make it strict then it would let u pass with setting a variable that should be a number, to a string value. This is helpful and preventive and applies to any use of strict data typing.
Along with strict data typing comes other good practices of code, such as initializing variables on the proper level, BEFORE using them. You don't have to set it to anything, infact you could just put the following, just to set its proper scope:
-
var myVar:String;
After getting into the habbit of strict data typing, I find myself getting picky with code that isnt using it, mainly because I see the code is not at it's full potential.
Strict data typing is most useful in classes, or I should say it is required with classes...well in most scenario's, but that is another post, on another day :).
I encourage all actionscript coder's to make this their new habbit of coding, you will be happy you did.

