19 December 2008
Written by
Richard Leggett (

)
Published on December 19th, 2008 @ 05:22:31 pm, using 874 words, 462 views
I’ve got a stack of posts scribbled over some paper that I’ve been meaning to blog about for months now regarding the MVP pattern (and ViewModels). But more on that later, because what is core to MVP is of course MVC, that most misunderstood of “patterns"…
Well this one is for new-guns and MVC vets alike, Cliff Hall presents on MVC as it is applied in PureMVC, perhaps one of the most refreshing frameworks to come to light in the Flash realm, and it is spreading to many other languages, ports include Python and even JavaScript/JQuery in the works.
I’ve started a re-write of a desktop application I worked on some years ago that was around 50,000 lines of AS2, I can’t even remember how many commands were in there but it was quite heavy and the “front controller” took a battering, so it’s a good challenge to put these concepts into practice and take advantage of the new possibilities, such as web and AIR deployment from the same code-base.
Before watching the video, if you are new to PureMVC I suggest you read the “best practices” document so that you get the most out of it, the most confusing aspect of PureMVC is perhaps the class naming (although the class names are logical when you know the terms). For example, most of your code will go in subclasses of Mediator, Proxy and Command. I’ll take each one in term:
- Mediators - These “steward” your views and keep them simple, i.e. take a user interaction such as a click, and do something in your application, and the reverse, take an application event and tell a view to do/display something.
- Proxies - These provide access to your model, local or remote, and can perform domain logic.
- Commands - These are executed by the controller to perform work e.g. store some data in a Proxy. (more on this later)
You can see now how these 3 types fit into the M.V.C. pattern very clearly, unfortunately the naming can at first confuse.
Without further ramble, here’s the link to the recording. The order felt a big reversed to me, so if you get bogged down hold out, he goes on to explain the core concepts later on. Cliff speaks with enormous experience and succinctness so even if you’re already comfortable with all of this it’s good to hear from other devs that have been through the trenches.
I guess this is a good opportunity to discuss MVC, it’s almost a dirty word now with people almost sick of hearing it, and interestingly most of us get burned when we try to “apply” it in Flash, struggling for perfection. The thing to remember is that it is not an implementation pattern, you can’t create an MVC structure per-se, and when I see people firing up a new project by creating a “Model” class that they make a Singleton and chuck all the data in I cringe* - it is in fact a guiding set of rules that dictate how the component parts of an application can be separated, governed, and how they can communicate.
*Yes PureMVC has a Model singleton/multiton, and I’m still thinking heavily on this design with regards to TDD. But either way it’s not the sort of bad singleton you see when newbies write their _global Model monoliths, instead its a simple cache for your actual models, your Proxies and as such is not the damaging monster you tend to find and it doesn’t reference any classes outside of those PureMVC.
Many of the books and articles written on the subject of MVC refer to server-side development. There’s a potential sting in the tail if you try to apply this directly to client-side, and Cliff points out in the recording that on a client application the Model becomes even more important. Remembering that on a client application the model persists, not just the data, but the state of the application, the history of the users actions perhaps, it can be much more complex and potentially confusing than when you’re working directly with a database. On a client application you have to constantly maintain a “context", users interact asynchronously in different contexts, that’s where the programmer skill comes in, and that’s why MVC helps by separating these , and why PureMVC helps by providing not just a framework designed around the principals of MVC, but also many helpful principals about where to write your code and when!
The forums are particularly useful. One simple example you encounter early on is setting data from a view… so my (view-) Mediators can access my (data-) Proxies directly, why bother sending a notification from my Mediator so that the Controller executes a Command to access the Proxy to set the data? Well a Command is a good way to bundle up some logic/actions that may get called from more than one place, more than one Mediator in this case. As such you’re avoiding duplication and edging towards more testable code.
I think that’s enough, this has been done to death over the years but it’s good to bring it up now, ultimately the most important rule is to remain pragmatic.
That video link again.
09 December 2008
Written by
Richard Leggett (

)
Published on December 9th, 2008 @ 11:33:39 am, using 340 words, 804 views
At my last job I met a software architect who, for a Flash project, jumped in and learned ActionScript in what must have been a few hours so he could work on the domain logic for a complex application. Just for background, I believe he primarily worked with .NET but you know how programming works… hours spent * natural ability = language independent skill.
Suffice to say he was indeed an extremely good coder and I’m grateful he spent some time passing on a few tips and tricks that apply to all languages, and some nice demonstrations using Haskell, ActionScript and of course C#. One such tip was the use of “pure functions” for example, which is always in the back of my mind now. (As a side note, I think learning a very different language like Haskell or in my case Ruby is a great way of keeping your mind open and it will help with your “daily” language).
Another of those tips went something like this: “hey, you know these switch statements, you can pretty much get rid of switch statements in an OO application using polymorphism". For those a little unfamiliar with that term, in ActionScript this could be achieved by subclassing a superclass several times. This statement turns out to be, on the whole, true. A switch statement can a lot of the time be reflective of a problem in the architecture, a coupling of code, and by simply introducing interfaces and polymorphism into the equation you can refactor it down to maybe 2 lines from however many switch-cases. Overall there may be more code in separate classes, but it’s a lot more elegant, maintainable, extensible, readable, and most importantly, testable.
That brings me to the subject of this post. Making testable code is actually one of the best ways of writing *better* code. The Google Testing Blog has some of the best posts I’ve ever read on the subject of better coding. The latest post is on the very subject discussed in this post.
The Google Testing Blog.
Written by
Richard Leggett (

)
Published on December 9th, 2008 @ 10:57:21 am, using 124 words, 575 views
Caught this over at BoingBoing, it’s a nice Flash-based simulation that runs through rapidly evolving a vehicle design, using a physics engine to test the thing out over some terrain (not sure whether it’s home-rolled or one of the big ones).

