Archive for the 'Framework' Category

220-BPM - a new flash framework is born

Monday, November 12th, 2007

I had an idea for a flash framework, and while I’m not fully there yet, the first of proof concepts at TriMM are very promising. It solves a lot of the problems I can think of up till now, so I’m curious to see where this will go.

Design patterns were clear enough, but their implementation is often less than straightforward. Take an MVC pattern. Have you ever found one straightforward implementation? Instead of 100k hits on google all with a slightly different twist? The same patterns that should make it easier to communicate with your coworkers come back to haunt you, since responsibilities in classes are slightly differently allocated than you are used to, mediators are called controllers, and everything is untyped or abstracted away for the sake of ’simplicity’ etc…

The ideas I’m currently having are in blueprint phase including an almost working example as well. Just a quick overview of the design goals that are (more…)

New Behrloo Developments

Friday, November 2nd, 2007

There are some new exciting developments going in Behrloo. The number of hits in Google seem to grow every day, and we have some exciting new plans for upcoming releases. I wish I had more time to develop content and cases myself, but being the (frontend) technical architect of a system this big is challenging enough as it is.
At the moment we are diving into the controversial subject of application frameworks, which is something really enjoyable to sink your teeth until it’s dark outside and everyone’s sound asleep :)

We had some very nice talks with Stefan Richter from FlashComGuru last week as well when he came over to TriMM to discuss one of our other projects called YPKA. We managed to sneak away to a mexican dinner for a while with the other Flash Geeks :) to share ideas etc.

All in all an exciting and good week, which ain’t over yet, so if you’ll excuse… I’m off back to work :)

Flash Frameworks Revisited

Saturday, October 20th, 2007

On my long list of plans for this year was to dive into the world of Flash Frameworks.
Only thing is that it has taken me so long, that the world has moved on to Flex Frameworks, but that’s beside the point.

I wanted to dive into frameworks because I heard all these people talk about how great frameworks are and how they’ve helped people build great applications etc.

A quick recap of my first impressions/experiences: (more…)

Wrapping function calls in AS2

Friday, August 24th, 2007

Also known as:

  • debugging function calls
  • displaying the name of a called function
  • tracing function calls director like

Today I finally got to try something I had been wanting to try for a long time: wrapping function calls in a nice way.

I had been playing around with function pointers etc, which was ok in itself, but I was forced to create the messy pointer bit each time over again. So today I implemented a FunctionWrapper class.

Say we have a class Test, with a method testMethod.

Now we want to execute some code before calling testMethod and after calling testMethod. “Well write another function to do so”, you might say. Of course that is one option, but assume we are looking for another way to do so (I’ll discuss some scenario’s in a minute).

Using function pointers you can let the Test.testMethod reference point to your own method and from your own method execute the original method, pre and postpending it with some functionality. I think this is a bit like the AOP mechanism, but I’m not too sure since I am a composition filter freak by nature :) .

The FunctionWrapper class takes care of all this reference shuffling for you:

FunctionWrapper.wrap (
TestClass,
"testMethod",
_preFunction,
null, //no additional args to pre
_postFunction,
null //no additional args to post
);

Now I won’t explain a lot more about that on this page, since I’ve got an example for download here, which contains all required classes and documentation. Note that the example requires some support classes (more…)

Preventing dynamic property collision

Monday, July 9th, 2007

We sometimes have to deal with dynamically added properties at runtime, and looking into the code of some frameworks, we are not alone in this. However, whereas our packages are carefully structured to provide classname collisions (according to the standard reversed domain name convention), my experience is that developers take no such precaution when it comes to adding properties to objects at runtime.
So it’s very probable for two different frameworks to add say a “className”, or a “hashCode” or a “key” property to classes or objects.

Now the odds of using two frameworks together might seem slim, but on the other hand its a very subtle bug waiting to happen. You might not be using 2 frameworks, but you might use a component based on another framework loaded at runtime that does etc.

I dont know if any of you are using any convention to prevent this, but we have recently started to use a very simple convention to make sure this doesnt happen, and I thought I’d share it.
Instead of object[”key”] or object[”className”] etc we have recently started to define these as:
private static var _key:String = “_”+[insert classname here]+”.”+[insert propertyname here]

so for example:

private static var _key:String =
"_nl.trimm.util.SessionUniqueIdentifier.key";

and then later on

myObject[_key] = ....

I’d like to hear if there are any of you who are using similar conventions or how you deal with this ‘problem’ (problem is a too big of a word for such a minor detail, but still).

Some of the subtle errors on collisions like this are 256 recursion errors, hanging applications, not working in general etc.
In my opinion a convention like this is especially important for sources you plan to release to a bigger audience, where these sources act upon the properties of all classes and objects from somewhere other than the class or object itself. As said above classes that fall into these categories are hashcodefactories, reflection utilities, wrappers and there might be more.

Flash Reflection package

Friday, July 6th, 2007

In the ongoing work on our internal framework, this week has been dedicated to rewriting the reflection package. A reflection package in general is responsible for providing you with names for your objects.

For example if you use:
ReflectUtil.getName (Stage);

the result would be:
“Stage”

Very handy for loggers and stuff. As a matter of fact my current reflection package is the package integrated in Xray). That implementation is pretty solid & simple however some things are bothering me about it, and I have partially solved them at the moment. I don’t think it’s going to replace the simple package anytime soon, but I do know the new package rocks.

