In looking at pieces of the MM framework, and how a lot of developers write their own code, I’ve noticed a lot of magic string usage. What do I mean by a magic string, let’s examine the following piece of code for a definition:
In this example, “change” is the magic string. The reason that it is a magic string is that code is dependent on that string being correct, but yet there is no formal assertion that it is correct. For example, if in the example I mispelled “change” as “chnge”, my code would fail silently. Though this may not be catastrophic, you are left to your own debugging methods to figure out what went wrong. Fortunately, there are several ways to remedy this. The easiest and most common is to implement some sort of constant variable in your code to represent the string. For example:
// definition : mx.controls.ListBox.EVT_change = "change"; myComponent.addEventListener(mx.controls.ListBox.EVT_change, this);
In this example, the dispatching object actually has a static property EVT_change that is equal to the string and which is used to represent the string. If you use this method and type in a property that doesn’t exist, you will get an error. So with little effort you can have strict typing, better debugging, and more relaxation time.