Quote from BoingBoing article, from Matthew the author:
“This is a GA I wrote to design a little car for a specific terrain. It runs in real-time in Flash. The fitness function is the distance travelled before the red circles hit the ground, or time runs out. The degrees of freedom are the size and initial positions of the four circles, and length, spring constant and damping of the eight springs. The graph shows the ‘mean’ and ‘best’ fitness.”
Check it out here.
07 December 2008
Written by
Richard Leggett (

)
Published on December 7th, 2008 @ 06:52:09 pm, using 237 words, 563 views
I got a tip from my friend Alex Peretti regarding the built in search in OSX. Skip to the end if you are familiar with the default search pains.
I couldn’t understand why, when you start typing in the search box inside any folder’s Finder window, it defaults to searching inside files of the entire computer for matching text, instead of looking at filenames inside the current folder. That means every time I search for something I have to change the settings (they aren’t stored) because 99% of my searches seem to bring up some wordy Nietzsche e-book instead of what I’m looking for.
The screenshot below shows the default search options for context.

My guess is they do it like this so that search works almost every time for the average Joe who doesn’t tend to have thousands of source files and libraries (particularly given Spotlight is insanely fast anyway). Either way, for the power-user help is at hand. This search syntax works inside Spotlight or a Finder search:
- name:hello - find files who’s filename contains “hello”
- kind:action - find ActionScript files
- date:today - find files modified today (or Apps used today)
- date:>1/1/08 - find files modified after 1/1/08
No need for wildcards, although I imagine they work too. You’ll find Spotlight searches are now lightening fast and hopefully more relevant. Cheers Alex.
Update: There are more commands available, this page has a good summary.
20 November 2008
Written by
Richard Leggett (

)
Published on November 20th, 2008 @ 10:11:57 pm, using 268 words, 621 views
It’s that time of year again. “Give good food to your Mac” is back with another set of software to choose from. The twist is the more you buy, the cheaper it gets, literally. Choose 3 apps, save 30%, 4 gets you 40% but 5+ gets you 50% off. It’s time limited, so 10 days to go.
Last year I purchased 10 pieces of software for the price of the 1 piece I actually wanted (Unity 3D). In that package I also got an impressive casual 3D modeler, Cheetah 3D. It won’t replace Maya or MAX, but for a lot of tasks it’s perfect. Yes some of the apps on there are a little gimmicky, some of them you may never use, but it really doesn’t matter if you’re getting the other apps for free (in my case anyway).
So what’s on this years list? My picks would be:
If you’re in the market for any of these or one of the other utilities on there you might consider the 50% off a deal clincher. Disclaimer: I have no affiliation with this site nor its creators.