Accessibility
Jeanette Stallons

Jeanette Stallons

stallons.com

Table of Contents

Created:
16 November 2009
User Level:
Intermediate

Refactoring Flex 4 applications

Refactoring is the process of cleaning up your code. It is not changing or adding functionality to your application. It is rewriting your code so it is more logical, readable, understandable, and extendable. In most cases, if your application is not broken, you leave it alone; there is no need to invest the time or risk the chance of breaking it. Typically when you do refactoring, then, is when you need to add functionality to an application. You refactor the application first and then add new functionality after. It's easier to restructure code without introducing bugs if you do it in small steps and don't try to change any functionality at the same time and it's easier to add or change functionality when you have clean, refactored code making it simpler and faster to extend and debug the application.

There are many different ways to refactor an application: you can move code from one place to an other, you can create new classes and member of classes; you can replace logic with shorter, more flexible code, you can remove dead code; you can rename classes and its members to more logical names, and so on. Martin Fowler, a well-known expert on refactoring, has catalogued many of these techniques on his website and his book, Refactoring: Improving the Design of Existing Code, the de facto reference on refactoring.

In this tutorial, you are going to refactor a simple Flex drawing application. The beginning application has all its code in the main Application file. The finished application has its code in multiple class files. You start with the more conceptually difficult refactoring tasks of extracting and abstracting code into more reusable, smaller pieces, move to the more straight-forward refactoring tasks of moving and renaming files to make the code more readable and understandable, and finish with the simplest way to clean up your code, by formatting it consistently. The refactoring tasks have been organized into some basic sections: Extract, Abstract, Move, Rename, and Format. These are groupings that I created in order to help give you a framework to introduce the different types of refactorings you can do. Many of the refactoring tasks, however, could be considered to belong to more than one of these groupings. For example, when you extract code from one location, you move it to another. In addition, most people would say formatting your code is not actually refactoring, but since this tutorial is all about cleaning up your code, I am including it as well.

In this tutorial, you will:

Requirements

To complete this tutorial, you need the following:

Flash Builder 4

Tutorial files

Prerequisite knowledge

A basic knowledge of creating Flex 4 applications is helpful. For more details on creating and customizing Flex 4 applications, see the Flex 4 documentation, the Flex 4 in a week video series and exercises, and the Adobe Flex Developer Center.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

About the author

Jeanette Stallons is an independent Flex trainer and consultant who has taught for many companies including Adobe, Oracle, Boeing, Wachovia, Morgan Stanley, and Charles Schwab. Prior to working for herself, Jeanette worked at Allaire, Macromedia, and then Adobe in the training department, architecting, writing, teaching, and building applications for Flash, Flex, and other products. Her latest projects for Adobe include the Flex 4 Test Drive; the Flex 4 trial sample applications; the Flex 3 Learning Paths application; and content for the Flex, Flex and Facebook, Flex and salesforce.com, and Flex and Java developer centers.