Quick Tip: Correct folder for Unit Tests in Flash Builder 4 4

I really like the fact that FlexUnit is built right into Flash Builder since one of the main reasons unit testing used to fall off our radars was it was a pain setting up Flex Unit and executing scripts. However one of the things that struck me pretty weird was that Flash Builder was writing tests in the source directory. Best practices in unit testing say that the tests should be in a separate folder called tests/ which should be a sibling of the src/ folder. This lets the tests be written with the same package as the code being tested. This allows, for example, testing functions with a package level visibility.

The correct way to get this working is, when starting the project add another folder called tests for the build path. Now Flash Builder will look at both the source and the tests path when looking for .as files. To write a unit test Test Case, right click on the tests folder and click on the new Test Case option.

New folder for Unit Tests

[Update] I have filed a bug in Adobe’s ticketing system to track this issue. Please vote for it if you agree: http://bugs.adobe.com/jira/browse/FXU-50

Compiled vs interpreted languages and why I sometimes wish Actionscript was interpreted 5

[UPDATE] My friend, Mat pointed out that this post isnt really about compiled vs interpreted languages but rather static vs dynamically typed languages.

Its always interesting to see the conflict between developers who love and those who hate interpreted languages. It often feels like the developers’ equivalent of a generation gap, with a lot younger developers preferring interpreted languages like Ruby and Python over things like Java and C. An interesting post I read on why Web Applications Should Be Compiled which is an interested read, though a bit strongly worded against Ruby. I quote:

The whole philosophy behind Ruby is that the machine should be the servant, not the coder. This is madness. We’re engineers, and we make machines work. You use the best tool for the job to do that. You don’t choose some pretty language that “brings back the joy of coding” or equivalent hippy shit. This is analogous to hiring someone to build a house and having him tell you he’s not going to use conventional materials like wood and steel because they aren’t as fun to use as Play-doh.

The performance argument is often valid. No ruby developer can claim that it can outperform its compiled brethren, though there are signs that this could be changing, with news like JRuby outperforming Ruby C. The core reason people who love interpreted languages is that it gets the cruft out of the way, allowing developers to think more of the business problem.

So how does this debate affect me, considering most of my work is in client side languages like Javascript or Actionscript?

The last few months I have been working with Javascript and have slowly started appreciating it quite a bit. One of the reasons I wasn’t pulling my hair out with cross-browser javascript is because of the JQuery library. JQuery syntax is really nice and I love the chaining feature. For example the purpose of the following line should be pretty self evident:

$(".mydiv").width(200).height(200).bind(click, function(){ alert("I got clicked") }).css("visible", true);

The actionscript equivalent would be something like:


mySprite.width = 200;
mySprite.height = 200;
mySprite.addEventListener(Event.CLICK, function(event:Event){"I got clicked"});
mySprite.visible = true;

I hate the zig-zag i have to do to read AS3 code, so while extending OpenPyro I figured I could make my methods return the object instance back, that could be used to further set properties on the object.

So say my UIControl had a function like setSize(w:Number, h:Number):UIControl and setVisible(val:Boolean):UIControl, I could use them in chaining like:


myControl.setSize(200,200).setVisible(true);

nicer right?

But now imagine you extend UIControl to make it a button and add functions for setLabel(str:String):Button and setIcon(icon:DisplayObject):Button. Note the return types have to be Button so that I can do something like this:

myButton.setLabel("print").setIcon(printer_png_instance);

Now for the fly in the ointment: Lets try chaining this:

myButton.setLabel("print").setIcon(printer_png_instance).setSize(200,200).setVisible(true);

works fine. but

myButton.setSize(200,200).setVisible(true).setLabel("print").setIcon(printer_png_instance);

fails. Why? Because the compiler sees setVisible returning a UIControl but then trying to set the label property on that control. At runtime, the returned instance is a Button but the compiler cannot check that. The only way to do chaining would be to cast objects appropriately for the compiler but at that point, the elegance and readability of the single line is lost.

