Construction in Programming – Convention over Configuration
0Tonight I was putting up an additional shelf in my closet and I didn’t have all the screws and wall anchors I need due to breaking some of the anchors. Thankfully, construction workers and engineers thrive off of convention over configuration! All I had to do is find the approximate size wall anchor in my toolbox.
Do you realize how annoying and unproductive it would be if every time during a construction project (whether a shelf or a house), construction workers had to cast all their own screws, bolts, joints, etc. No! They build from convention. They build with tools and parts that align to common specs and this allows them great customization.
So, why do programmers often say “we don’t have time” or “[convention over configuration] works in an ideal world”?! If programmers were construction workers, they’d be fired and construction would take even longer (think about that scary idea).
Convention over configuration isn’t a “nice to have.” It is paramount to us being productive, efficient, and effective developers. We should take the lesson and be Program Workers.
Construct away!
Popularity: 11% [?]
Process
0Process 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: 5% [?]
The Power of Separated Logic: Opening the Door
0I have to give the Adobe Flash team big props on the new Spark framework’s focus on separated logic. Not only does it enhance my productivity and sanity, but it has opened wide the door for and encouraged the Flex community to create vast reusable code in ways that Flex 3 had problems encouraging developers to do.
For instance, the Spark separated layout structure with a DI slant has already saved time and encouraged so many layout concepts that are no longer component based solutions (example). The community is now able to focus on solving small solutions and then combining them into greater solution for a given project!
I expect to see more focused classes coming soon.
UPDATE (June 10, ’10):
The focus on solution driven instead of component has allowed Adobe to have features like Layout Mirroring (link). This further demonstrates the power of developing a DI driven framework. I encourage more of us to architect this way. OH MAN, did I really suggest us to architect like Adobe?!
Popularity: 7% [?]
BackgroundAlpha: Why contentBackgroundAlpha?
0Situation: I was working with the Spark (Flex4) TextArea component. I wanted the background to no longer be visible or just simply alpha set to zero. I expected I could set a stylesheet property backgroundAlpha to zero. However, that didn’t work and there wasn’t even a property on the component called that. I dug in the source to find it and realized it is really contentBackgroundAlpha. Why? I see borderAlpha and expect to follow the same convention! I do not see contentBorderAlpha.
Why break a convention?
Why use contentBackgroundAlpha when borderAlpha is present and there is not already a backgroundAlpha?!
Popularity: 34% [?]
Parsing Algorithms – Creational Patterns
0This 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: 10% [?]
Tween Library Preferences and Strategy Patterns
0Problem:
When it comes to Tween libraries, it’s either a project’s requirements and/or developer personal preferences that can conflict in selecting a library. Honestly, the library shouldn’t matter. The importance is that the objects are tweened. This is especially important when it comes to open-source projects, as the issue only compounds where it’s much more likely that both personal preferences and project requirements will both be the issue with the open-source project.
Solution: Use the Strategy Pattern
Example (real-life example): Duncan Reid created a ToolTip UI class for ActionScript3. The problem I ran into was that fl.transitions.Tween was being used and I was working on an AS3 application that is using the now Greensock’s TweenLite. The solution that Duncan and I discussed was to use a Strategy Pattern to decouple the Tween library from the ToolTip class itself and offload the reference onto implementation. In other words, by utilizing the Strategy Pattern, it allows the developer to decide which tweening library he/she would prefer to use. For Duncan Reid’s ToolTip UI I did the following:
- Added New Files:
- com.hybrid.ui.strategies.IToolTipTweenStrategy
- com.hybrid.ui.strategies.ToolTipTweenStrategy – The only place where fl.transitions.Tween is being used/referenced
- ToolTip now has a required constructor parameter tweenStrategy which will accept anything implementing the IToolTipTweenStrategy
This now means that the ToolTip UI class depends on one thing – the Tween Strategy. In fact, the strategy could be set to null (not given to the ToolTip), then no tweening would take place. Instead, the tooltip would just display or hide immediately. As such, the required construction param could be made optional (ie default set to null).
In action this simply looks like
new ToolTip(new com.hybrid.ui.strategies.ToolTipTweenStrategy)
//OR
new ToolTip(new com.novelastudios.ui.strategies.ToolTipTweenStrategy)
Source Files / Source Examples (using Duncan Reid’s ToolTip UI)
- Example of dynamic strategy changing ( visual demo / source )
- Click the Radio Buttons at the top of the visual demo to switch between the different strategies.
- Implementing it into Duncan’s original example
- Using fl.transitions.Tween ( visual demo / source .fla | .as )
- Using com.greensocks.TweenLite ( visual demo / source .fla | .as )
Note
The above source is not officially supported / maintained by Duncan Reid and as such I recommend that this simply be used as an example and refer you to his site and source code for production use.
Additional Information
For more information on the Strategy Pattern with ActionScript3 checkout the following:
Popularity: 100% [?]
The Genius of Programming
0This post is simply a quote from my brother-in-law Forrest Kyle, which he kindly spent time writing out what he said to me on the phone while we discussed architectural practices. I wanted to share this, because I believe it is so well put.
In programming, genius rarely manifests itself in some radical new idea, or some blazing new algorithm that redefines the parameters of what is possible. Often, genius is the visible result of a forceful and determined intensity of focus placed upon mundane details, and making sure software is fundamentally correct. A good sign of a genius programmer is one who is willing to work closely with details that are “beneath him” if it serves the greater good of program efficiency and correctness.
Don’t allow yourself to make a design decision for which you can’t articulate a verbal defense. Imagine yourself explaining why you made your design choices to a panel of super geniuses. If you find yourself at a loss for words, it is likely that future users and developers of your program will find themselves at a loss for patience.
A source file is more than just a set of instructions for your computer; it is a formal engineering schematic and must be crafted in such a way that communication with other developers is as deeply ingrained in the presentation as the function of the program itself. You are not writing source files for yourself, you are writing them for current and future coworkers.
Programmers who don’t feel the need to write clear, helpful documentation are like cats that don’t feel the need to use the litter box.
-Forrest Kyle
In closing, this really is not just a great explanation of solid programming, but of communication and thought out action.
Popularity: 11% [?]
Invoking Constructors
0RJ 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: 13% [?]
Rasterize a Transformed Vector without Pixelation
0How does one rasterize a transformed vector capturing it’s new dimensions without pixelation?
Note: This follows my previous post Matrix Not Synchronized
Challenge: To render a bitmap of a transformed vector display object without pixelation. Specifically, after a mx.controls.Text is transformed, a bitmap snapshot is needed to be taken of the new object including the dimensions. I should note that the reason I am needing to get a bitmap snapshot is do to some shaping to the text.
Problem: However, doing so results in pixelation or resamples / renders the original dimensions of the transformed Text. It appears that flash.display.BitmapData::draw cannot get the new transformed display. In other words, when a flash.display.InteractiveObject is scaled on the x axis (or any axis) using transform.matrix, flash.display.BitmapData::draw cannot capture the new dimensions of the display object.
Example: http://sources.novelastudios.com/flash/matrix_adustments/MatriciesAndDimensions.html#
When the application creation complete is fired a snapshot is made. Change the matrix width (transform.matrix.a) and then re-render a snapshot / bitmap. Notice it retains the original, non-transformed values. Therefore and considering it is a rastered snapshot, when the snapshot/bitmap is scaled it becomes pixelated. Example version was 1.1.0.0 that should this problem. Version 1.2.0.0 is a workaround for now showing the workaround.
Workaround: I give credit to Guy Stables (needs a link) and Alex Harui (http://blogs.adobe.com/aharui). Essentially, the workaround is as follows:
- The InteractiveObject (we’ll call our target) needs to be placed in a container. In this example mx.controls.Text is placed inside a mx.core.UIComponent.
- The matrix transform is then applied to the target (eg mx.controls.Text)
- The dimensions of the container (eg the UIComponent that contains the Text/target) are then set to the target’s pixelBounds dimensions.
- Finally, take the snapshot bitmap of the container (the UIComponent).
For a demonstration see the latest version 1.2.0.0 of the example.
More Information:
Also found at http://forums.adobe.com/thread/521432
Popularity: 19% [?]
Matrix Not Synchronized
0Create a UIComponent transform the matrix’s a value and then inspect the width. Notice that there is no change
Note: With UIComponents you can change the explicitWidth when the transform takes place to perform a workaround. However, that’s not the point.
@Adobe Include event dispatching for matrix transforms!
Popularity: 9% [?]