Feb 5

Strict typing is good mkay

Posted by Cliff

I’m currently in the final stages of a refactor and I thought I’d just share this little tidbit on why strict typing is a good thing in AS3, especially when working in an agile environment where things can change quickly as requirements unfold and mutate.

Let’s say you’re working with data via AMF, and you have a Project value object with the properties ‘id’, ‘name’ and ’status’.

public class Project {
  public var id:Number;
  public var name:String;
  public var status:String;
}

In no time at all the rest of your code will be littered with references to these properties, and obviously if any of these properties changes during refactoring will be instantly picked up by the compiler. No biggie refactoring those. But there are instances where changes can slip under the radar.

For example (examples are always good), take a look at this snippet:

private function on_result(event:ResultEvent):void {
  this.status = event.result.status;
}

See the subtlety? In this instance, event.result is an Object, which is dynamically typed. Therefore accessing erroneous properties will not be picked up by the compiler, and this ultimately leads to subtle, frustrating bugs and loss of hair.

To avoid this, ensure you type cast dynamic objects to their appropriate types before accessing properties thusly:

private function on_result(event:ResultEvent):void {
  this.status = (event.result as Project).status;
}

Now if the status property is renamed during refactoring, the compiler will instantly pick it up and the world will seem a little brighter.

3 Responses to “Strict typing is good mkay”

  1. Daniel Tenner Says:

    Ah, that’s a very good point.

    Mind you, I imagine a thorough suite of unit tests would pick this up too :-) Rails, for example, has no compile-time type-checking, but makes it very easy to construct thorough unit tests. So perhaps the strict typing could be forgotten about if Flex made it very straightforward to write unit tests…

    Hmm…

    Daniel

  2. Julien Says:

    Very interesting one :)
    Thanks for the tip & good luck for the next iterations man

    ttyl,
    Julien

  3. Cliff Says:

    @daniel Yep, unit tests will almost certainly catch most of these cases. I’m still a fan of strict typing though, and I wouldn’t want to see AS3 go back to the dark days of AS1 ;-)

    Also, it’s a matter of workflow. In Rails, continuous integration testing is very easily accessible - it’s a simple matter of running autotest. Running Flex unit tests first requires compilation of the test project, then execution of a test runner. Whereas in Rails, your first level of defence is your instant notification via growl. In Flex, my first level of defence is the Flex Builder ‘problems’ panel. It’s just quicker.

    That’s not to say it’s not possible to do Rails style continuous integraiton in Flex- it just requires more resources, and some Ant fudgery. Something you and I will look at in the very near future :-)

Leave a Reply