Better Software - January 2009 - (Page 12) Code Craft A Path to Readable Code by Ken Pugh Recently I was reviewing the agile development process at a client where the development group was implementing acceptance test-driven development. The client’s agile teams include both a business analyst and a system tester. Working together, they are developing acceptance tests using the Framework for Integrated Testing (FIT). FIT tests are represented by tables embedded within HTML pages. Each table specifies test cases consisting of values of inputs and expected outputs. When creating test cases, text-based business rules are converted to tables that give examples of those rules. For instance, suppose the business rule for giving a discount is: If Customer Rating is Good and the Order Total is less than or equal $10.00, Then do not give a discount, Otherwise give a 1% discount. If Customer Rating is Excellent, Then give a discount of 1% for any order. If the Order Total is greater than $10.00, Then give a discount of 5%. Table 1 gives examples of this business rule. The FIT framework transforms this table into calls to a fixture. Fixtures convert the values in an HTML table to asThese requirements might be interpreted in multiple ways. signments to variables and the execution of methods. UserFor example, if the rating is excellent and the order is greater written fixtures inherit from predefined classes provided by than $10.00, is the discount 1 percent or 5 percent? Using the FIT framework. The variables and methods are defined by a table to illustrate the rule can decrease misinterpretation. the column headers. A header with the “()” suffix denotes a method. For each row in a table that references a column DiscountFixture fixture, FIT sets the variables, executes the method, and compares the returned result to the expected value in the orderTotal customerRating discountPercentage () method column. 10.00 Good 0.0 The name of the fixture DiscountFixture is the first 10.01 Good 1.0 line of the table. The fixture has public data members with the names orderTotal and customerRating and a .01 Excellent 1.0 public method called discountPercentage(), which re10.00 Excellent 1.0 turns a percentage (implemented as a double). Listing 1 10.01 Excellent 5.0 shows how this fixture would be implemented in Java. Assume that CustomerRating is an enumeration Table 1 with a method that converts a string such as “Good” to a constant as CustomerRating.GOOD;. class DiscountFixture extends fit.ColumnFixture { Creating this table in advance of coding helps clarify the public double orderTotal; requirements of the system. It also helps the developer create a public CustomerRating customerRating; readable set of tests. In my book Prefactoring [1], I give some public double discountPercentage() { guidelines for readability. One of them is to “Use the Client’s // Calls the method that computes the Language.” Within the FIT table, the client has specified the // discount and returns the discount requirements in his own language. The names of the table colreturn computeDiscount(orderTotal, umns should be used in the code that implements the business customerRating); rules. For example, the method should look something like: } }; Listing 1 double computeDiscount(double orderTotal, CustomerRating customerRating); 12 BETTER SOFTWARE JANUARY/FEBRUARY 2009 www.StickyMinds.com ISTOCKPHOTO http://www.StickyMinds.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.