3.1 Why Static Typing?

 <  Day Day Up  >  

There's plenty of debate about whether static typing (compile-time type checking) is better than dynamic typing (runtime type checking). But there's no question that a programmer benefits from being alerted to potential or actual type mismatch errors. Next to syntax errors ( grammatical and typographical errors), datatype errors are the most common kind of error in an object-oriented program. If a syntax error occurs, a program won't compile. If a type mismatch error occurs, a program may be able to run, but it wouldn't likely be able to perform one or more requested operations. Such errors often lead to program failures. Some compilers merely warn of type mismatch errors but still allow compilation to continue. However, ActionScript 2.0's static typing facilities prevent the movie from compiling if type mismatch errors exist. Compared to ActionScript 1.0's complete lack of type checking, this is a great step forward for the language.

You must address all type mismatch errors to get your movie to compile. If type checking is new to you, you may feel that this requirement adds unnecessary work. You might be tempted to simply eliminate the datatype declarations that are causing the type mismatch errors in order to get your movie to compile. That's a little like going upstairs when your basement is flooded. Avoiding the problem doesn't solve anything in the long run. Here's a laundry list of reasons that explains why you should stay the course and learn to love type checking:

  • Type checking guarantees the validity of most operations at compile time, catching potential errors before they ever occur. Once a program type checks (i.e., compiles without producing type errors), most careless errors are eliminated, so you can focus on correcting any errors in logic.

  • Specifying type information throughout your code helps to ensure you follow your own OOP architectural plan. Similarly, it guarantees you'll use another developer's class as intended. If you misuse a class, you'll hear about it from the compiler.

  • Type checking reduces the amount of manual data-verification code you need in your methods (though you'll still have to do some data verification, such as when parsing user input or server results).

  • Some programmers find static typing easier to read because statically typed code expresses important information about values and dependencies in a program's source code.

In Flash MX 2004, when you specify datatypes in your code, the Flash authoring tool provides code hints for built-in classes and components . A code hint is a pop-up menu that lists the properties and methods of objects as you type them. This isn't a traditional OOP benefit, but it's a nice side effect. It also eliminates the need to use suffixes such as "_mc" to activate code hinting, as was necessary in Flash MX.


As noted earlier, static typing is not universally accepted as the best way to implement type checking. The jury is split, but many well-known programmers argue in favor of dynamic typing over static typing. Dynamic type checking is often less restrictive and can make code easier to change than statically typed code. Those arguments aside, ActionScript 2.0 will be statically typed for the foreseeable future because static typing is part of the ECMAScript 4 specification on which ActionScript 2.0 is based. However, if type theory debates titillate you, you should enjoy these articles:

  • Static Type Checking , by Dave Harris, at http://ootips.org/static-typing.html

  • Strong Typing vs. Strong Testing , by Bruce Eckel, at http://mindview.net/WebLog/log-0025

The main advantage of static type checking is that it can check all your code at compile time, whereas dynamic type checking requires that you execute the code in order to verify it. To be sure that all your code is executed, you need to perform unit testing on each module (writing classes specifically to test the output of other classes). Mr. Eckel argues that combining religious unit testing with dynamic type checking offers all the error-catching benefits of strong typing and more. For a quick-and-dirty example showing how to write a unit test in Java, see http://c2.com/cgi/wiki?CodeUnitTestFirstExampleTwo. For an ActionScript 2.0 unit testing tool, see http://www.as2unit.org.

In some languages, static typing improves runtime performance. ActionScript 2.0's static typing does not improve runtime performance.


 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net