Programming


30
Aug 08

How to get rid of mx:Script blocks in your MXML

UPDATE: Okay, so maybe I just wasn’t really paying attention when I’ve googled this in the past, or maybe I just know more about Flex now and I better understand what’s being discussed in the comments… but there’s a blog post about code-behind that draws exactly the distinction I outline below and in the comments, some of the Flash/Flex gurus go through the pro’s and con’s. It’s reassuring, at least!

I know, not a very clever title, but I want this post to be easily googled as I would have been elated to get this information 2 years ago when I started using Flex. If anyone know where I could find these details elsewhere, please let me know, as this is exactly the technique I’ve wanted for a long time.

There’s a debate in the Flex community about how AS3 code should be mixed with MXML. There’s a code-behind camp that suggests first creating a base class containing your AS3 code (in a .as file) which your MXML code then extends. This has all sorts of drawbacks, like having to declare all instances you want the AS3 code to access in the base class, which kinda defeats the whole purpose of separating the MXML and AS3! Then there’s the code-in-front camp that suggests having your AS3 code inherit from your MXML class. Same issues appear, though, just from a different direction.

And finally, there’s the more common approach of just using an ugly <mx:Script/> block in your MXML, which is ugly because AS3 code has to first be wrapped in a CDATA block because of the special reserved characters used (angle brackets, double-quotes) and things like “Organize Imports” doesn’t work. The end result is a very ugly chunk of non-declarative code in the middle of nice declarative XML.

Of course, some may argue that means you should just probably slice up your code along MVC lines and then you’ll have a minimal amount of code in your MXML. Sure, but the reality is that it’s never that straightforward, and besides, even with a proper amount of code I’d like to have the automatic import organizing.

And I almost forgot… when you’re working in a team situation it’s likely that the MXML and the underlying AS3 are developed by different individuals (designer and developer, respectively). Sure, the merging won’t be too heinous with the script block, but it would still be required (this is another argument for code-behind/in-front).

So today, entirely by accident, I found the solution. If you include an AS file from exactly one other file (either an AS or MXML file), the Flex Builder editor will treat that included AS file as if it was actually in that file in regards to auto-complete. In other words, if you hit CTRL+SPACE while working in the include file you’ll get the context-appropriate auto-complete pop-up as if the file was already included where you used the include directive. If you refer to the included file from two different places in your code, then it won’t work (you get whatever is common between the locations).

Oh yeah, and organize imports works perfectly.

So, now in your MXML files you can just do this:

<mx:Script source="my-source-code.as" />

And put all your AS3 code there, neatly tucked away from all your MXML. Nice. All you gotta do is come up with a naming scheme to keep your AS and MXML files grouped (for your convenience only). The MXML compiler actually produces an AS file with the same name as the MXML file, so you can’t use that. But you can use a clever variation (that doesn’t appear to break anything). The pattern I’m using right now is this:

MyViewComponent.mxml
MyViewComponent.mxml.as

Cool, huh? It almost seems too good to be true. Am I missing something?


1
Aug 08

I guess I’m a hippie.

In the Perforce Software Newsletter, Robert X. Cringely writes about 3 types of programmers: nerds, hippies and lumpen programmers. I’m definitely a hippie. And I can’t wait until we can hire a few nerds to do my bidding… ;-)


19
Jul 08

Domino Logic

Fairly brilliant geek stuff…


12
Jun 08

An Interesting Theory of Code Stability

Michael Feathers recounted a conference run-in with one of the original XP and TDD practitioners:

John Nolan gave his developers a challenge: write OO code with no getters. Whenever possible, tell another object to do something rather than ask. In the process of doing this, they noticed that their code became supple and easy to change. They also noticed that the fake objects that they were writing were highly repetitive, so they came up with the idea of a mocking framework that would allow them to set expectations on objects — mock objects.

That’s a fascinating suggestion, writing OO code with no getters. To think of it in object terms, that means that you create objects that don’t have information extracted from them, they’re only asked to do things. If you take his statement literally and to the extreme, you have the scenario where you don’t know the state of a separate object you can only tell it to do things and hope it does them (you can’t confirm it because there’s no gettable state!). Hmm… that sounds familiar.

I think event-driven programming achieves the goal of “writing OO code with no getters.” It has the added bonus of being dynamically (and anonymously) coupled at runtime. And, in fact, I’ve found event-driven code to be the least brittle. Definitely more complicated in some scenarios (usually trivial cases, while being less complicated for the non-trivial cases), but always more stable, higher quality, fewer bugs.

AS3 has a particularly beautiful event model. When an object dispatches an event the listeners for that event are called immediately (inside of the dispatch function) as opposed to being queued and processed at a later point in time. Callbacks, basically. But at the same time you have to write them as if they occur asynchronously.

I’m not sure the point I’m trying to make… other than to say that event-driven programming is less fragile?


7
Jun 08

Functional Programming in AS3

I’ve never done a lot of functional programming. Sure, I did a bit of required Scheme in my Programming Languages class in college. Didn’t do anything real though — well, maybe a bit of decent AI stuff, but no actual apps. The point is, I’ve never really worked extensively with functional programming largely because the languages/tools I’ve been working with haven’t been purely functional.

