You are viewing the Articles published in

Flex Unit TestSuiteHelper

Test Driven Development is an essential part of any software project. Leveraging Flex Unit in your Flex and AIR projects is a well known best practice and will inevitably result in less unknowns and provide a much more reliable codebase, however like most things in life that provide value, unit testing comes at a cost: Time.

In general, writing Unit tests is often an overlooked process as it ultimately equates to an additional level of effort (time) in a project plan. However, writing unit tests should not be over looked for a number of reasons, and in my opinion most importantly because it forces developers to understand what problems the code they write are intended to solve, thus resulting in a better understanding of the problem domain.

Now without getting into a lengthy discussion about the benefits of Test Driven Development, as there is plenty of information available on the subject, I am going to focus on how to make it easier in Flex.

Typically when I write a unit test I stub it out and then run the test runner to make sure it fails. I pretty much do this immediately after I write the test and then I see that my test did not come up and realize I never added it to the test suite. Obviously something like adding tests to a test suite can be an automated process which would allow unit tests to be developed a little faster as all that would be needed was to write the test.

So in an effort to simplify the process of adding tests to a test suite I have developed a TestSuiteHelper which is a simple utility class that automates the process of adding unit tests to a test suite. Rather than manually adding each test the TestSuiteHelper will automate the process simply by invoking a static method and passing in the test class from which you need to extract all of the tests.

So if you would like to simplify your team’s unit testing workflow feel free to utilize the TestSuiteHelper.

API Design: Method parameter objects

When defining a constructor or method with more than three parameters it is considered a best practice to create a parameters object for holding the values which are required. This especially holds true when some of the parameters are optional (as they will contain default values) and also when two or more consecutive parameters are of the same type (as developers may accidentally transpose these parameters – which will not result in compile time or runtime errors, making debugging a nightmare!)

The most appropriate solution for such cases is to break up the method into separate methods, however when this is not feasible creating a parameters object will improve both code readability and provide a cleaner design which leaves less room for unexpected errors.

Consider the following method:

The parameters defined in this method are typical of what you will often see, however it is much cleaner and reliable to create a parameters object for holding these parameters. In addition, a parameters object allows for a much easier client implementation as default values need not be reassigned for all parameters which proceed a parameter which you need to specify a value other than the default value. For instance, if a method accepts 5 parameters and the first two parameters are required but the last three are optional, if you you need to specify a value for the last parameter you will need to re-assign the default values for the proceeding parameters, when in fact you really only need to specify three parameters.

The following example demonstrates creating a parameter object which can be passed to “someMethod”:

When invoking “someMethod” clients can now simply instantiate an instance of the parameter object, set values only for what is needed and pass it in as follows:

So if you would like to improved code readability and provide proactive exception precautions, consider utilizing parameter objects for methods which require more than three parameters.

Quick Tip: Cairngorm Best Practice

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

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:

It’s always better to be safe than sorry.

Quick Tip: Flex 3 IDE

I have added a new Category to my blog called “Quick Tips” from which I plan to post various quick and simple, yet useful little things I come across from time to time.

This first Quick Tip has to do with the default state of the “Mark occurrences feature” in Flex Builder 3 (the feature which causes all methods, properties etc to be highlighted in blue).

Upon installation of the latest Flex 3 beta one of the initial features I noticed was the Mark occurrences feature. Personally I found it to be quite annoying and somewhat intrusive. In addition the feature was also pretty slow (however I believe this is currently being addressed).

In any event I wanted to disable this feature, problem was I couldn’t figure out exactly how to turn it off! After some exploring in the preferences panel I was still unable to find where the Mark occurrences feature could be disabled, so I posted a new topic on the Flex 3 pre-release forum, and luckily I got a quick response from Laurence Mclister explaining where the feature could be disabled – it was right there in the toolbar!

Mark occurrences can be turned off via the toolbar button which looks like a yellow highlighter it is typically toward the center of the toolbar). Below I have highlighted in red where the Mark occurrences feature is located on the toolbar.

Mark Occurrences

Hope that saves someone some time down the road.