Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Motivation

The refactoring Replace Type Code With Property Flags is used to do the following:

  • Denormalize for performance. When you want to improve performance, by having a column for each instance of the type code. For example, the Address.Type column has values of Home and Business; it would be replaced by isHome and isBusiness property flag columns. This enables a more efficient search because it is faster to compare two Boolean values than it is to compare two string values.

  • Simplify selections. Type code columns work well when the types are mutually exclusive; when they are not, however, they become problematic to search on. For example, assume that it is possible for an address to be either a home or a business. With the type code approach, you would need the Address.Type column values of Home, Business, and HomeAndBusiness, for example. To obtain a list of all business addresses, you would need to run a query along the lines of SELECT * FROM Address WHERE Type = “Business” OR Type = “HomeAndBusiness”. As you can imagine, this query would need to be updated if a new kind of address type, such as a vacation home address, was added that could also be a potential business. With the property flag column approach, the query would look like SELECT * FROM Address WHERE isBusiness = TRUE. This query is simple and would not need to be changed if a new address type was added.

  • Decouple applications from type code values. When you have multiple applications using Account.AccountType of Figure 7.13, making changes to the values becomes difficult because most applications will have hard coded the values. When these type codes are replaced with property flag columns, the applications will only have to check for the standard TRUE or FALSE values. With a type code column, the applications are coupled to the name of the column and the values within the column; with property flag columns, the applications are merely coupled to the names of the columns.

    Figure 7.13. Replacing the AccountType code column with property flags.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint