Tuesday, April 29, 2008

Specifying Variable, Parameter, and Function Types in ActionScript and UML

ActionScript is generally a very easy language for Java developers to pick up because of many similarities including syntax similarities, common object-oriented features, ASDoc similarities to Javadoc, and other common features. However, there are a couple small things that can seem a little strange to the Java developer who is starting to use ActionScript. These include the need to declare local variables as const or var and the placement of a datatype after the name of the variable, parameter, or function that type applies to.

While placing the datatype of the variable or constant after the variable's name or the constant's name seems a little strange to Java developers, it is the prescribed way of expressing type in the Proposed ECMAScript 4th Edition Language Overview. Likewise, placing const or var is also called out in this newer in-work version of ECMAScript (ECMAScript Edition 3, the specification behind most flavors of JavaScript, was finalized clear back in December 1999). In fact, Firefox/Mozilla JavaScript already supports use of const and all major flavors of JavaScript support var.

What may be even more interesting to Java developers transition to ActionScript is that the ActionScript syntactic approach to expressing data types matches the UML approach. As shown in Class Diagram examples (Figures 2 and 3) of UML Basics: An Introduction to the Unified Modeling Language, the data types for class data members and for method return values are expressed following the data member name or function they describe with a colon connecting them. The diagrams in the previously referenced article don't show it, but the same is true for parameters to methods -- the data type of each parameter is expressing after the name of the parameter with a colon between the name and type.

In NetBeans 6.1, the UML tool allows for "Platform-Independent Model" UML diagrams to be generated or the developer can select "Java-Platform Model" UML. If the platform-independent UML is used, the data types for the data members are expressed following the data member names. Likewise, the data types follow the methods and following the names of parameters in a method signature. In other words, class data member, function return values, and function signatures look very similar in platform-independent UML as they do in ActionScript. The next screen snapshot (click on it to see larger version) shows how this platform-independent UML looks very much like ActionScript syntax.



When one uses NetBeans 6.1's Java-Platform Model UML capabilities to generate a Java UML diagram, it looks Java-like as shown in the next screen snapshot (click on it to see larger version). Note also the class descriptive text in the lower left corner. Also note that the NetBeans 6.1 IDE automatically adds the getter/setter method for the Java class attribute in the Java platform UML.



When the UML diagram above is generated as code using the NetBeans option of "Generate Code..." provided by right-clicking on the UML project, the following code is generated (note that the class documentation is included as Javadoc documentation for the class):


/**
* <p style="margin-top: 0">
* This class is being created in NetBeans 6.1 using its UML tool to
* demonstrate UML representation of a Java class.
* </p>
*/
public class SomeJavaClass
{
private int aVariable;

public SomeJavaClass ()
{
}

public int getAVariable ()
{
return aVariable;
}

public void setAVariable (int val)
{
this.aVariable = val;
}

public void doSomethingToAVariable (String aVariable)
{
}
}


While ActionScript syntax for expressing data types of class members, function return types, and function parameter types may seem a little strange to Java developers at first, the fact that this same syntax is used with platform-independent UML should help with the transition.

No comments: