Archive for the 'Quick Tips' Category

Passing …(rest) parameters between functions

Tuesday, April 22nd, 2008

At some point when developing an application with ActionScript 3 you may need to pass a …(rest) parameter to a subsequent function call, and although at first this may appear to be pretty straightforward, doing so will not produce the results one might expect.

For example, consider the following method doSomething which accepts a single …rest parameter:

public function doSomething(rest) : void
{
    var n:int = rest.length;
    trace( "…rest.length = " + n );

    for (var i:int = 0; i < n; i++)
    {
         trace( rest[i] );
    }
}
doSomething("a","b","c");

// outputs:
// …rest.length = 3
// a
// b
// c

Let’s say we also have another function we need to invoke called doSomethingElse which accepts a …(rest) parameter as well:

public function doSomethingElse(rest) : void
{
    var n:int = rest.length;
    trace( "…rest.length = " + n );

    for (var i:int = 0; i < n; i++)
    {
         trace( rest[i] );
    }
}

Now suppose we need to pass the ...(rest) parameter from the doSomething function to the doSomethingElse function. The result would be as follows:

public function doSomething(rest) : void
{
    var n:int = rest.length;
    trace( "…rest.length = " + n );

    for (var i:int = 0; i < n; i++)
    {
     trace( rest[i] );
    }
    doSomethingElse( rest);
}
// doSomething outputs:
// …rest.length = 3
// a
// b
// c

// doSomethingElse outputs:
// …rest.length = 1
// a,b,c

So what went wrong? What happens is the original …rest parameter (passed to doSomething) is now being passed as a single parameter of type Array to the subsequent function (doSomethingElse), not as an Array of individual arguments as you may have expected.

This can easily be resolved by using Function.apply(); as can be seen in the following:

public function doSomething(rest) : void
{
    var n:int = rest.length;
    trace( "…rest.length = " + n );

    for (var i:int = 0; i < n; i++)
    {
     trace( rest[i] );
    }
    doSomethingElse.apply( null, rest);
}
// doSomething outputs:
// …rest.length = 3
// a
// b
// c

// doSomethingElse outputs:
// …rest.length = 3
// a
// b
// c

What we are doing is applying the call to the doSomethingElse function with the original rest parameter as if we were invoking the function directly. So keep this in mind should you ever need to pass a …rest parameter to a subsequent function.

Web-Based UML Sequence Diagram Generator

Monday, April 14th, 2008

If you need to create sequence diagrams quickly and do not have the time to use the more traditional Software Modeling tools; Together, Enterprise Architect, Visio etc. you should take a look at www.websequencediagrams.com.

This handy little tool is pretty capable for a free web based utility and is very easy to use. It took me just seconds to create the simple sequence diagram below…

Web-Based UML Sequence Diagram Generator

So the next time you need to create UML sequence diagrams in a hurry make sure to check out this very useful tool.

Using multiline values in properties files

Monday, March 24th, 2008

When building localized Flex applications with ResourceBundle there are a few tricks you may not be aware of which can come in handy should you need them.

I have had quite a few people ask how multiline values can be specified in a properties file. This is a pretty common question and luckily the answer is very simple: backslash (\).

For example, suppose you have a properties file which contains a string resource with a really long value, like a paragraph. Typically the property value will just be one really long String on one line. However you can use the backslash (\) character to continue the value on multiple lines in order to make the properties file more legible, such as in the following example:

title   = Welcome
message = Lorem ipsum dolor sit amet, consectetur elit, \
sed do eiusmod tempor incididunt ut labore et dolore magna \
aliqua. Ut enim ad minim veniam, quis nostrud exercitation \
ullamco laboris nisi ut aliquip ex ea commodo consequat. \
Duis aute irure dolor in reprehenderit in voluptate velit \
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint\
occaecat cupidatat non proident, sunt in culpa qui officia \
deserunt mollit anim id est laborum.
 

So if you have properties with very long values remember to use backslash (\) to help clean up your resources.

example

Quick Tip: Cairngorm Best Practice

Thursday, January 10th, 2008

When developing Flex applications in Adobe Cairngorm it is quite common to define all event constants as follows:

public static const LOGIN_EVENT:String = "LoginEvent";

The problem with this design is that there is no way of guaranteeing the event type will not collide with an Event type in another package.

Now, I think it is only fair to say that in a good design there typically would not be two Events with the exact same name. However, when developing large scale Flex applications in which additional modules are added with each major release, not to mention the fact that there can be literally hundreds and hundreds of classes, the chances of creating an Event with the same name begins to increase.

As a best practice you should assign the Event type a value which is identical to the Event’s fully qualified class path. This will ensure there can never be a collision of Event types. An example can be seen in the following:

package com.domain.events
{
import com.adobe.cairngorm.events.CairngormEvent;

public class LoginEvent extends CairngormEvent
{    
   public static const LOGIN_EVENT:String="com.domain.events.LoginEvent";

   public function LoginEvent()
   {
       super ( LOGIN_EVENT );
    }
  }
}

It’s always better to be safe than sorry.