|
-
OOP is one letter from OOPS
Data Typing
Ok so in some of my examples you may remember seeing some thing like:
var _myName:String;
This is an example of data typing. Simply put with the above syntax, if I try to assign anything other than a string value to that variable, the compiler will throw an error. So if we:
//declare a variable that can only hold a string
var someString:String;
someString ="Hello" //ok
someString = 5 //error 5 is not a string
someString = "5" //this is ok since 5 in this case is the string five and not the number 5 since it is " "'s.
So why do this. OK well take the following situation:
//declare some untyped variables that can hold anything
var holdsAnything1 = "2";
var holdsAnything2 = "5";
var total = holdsAnything1 + holdsAnything2;
trace (total);
rather than producing 7 this would trace the string 25 (in the context of string, the + sign joins to strings together, in fact in testing this, you can actually leave the quotes off of either the 2 or the 5 and you will still get 25, it is not until you take the quotes off both that you get 7, least in flash 8). Point being what is you inadvertently assign the wrong type of data to a variable that should only hold a certain type. How would you figure out where the assignment went wrong? Well you could start just looking through all of your code........ or use typing as that will throw an error at the point were the assignment goes wrong.
//note we could have used the same names from the above example, but since I have been giving them names related to the current point, I will continue to do so. The important part is the :Type
var mustBeNumber1:Number = 2;
var mustBeNumber2:Number = 5;
var total:Number = mustBeNumber1 + mustBeNumber2;
trace (total);
With the above setup, the compiler will throw an error at the point we try to assign the undesirable value to the specific variable and will point us to the line on which it occurred.
Other benefits of typing include:
(See: http://www.oman3d.com/tutorials/flash/variables_bc/)
One should not underestimate the benefits of strict data types, they do not only prevent you from assigning an incompatible data type and make error correction much easier, but they also make your code much easier to read because as the intended purposes of the variables that you create become clearer and easier to follow. A great other benefit of using Strict Data Typing is that variables to which this feature is enabled activate ActionScript quick reference code hints that pop-up when the variable name is written in ActionScript.
And finally, this will also work with you own custom classes that you create. So from out previous examples. I could:
//create a variable to hold an instance of a class I have created:
var someBody:Person = new Person("Typed Variable");
with the :Person syntax, I can only ever hold an instance of the person class that I created in that variable.
Now just as one final note as there can be endless debate back and forth as to whether typing is useful (some people like, some hate it, and there are those in between). There are occasions where you may want a variable that can hold multiple types of data. And if that is what you want and have designed for it, then that is equally as valid.
For an indepth look at the topic of typing see:
http://en.wikipedia.org/wiki/Type_system
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|