advertisement

Print

What Is Prefactoring

by Ken Pugh, author of Prefactoring
11/15/2005
Prefactoring
Prefactoring is the application of past experience to the creation of new software systems. Past experience includes both your own and that of others, particularly lessons learned from refactoring. The experience is captured in a number of guidelines that can be easily applied to the development process.

In This Article:

  1. Extreme Abstraction
  2. Extreme Separation
  3. Extreme Readability
  4. Testing
  5. Retrospectives
  6. Summary

You probably have heard of refactoring, the process of restructuring code without changing its external behavior. Refactoring was made popular by Martin Fowler and his book, Refactoring: Improving the Design of Existing Code. Prefactoring does not imply that you will never need to refactor your code. But it may help you to create more readable and maintainable code, as well as to make design decisions that are more amenable to refactoring.

Prefactoring has a number of guidelines that suggest ways to proceed with your development. The guidelines have been gathered from the experiences of many programmers over the years. My book, Prefactoring, lists guidelines in the areas of Extreme Abstraction, Extreme Separation, and Extreme Readability, as well as general ones. I'll cover a few of the guidelines in this article.

Extreme Abstraction

The "When You're Abstract, Be Abstract All the Way" guideline states that you should define all your data with abstract data types, rather than primitive data types. Abstract data types usually have additional behavior that can be encapsulated within the type. For example, any variable or field that represents a dollar amount should be declared as a Dollar, rather than a double. The Dollar type can include functionality for converting it to and from a string, as well as appropriate arithmetic functions such as add and subtract. The class might appear as:

    class Dollar
        {
        String toString()
            {
            // Format output according to corporate
            // standards, e.g. use minus sign or
            // parentheses for negative values
            }
        static Dollar parseString(String in)
            throws InvalidFormatException
            {
            // Parse string, throwing exception if
            // not formatted according to corporate
            // standards
            }
        Dollar add(Dollar value)
            {
            // Add dollar value
            }
        Dollar multiplyRoundDown(double multiplier)
            {
            // Round the result according to
            // the business rules
            }
        //. Other methods relating to Dollar
        }

With the functionality and details contained within the data type, you are less likely to create duplicate code. If you find you need additional functionality for Dollar, you already have the class in which to put it.

In addition, with abstract data types, your code becomes more readable. You can concentrate on the solution to the overall problem, rather than the specific details. For example, consider:

    Dollar discount = amount.multiplyRoundDown
        (DISCOUNT_PERCENTAGE);
    System.out.println("Discount is " + discount); 

Without the Dollar type, the code might have read something like:

    double discount = round
        (amount * DISCOUNT_PERCENTAGE, 2);
    System.out.println("Discount is " +
        toDollarFormat(discount)); 
Prefactoring

Related Reading

Prefactoring
By Ken Pugh

Pages: 1, 2, 3

Next Pagearrow