Flex Application View Organization
The way I develop software regularly evolves as I learn better practices, and this is particularly true in the way I organize view code in Flex applications. I've seen (and built) too many applications where the code is unncessarily difficult to jump into and maintain. Inspired by domain-driven design and test-driven development, I've been organizing view code in such way that I believe helps maintainability and also helps answer the question, "Where should I put this component?".
Here are some guiding principles:
- The view code should reflect how you talk about the application with the entire team.
- The view code should reflect how you interact with the application.
- Repeating yourself can be a good thing.
- Components and events should be contextual.
Let's way we have an application that manages contacts. There's a list of all your contacts on the left. There's a list of recent contacts on right right. The selected contact's detail shows up in the middle. There is also a Twitter feed widget in the contact's details. Here's a wireframe:

Here's how I would create the view package:
com.foomonger.contactmanager.view.ContactDetailsView
com.foomonger.contactmanager.view.contactdetails.events.ContactDetailsEvent
com.foomonger.contactmanager.view.contactdetails.TwitterFeedWidget
com.foomonger.contactmanager.view.AllContactsView
com.foomonger.contactmanager.view.allcontacts.ContactItemRenderer
com.foomonger.contactmanager.view.RecentContactsView
com.foomonger.contactmanager.view.recentcontacts.ContactItemRenderer
So what did I do? I created components that reflected the wireframe. There is a component for each major section (all contacts, contact details, recent contacts). I named the Twitter component "TwitterFeedWidget" even though I thought the "widget" part was dumb but that's what the product owner and overall team calls it. I also created a ContactDetailsEvent class within an "events" package specific to the ContactDetailsView component. That may be a bit extreme for some people, but I find that it helps me keep track of the purpose and scope of view events. I also created a ContactItemRenderer for both the AllContactsView and RecentContactsView. Even if the code was identical in each item renderer, I prefer to keep them distinct instead of having a common one in a generic "itemrenderer" package. I argue that what I lose by repeating myself I gain back in maintainability. If there was a common ContactItemRenderer, after many months of new feature development with several developers it'll be easy to lose track where it's used. If the item renderer has to do something different in one particular case you can of course add properties and states to keep it multipurpose. However I've found this to be regression prone. If you have distinct contextualized components, you can be more confident that a change will not cause regression elsewhere.
I used to always look for even the slightest resemblance of a pattern and abstract all kinds of code into shared components. But I've realized that developing an application is different from developing a component library or a framework. In a component library you can have a generic color picker. In a framework you can have a generic "itemrenderer" package. I'm not saying you can never do these things in an application, but I am saying that I think application development should be very (domain) specific.
I would agree that there is a distinction between application development and framework or component library development. In one you’re trying to achieve a specific business goal and in the other you’re providing tools for others to accomplish their business goals a little more easily.
What I think is lacking in your argument is how to maximize re-usability. If each team member is creating similar components or writing similar classes with slight variations between them for the sake of maintainability, that seems almost like wasted effort to some degree.
In many cases, maintainability is a laudable goal, but I’d like to think that there should be a better balance between maintainability and re-usability. Perhaps it is achieved through better communication between developers and better documentation of what’s there and what changes need to be made to keep existing code working *and* extending them for new purposes.
What do you think?
Johnny Boursiquot
1 Dec 09 at 10:59 AM
With good documentation and good communication I think there can be more reused code, but I think that effort would have to be done very very purposefully, i.e. clearly creating a component architecture, supporting docs, clean code organization. This reused code should probably even be in a separate code-base than the application code to further distinguish what is reused and how to maintain what. Personally I haven’t seen this executed well very often which I think is the norm. If you have a “performing” team and the right project, then I think it can be more appropriate and successful.
Sam Ahn
1 Dec 09 at 11:53 PM