Explore BrainMass

Question about Reusable Code

You realize that the code you had created can be reused for many other purposes for future programs. You decide to create a library for your reusable code. Using the pseudocode for the program, you identify the aspects of the program that can be reusable and write/rewrite it so it can be reused. You rewrite your pseudocode to implement this reusable code.

This is my code, how would I rewrite it?

Program Name { //begin of program

Procedure TrackPayment {

if amount_received < billing_amount then
amount_owing = billing_amount - amount_received

if date_payment_received > thirty_days then
late_fee_amount = late_charge_rate * billing_amount

account_balance = account_balance + amount_owing + late_fee_amount


Procedure CalculateInvoice {

if client_type = regular then
billing_amount = regular_price
billing_amount = regular_price - (service_price * discount_rate)

billing_amount = billing_amount + account_balance

Procedure PrintInvoice {

print client_name
print client_mailing_address
print invoice_number
print invoice_date
print billing_date
print billing_amount
print billing_period

begin main ( ) {
do TrackPayment
do CalculateInvoice
do PrintInvoice

} //end of program

Solution Preview

The first thing to do before writing a class library is to determine the goal of the library.

After defining the goal, the following "10 tips on writing reusable code" should become useful (from: http://hoskinator.blogspot.com/2006/06/10-tips-on-writing-reusable-code.html)

"10 tips on writing reusable code

1. Keep the code DRY. Dry means Don't repeat yourself. Always try to eradicate duplication and if you find any then move remove the duplication to a relevant place. Sometimes this has leads to a need to creating Static Helper classes or move it to the class it makes most sense to have it.

2. Make a class/method do just one thing. This is along the lines of the advice of giving the
class only one reason to change. This often means creating methods that other methods use but
this helps to make the methods/classes simple and less coupled.

3. Write unit tests for your classes AND make it easy to test classes. Writing code that is easy
to test is decoupled. If you write code and are thinking about writing a unit test for it then
you tend to split up the code into smaller testable chunks.

4. Remove the ...

Solution Summary

The solution provides a source with 10 tips on writing reusable code in a library with example code included.