At the moment it can tell the names for all packages, classes and functions and whether a function is static or not. No references are kept between the object being reflected and the reflection object itself.

In addition methods are provided to resolve a reflected object to the object being reflected and vice versa. It includes a basic visitor implementation so to act on the class tree you no longer have to deal with all the nitty gritty details of methods, properties etc.

At the moment I’m working on optimizing the implementation and synchronizing the source after new code loads. I’m not sure whether I should override the loadMovie function for that or implement a Command queue, with a simple RefreshCodeTreeCommand.

kk back to work now, I’ll post the result to the flashcoders list when it’s done.

flash try-catch

Tuesday, June 26th, 2007

Flash Try Catch not catching custom Error Subclasses.

Last week I’ve been working on a custom error class for flash (as 2). This error class will become the key error handling mechanism in our framework. The base error class in our framework will be a RuntimeException.

What makes this class special is:

  • uncaught top level errors can be dispatched to a catch all handler
  • it contains the error cause, and the parent error
  • it contains a subcode

Not all that special however the first item makes this an (at least to us) indispensable asset in our framework.

Normally people have different ways of handling errors in Flash:

  • they ignore the fact errors occur and remain blissfully ignorant
  • they return some kind of errorcode or provide a method for error detection
  • they use the try-catch exception mechanism

In my opinion the easiest way to handle errors is to:

  • handle them at the level they occur
  • throw an Exception and let the caller handle it

Ignoring the fact errors can occur, or adding lots of methods to the signature of an object to allow (but not force) a client to detect errors are not really options I’d like to consider.

However throwing exceptions involves some intricacies, the two most important being:

  • it requires knowledge of lower level library errors and thus couples high level code to your libraries if you are not aware of this issue. In practice this usually means converting errors from one level to the next, or catching errors in general and not specific errors. So instead of throwing your webservice exception all the way to the frontcontroller, you catch the exception, convert it into a more general DataSourceFailureException and throw that.
  • it requires knowledge THAT code throws exceptions at all. RuntimeExceptions are not checked (checked exception do not exist in Flash), therefore the compiler does not enforce them.
  • displaying uncaught exceptions

The base error class in Flash is the Error class.
If you use:

throw new Error("test");

your swf will show “test” in the trace output BUT only if you run in the IDE.

What our RuntimeException class does is the following:

  • it extends Error, so you can use it through throw new RuntimeException();
  • it sets an interval to run after the current callstack unwinds, pushing itself onto a list of not yet handled errors
  • it allows itself to be consumed, removing itself from the list of not yet handled errors

After the current callstack unwinds, the interval kicks in. Two things might have happened.
One, the error may have been caught and consumed :


catch (e:....) {
//show and handle error here, never just catch it, that negates the use of error handling
....
e.consume(); //consume the error so it doesnt propagate any further
}

Two, the error may not have been consumed.

If the error has been consumed, it would have removed itself from the list of unhandled errors.
The moment the interval kicks in, all errors that are still unhandled at that moment are passed to an error handler.
If the RuntimeException class cannot find an error handler, it creates a default error handler, which does nothing more than create a textfield at the stage and print error information to it.

In effect what this does is that you can throw exception throughout your code, which you should catch and handle.
However, if you forget to catch one, it will show up at runtime on your stage instead of remain hidden.

One step further and you can imagine it is very easy to implement an Assert class with an AssertionFailedException.

The assert class can be used in your code, for example:

public function setParent (pParent:MovieClip) {
Assert.assert (pParent != null, "Parent movieclip cannot be null.");
.....
}

If you fail to pass a reference, the assertion will fail and a message will show up on your screen. Combining this with our reflection package, the message on our screen looks something like:


ASSERTION_FAILED in MyClass.setParent, called from MyClassB.myMethod, with parameters ......

Ofcourse this check could be even more extensive including runtime type checking, but we should wonder how far we should take this. In most cases, compiling using MTASC with the strict flag, most type checking will already have been done. Ofcourse calling the method like:


setParent (myClips["clip")

circumvents typechecking, and might cause some subtle errors which do not trigger exceptions.

In some cases, adding a type check is easy:

Assert.assert (MovieClip(pParent) != null, "Parent movieclip cannot be null.");

However if you do this using an array:

Assert.assert (Array(pArray) != null, "Given array cannot be null.");

it will work as expected in MTASC but not in the Flash IDE, since MTASC will cast it, but the Flash IDE will create a new array for you. In other words, this would require you to implement some more extensive checks.

Which brings me to another framework class:
ReferenceValidator

The Reference validator is an alternative to the Assert class, specifically created for testing references for null and their types.

A common scenerio is where you wrap a clip with your own screen class. The screen class needs a reference to the clip and the components on it, which is where the ReferenceValidator comes in. It copies all the references for you from the clip to the wrapper, testing the values for their types and null (if required).

Ok, that’s it for now, I hope to post more on the framework coming into being at the moment.

Before I forget:
One major bug I encountered is that in order to catch a custom error you have to specify the full path to the error.
So in other words:


catch (e:MyCustomError) {

}

will not work, whereas :


catch (e:my.full.package.to.my.error.path.MyCustomError) {

}

will work.

Very annoying. Ah well such is life.