ActionScript, Flash, and Flex
Posts tagged Suggestion
Process
Aug 29th
Process is like any philosophy: You have one even if you say you don’t care about it or think it causes more weight to consider it.
Popularity: 1% [?]
Parsing Algorithms – Creational Patterns
Mar 27th
This is as much a recommendation as a rant, so pardon me while I do both
Situation
Open-source project or foundational classes for use within your organization that contains methods for parsing data.
Background
Often, I’ve run into issues where I am using an open-source library (public or private) and I need to add extra logic to a creational algorithm(s) and cannot or cannot easily do so. This is what has sparked me to write this post, because I am increasingly seeing these types of issues.
Recommendation
When creating algorithms that create objects from other objects (eg models and serialization) , please use offload the logic to accessible and easily modifiable methods. In Design Pattern terms, I’m requesting and ranting that the Factory Method Pattern, Strategy Pattern, or something similar be used. This only compounds in importance when there are several objects created in a single method! <– That right there is arguably bad practice in general for large projects.
Example
NOTE: Showing a simple example seems to not show the problem, but I’ll try. For a better example, please refer to “Real-Life Example” below.
Mapping XML properties to models within a project.
protected updateList():void
{
var header:Header = new Header;
header.title = _xml.header.@title;
header.lang = _xml.header.@lang;
header.colors = _xml.header.colors.toArray();
var body:Body = _body;
var child:XML;
for (var i:Number = 0; i < _xml.body.item.length(); ++i)
{
child = _xml.body.item[i];
body.getItemAt(i).text = child.@text;
}
}
Real-Life Example
NOTE: I do not mean to pick on or criticize the fine work done by the peeps working on the OpenVideoPlayer Project and MUST EMPHASIZE that the project is very useful and saves time. This merely serves as an easy example to show. Moreover, I’ve written my fair share of problematic code along these lines too.
Popularity: 11% [?]
Invoking Constructors
Nov 26th
RJ Regenold and I were discussing last night how we both strongly believe constructors should have extremely limited algorithms that simply invoke protected methods.
For instance, PureMVC’s Facade initializes the instance in the constructor
package org.puremvc.as3.multicore.patterns.facade
{
import org.puremvc.as3.multicore.core.*;
import org.puremvc.as3.multicore.interfaces.*;
import org.puremvc.as3.multicore.patterns.observer.*;
public function Facade( key:String )
{
if (instanceMap[ key ] != null) throw Error(MULTITON_MSG);
initializeNotifier( key );
instanceMap[ multitonKey ] = this;
initializeFacade();
}
}
package org.puremvc.as3.multicore.patterns.facade
{
import org.puremvc.as3.multicore.core.*;
import org.puremvc.as3.multicore.interfaces.*;
import org.puremvc.as3.multicore.patterns.observer.*;
public function Facade( key:String )
{
if (instanceMap[ key ] != null) throw Error(MULTITON_MSG);
initializeNotifier( key );
initializeInstance();
initializeFacade();
}
protected function initializeInstance():void
{
instanceMap[ multitonKey ] = this;
}
}
I would even argue that the Error checking / validation takes place in an init of some kind as well.
Another example is as follows
Instead of doing,
package
{
import flash.display.Sprite;
public class ViewUI extends Sprite
{
public function ViewUI()
{
super();
width = 100;
height = 200;
}
}
}
Do this
package
{
import flash.display.Sprite;
public class ViewUI extends Sprite
{
public function ViewUI()
{
super();
init();
}
protected function init():void
{
width = 100;
height = 200;
}
}
}
This practice becomes increasingly important in open-source projects. Moreover, this example may be limited but consider more complex constructors that you’ve seen or written yourself. The construction process should be divided into segments when possible.
UPDATE ( 01.13.10 ): Bill Sanders recently posted a good article about this exact topic!
Popularity: 23% [?]
Most Recent Comments