Well, apparently I’ve just been missing it in AS3. It was staring me plainly in the face but hadn’t really thought about it in those terms. Bruce Eckel did, though. He shows how we can use the various Array methods (and add some of our own since Array is dynamic) to work functionally. There’s some very clever stuff in there, particularly the in-line functions that leverage closures to return results to the calling method… very clever use of AS3.

There is quite a bit of noise in the article related to his working with Flex Builder and writing the MXML to display the results of his tests. He also has a very — aheminteresting code style that I wouldn’t suggest anyone follow if you plan on sharing with other AS3 coders. I’m referring to his mixing of MXML and AS3 (and I’m not talking mx:Script blocks, either). First time I’ve ever seen that…


4
Jun 08

This is why I use Flash.

Peter Christensen explains why I feel no shame for moving from the world of cutting-edge console games to web-based Flash games.


6
May 08

Are magic MVC frameworks really helping me?

As a preface, please read this about “magic” code.

It wasn’t that long ago that I gushed about Cake. But after doing some development with it (beta sites for Mockingbird), and after recently working on our current site (which runs on top of Pligg, kinda), I’ve started to kinda think, “hey, what are these guys actually doing for me?”

First, I am still baffled that with all of the auto-sniffing-magic that these frameworks (Rails, et al) do I still have to describe by database schema in two places. I mean, come on, Microsoft solved this problem in .NET. Sure, they do it through code-generation (XML database schema creates the actual underlying database as well as generating all of the C# classes used to interact with it in a completely typed, validated way). But with Cake (and with Rails, AFAIK) I have to go off and do DB admin creating tables and indices and such, and then I have to come and create some parallel classes in the framework. Sure, I don’t have to specify the individual fields, it’ll automatically attempt to map unknown properties for me… unless I want to build-in validation, or relationships, or serialization… you know, trivial little things that only serious webapps would need… ahem. Sure.

Second, I’m just terrified by the amount of decision-making that is made each time “through” the framework when one of my pages is served up. Sure, op-code caching alleviates some of this, but really, should I be executing 10x the necessary amount of code to save me from having to write out a dozen lines of completely self-explanatory, easy-peasy lines of code?

Maybe I’m missing something… maybe these guys are doing a lot of stuff for me I’m not realizing. But I get the itch’n feeling that they’re really just providing a different syntax (with some defaults that make building a bare bones blog in 15 minutes a snap) on bits that are already there, at a lower level.

Routing — mod_rewrite/.htaccess is the only routing tool you need. I almost guarantee you’re not doing anything in your webapp that requires some kind of routing logic that can’t be handled elegantly with mod_rewrite/.htaccess.

Controller — this is the PHP file that your mod_rewrite directs queries to. All you need to do is practice some self-control and keep all of the aesthetic bits out of this PHP file. Just do the work, the logic of the user’s request. When you done all your work, just pass the results off to the view.

View — go download Smarty. Just use it. Flickr uses it and they get more traffic than you ever will. It’s a templating engine designed with the viewpoint that the template should *only* be the view and that the designer and programmer are at least independent tasks if not entirely different individuals.

Model — I was very frustrated with Cake when I tried to combine it with Pligg. Pligg had a particular table/field naming scheme, and Cake had a similar (but different enough) scheme. There was no combination of tweaking that would get one to be compatible with the other. Writing the necessary SQL queries to bridge the gap manually was trivial. And I knew them all along – in fact, I used them when debugging the naming scheme in an attempt to work backwards. :) Just grab ezSQL, that’s as simplified and automatic as your database interaction should be, because the database is critical (and powerful) if you don’t discount it as just a big property list.

Helpers, Components, Behaviors &mash; these frameworks all have a bunch of additional “utility” features that basically just make life easier as a webapp developer. These could be worth it, if they’re you’re particular flavor. But I bet for each one there’s a strong, healthier open-source version that does more.

Maybe I should put my money where my mouth is? Maybe so. How cleanly could I build a webapp without a framework (and without implicitly building one myself)? In other words, have we heaped one abstraction too many on the pile?


3
May 08

Magic Thinking

Code on the Road: No Magic

Amen, brother.


3
Apr 08

The Craft of Gamemaking

I presented this evening at the ACC. I think it went well. I’m definitely out of practice. The slides probably won’t be interesting without the talking, but in case anyone is curious: The Craft of Gamemaking.

If you saw the presentation, I’d love to hear from you in the comment section.


19
Nov 07

Too good to be true? Box2D for AS3

Just caught this on the feeds today (sorry, forgot where): Box2D for AS3.

Just “wow.” I had been jealous of the C++ guys with Box2D as it’s a very impressive, cleanly written 2D physics API, something Flash really needed. Sure, we’ve got Fisix if you want to pay for it, or APE if the feature set is right (and it’s what we’ve been using for the last year). But Box2D always appeared to be the best of both worlds: feature-full and open/free.

And seeing the demos on the page, it appears to be pretty freak’n fast as well. I’ve not downloaded it yet and done any tests, but my gut is telling me its 2x to 3x faster than APE, which is really impressive considering that it’s rigid bodies (as opposed to APE’s particle-based approach).

I’m looking forward to what the community produces with this resource, and I’m looking forward to getting to play with it myself!