Better Software - January 2009 - (Page 14) Code Craft class Dollar { public static Dollar add (Dollar one, Dollar two); // or public void add (Dollar another); // or public Dollar add (Dollar another); }; Listing 5 implementation details and keeps the formatting and string interpretation in one place. Following the “Don’t Repeat Yourself” rule makes for consistency. Who should better know how to format a Dollar than the Dollar class itself? Dollar myDollar; Dollar discountAmount = myDollar.multiplyBy (discountPercentage()); Listing 6 For business-related programs, you’ll probably have a small number of data types, such as Dollar, Percentage, and Count, that you’ll use over and over again. The work involved in creating a set of data types and the associated arithmetic methods will pay off. For scientific programs, you may find the number of data types keeps growing. Every new data type may require more arithmetic methods to interact with the existing data types, such as dividing a length type by a time type to get a speed type. But chances are there are more limited interactions between these data types, and the necessary arithmetic methods will not rapidly approach unmanageable levels. Creating acceptance tests not only helps define the requirements but also helps make the code more readable to the client. Using the client’s language makes the translation between client expectations and developer implementation much simpler. {end} Simply multiplying a double by a double that yields a double does not handle all real-life issues. When you create a method called multiplyBy (Percentage other), then you may have the impetus to discuss with the client what issues are involved. In implementing the method, you may realize that rounding is involved and you are not sure how to deal with it, so ask the client. Suppose the client responds that rounding is a case of “Well, it all depends on the context.” Then, you can either create two methods (multiplyByRoundUp and multiplyByRoundDown) or add a parameter (called RoundingType having values ROUND_UP or ROUND_DOWN) to multiplyBy(). In addition, having a multiplyBy () method permits a single place where you can accumulate all the pennies from rounding, so you can save them for a rainy day. And what about Percentage? Having a data type of that kind should make it clearer that percentages should be expressed in terms of 1.0 percent rather than as a fraction of one (e.g., .01). Before we conclude, let’s go abstract all the way. Let’s create a data type called Count. We’ll need to add some arithmetic methods to Count, as shown in listing 7. We may find a need to add a method to Dollar called Dollar multiplyBy(Count other)—and maybe even a few more. But every time we do, we’ll be encapsulating another implementation, and separating the implementation from the use will make your code more readable. class Count { void increment(); void decrement(); Count add (Count another); } Listing 7 RefeRenceS: [1] Pugh, Ken. Prefactoring. O’Reilly Media, Inc, 2005. [2] Mugridge, Rick and Ward Cunningham. Fit for Developing Software: Framework for Integrated Tests. Prentice Hall, 2005. What is your approach to dealing with the Dollar and Percentage concepts? Follow the link on the StickyMinds.com homepage to join the conversation. Sticky Notes For more on the following topic go to www.StickyMinds.com/bettersoftware. n Further reading Have tHe LaSt WoRd! If you have a point to make or a side to take on issues and trends that affect the industry, we want to hear from you. We are looking for insightful, thought-provoking commentary for possible use as a Last Word column. Please send your submission to editors@bettersoftware.com. You will be notified if you are being considered for publication. 14 BETTER SOFTWARE JANUARY/FEBRUARY 2009 www.StickyMinds.com http://www.stickyminds.com/codecraft11-1 http://www.StickyMinds.com/bettersoftware 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.