Flex Best Practices
I came across this video recording of James Ward presentation on Best practices with Flex. Do take the time to go through the complete video
I came across this video recording of James Ward presentation on Best practices with Flex. Do take the time to go through the complete video
Loose coupling is a practice that very strongly compliments OO principle “Programming against Interface, and not against an Implementation”. Two components are said to be loosely coupled when then are developed as interfaces that can be used for transferring data between the two. This involves lesser risk to the fact that change in one would lead to a change in the other module.
In Flex world, you can achieve loose coupling either by using Interfaces or by Events. Both, are very effective ways of getting around the same problem. People like me coming from Java/C# background find it easier to work with Interfaces due to our long association with the concept.
I personally have started to like events a lot better, we they make the components truly loosely coupled. By defining an event on a component, you do not have to worry about coding against an interface. All, you should be aware of is the event that component exposes. You can dispatch the event and you would be done. Let is see how this helps, by way of an example:
<mx:application xmlns:mx=”http://www.adobe.com/2006/mxml”>
<mx:Script>
<![CDATA[
private function onClick():void
{
// do something/anything
}
]]>
<mx:Script/>
<mx:Button id=”btn1″ click=”onClick();” />
<mx:application/>
You would notice that when the button was created (by Flex library), it just exposes an event that you can handle. Rest is taken care by the framework and you do not have to worry about any interface, yet you can use the same component anywhere you want to.
A very trivial, yet a very essential best practice. Also read my entry on best practices.
Creating layouts in Flex should be easy, specially when you are using Flex builder and drag and drop capabilities. But, it is a little tricky than it seems and if you choose to ignore some of the finer details, I promise you will be stumped mid-way. While, I found some articles, but I still find something consolidated that I can get into my head. These are all scattered away (maybe I can not pin-point to the right stuff yet). Here is my attempt to put down in black-n-white some guidelines and rules as to what you should be doing and what not?
While all the feedback and comments are welcome, I would appreciate if you can collaborate with me on this one for us to have a comprehensive set of guidelines
Flex default layout manager, containers and children follow sizing and measuring algorithms to layout the pages and this exercise can be very resource intensive. You never know the client (PC) that will be running your application. Also, what you can not do is to have a minimum requirement like 2GB ram on all systems that run the application, because that would not be possible practically. This is not a game but an application that business users will work on.
Rule 1: Avoid unnecessary container nesting
Coming from an HTML/CSS background it is easy to fall prey to this habit when the objective is to have flexible designs. One example where things can go wrong very quickly are
When you have such kind of nesting, the containers run measuring and sizing algorithm on children. Now, if you have some of the children as containers themselves this can be very time consuming. This will also be a performance issue if you are choosing to resize your screens as the same algorithm is executed again.
You must be asking what should we be doing if we find ourselves in such a situation? Look for alternatives. You should re-evaluate your choice of containers, a better option is Constraint based layouts which makes use of Styles, Horizontol & Vertical alignments, margins, and spacers.
Rule 2: Do not use absolute layout techniques
This technique needs you to define co-ordinates (x,y) for objects. While, this technique is most performant as the there is no need for the player to find out positions for the children, it is not a recommended practice. This approach will not provide you flexible designs and re-sizing the browser window will not resize and re-layout the objects. Try avoiding this one. This approach can be used only with Canvas or Application with layout=”absolute”
Also, you can choose to hard-code widths and heights. This also is a quick win solution because the player need not calculate sizes of objects. In many applications, you will find that this is anyways needed, because, you would not want to extend/shrink the size of your controls like TextInput with changing browser size.
Rule 3: Make wireframes before you start making layouts
This is a very handy and good practice. If you can, then make sure you have a paper-layout (wire frame) of your application design. This will help you in understanding the application design and assist in choosing a container appropriately
Rule 4: Use Navigator containers
Navigator containers like Accordion, TabNavigator and ViewStack has built-in deferred creation policy. This means that all the controls are not created at application start-up, but are deferred until you have activated that view.
Rule 5: Use Box when you have line up things in one direction (horizontally or vertically)
You should be using Box, HBox or VBox only when you have to logically group some controls together and place then in a layout. Do not use nested HBox and VBox to do placements on the screen.
Rule 6: Use Constraints if you have applications that can change size
You can use top, bottom, left, right to set anchor point on the application. This will ensure that your containers and controls would be anchored to the borders as defined by you. Let us say that you have set a control with left=10 and right=10, this will mean that as you resize the application, the size of the container / control will change.
I will try to code an example and upload the same for you to view in some time.
As in any other language, use of design patterns is also recommended in Flex. Some of the most widely used and successful pattern in Flex world are:
Caveat: You can not use Singletons together with Modules. Doing so would mean that multiple instances of the same module will be referencing to the same model and hence data causing issues on the views. This is one area where you would have to careful not to tip the balance.
I would be blogging away with more detailed viewpoints on each of these in times to come.
Disclaimer: Please note that the best practices listed above are purely my ideas/opinions and not recommendation by Adobe or Sapient. You
There have been times when I have thrown away my code. Did you ever face this situation – I am very sure you did, as every developer does. Something that I have sweat over, written to deliver results, is ready to be discarded now thrown because I no longer need it or I find better ways of doing the same thing.
I never mind hitting the delete key on a code; actually when I reach that decision, I start to enjoy the experience as I realize that I am now evolving. I have faced this situation many times and it was only the first time that I was hurt. I still remember almost 3 years back when my Architect told me to scrap everything and start afresh, I was aghast, but now when I look back I realize how good a decision was that.
Today, as well, I am facing a similar situation, when I am looking at improving a code base which will bring in performance improvements to the application I am working upon. While the effort is a significant one, and I have many other things to take care of, the task itself is fun.
Do not get carried away with this and start doing it with your code, do it when it makes sense.