And that is the core problem. Compilers are stupid and completely depend on the return type information defined in the method signature. I could return Object datatypes for all methods but the performance hit would be pretty severe.

As I write this a new direction comes to mind. Doing $(”#mydiv”) in JQuery doesnt actually return a div but rather a jQuery wrapped set. Maybe a AS3 equivalent could be created using Actionscript 3’s Proxy class

Hmmm…stay tuned for a followup post.

Epilogue:
Returning for a bit to the previous quote chastising developers on choosing Ruby, a counterpoint can be found on why developers at Twitter chose Scala:

The other big reason we looked at Scala was that, although we’ve run into problems with Ruby, we like the flexibility of the language. We like that it’s such a full featured language, that it’s fun to code in. … We know that people write super high performance code in C++, and engineers like Steve and Robey have had experience with that. But we wanted to be using a language that we’re really passionate about, and it seemed worth taking a gamble on Scala.

I think programmer productivity is a factor of more than the programming languages, its also dependent on the developer’s passion for the language.

New Adobe toys this week: Flash Builder 4, Flex 4 SDK, Flash Catalyst, BrowserLab 0

An exciting week for Flash Platform Developers as Adobe released a number of Beta versions on the Adobe Labs site. I have just started playing with a couple of them but its definitely worth the shout out.

Flash Builder 4:
Flash Builder 4 is the next version of the Eclipse based IDE formerly known as Flex Builder. The renaming is more than just a re-branding exercise, as FB4 seems to have added features that make it a lot easier to work with other frameworks besides Flex. This is really exciting for me personally as this means it will be a lot easier to work with OpenPyro in FB4. In fact Greg even created a simple project with AS3 only classes that could be exposed as MXML tags. The resultant file size is minute.

Besides that, the my favorite extension to the IDE has been the inclusion of FlexUnit, the unit testing framework for Flex. While FlexUnit itself has been available as a library for a while, inclusion into the IDE will make unit testing a lot easier and hopefully a lot more prevalent.

There is also a network monitor built right into the IDE. While I have used Firebug with success for Flash apps on the web, doing the same for AIR apps has been a challenge (though Charles has been really good, though a requires a bit of a context-switch when coding). The network monitor is going to make a lot of lives a lot easier.

Integration of ASDocs is another great feature, as well as code generation for integrating with serverside code.

[Update] There are also a bunch of improvements to the Debugger on FB4 which you can read more about here.

Flash Catalyst:
Flash Catalyst is a new tool that Adobe is releasing with the promise of truthfully translating designer interactions to stub code that a developer can add functionality to. Basically Catalyst can import any Photoshop, Fireworks or Illustration file, and then can allow the design to assign behaviors to the different parts. For example the designer can select four graphics and mark tham as the four parts of a Scrollbar. The interactions are written out as Flex4 tags that can be compiled into a swf with complete interactivity.

The video below shows how Catalyst can be used:

Flex 4:
The two releases of course directly depend on the Flex 4 SDK, Adobe’s Open Source framework for building Rich Internet Applications. The new release of the SDK introduces a new component architecture called Spark. The core difference between the Spark and the earlier Halo architecture is the favor of the composition as a design pattern over inheritance. What this translates into in everyday development is a leap in productivity as complex UI components can be created a lot easier than before and not requiring things to be subclassed as often. This also makes skinning a lot easier, since all the skin parts can be swapped pretty easily. Another new thing in Flex 4 is the introduction of a new graphics interchange format called FXG, an xml format for graphics that can be understood even by the CS4 products like Photoshop and Fireworks. Matt Chotin’s post on changes in Flex is a lot more comprehensive.

BrowserLab
BrowserLab, a project formerly known as Meer Meer, is a new service being released by Adobe that targets HTML developers. BrowserLab allows developers to preview any URL as rendered by different browsers. The service is in limited beta, but I was able to get an account before it was capped, and I can tell you its pretty impressive. It would be interesting to see how Adobe can integrate this with Dreamweaver, although its supposed to remain open to mortals like myself who still prefer TextMate ;)

So a lot of toys on the labs site. Take em out for a spin and let me know what you think :)