Exercises in Unit Testing

Today’s exercise is based of of chapter five in our software development book. I have to look at three exercises at the end of the chapter. Based on what I have seen and read, these exercises will be applications in testing and documentation within a FOSS project. Let’s get started.

5.1 Examine the RMH Homebase release 1.5 code base and accompying documentation. Identify at least one instance of the following:

a. Long Method
b. Too Few Comments
c. Data Clumps

This method comes from personEdit.php. It is over 140 lines long. The purpose of the function is to refine all the forms entered. As you can see in the code below. The code is effecting over 14 variables in this part of the code alone which could cause some hard to find side effects. There are few comments relating what each of these blocks effect, and the method tries to accomplish too much in this one function. There is so much going on that it could be its own php file. Also it appears every piece of data extraction is occurring in this function on the variable id. The constant use of id in so many different extractions leads to data clumping.

function process_form($id) {
//step one: sanitize data by replacing HTML entities and escaping the ‘ character
$first_name = trim(str_replace(‘\\\”,”,htmlentities(str_replace(‘&’,’and’,$_POST[‘first_name’]))));
$last_name = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘last_name’])));
$address = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘address’])));
$city = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘city’])));
$state = trim(htmlentities($_POST[‘state’]));
$zip = trim(htmlentities($_POST[‘zip’]));
$phone1 = trim(str_replace(‘ ‘,”,htmlentities($_POST[‘phone1’])));
$clean_phone1 = ereg_replace(“[^0-9]”, “”, $phone1);
$phone2 = trim(str_replace(‘ ‘,”,htmlentities($_POST[‘phone2’])));
$clean_phone2 = ereg_replace(“[^0-9]”, “”, $phone2);

$private_notes = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘private_notes’])));
$public_notes = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘public_notes’])));
$my_notes = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘my_notes’])));

$background_check = ”;
$shadow = ”;
$interview = ”;
if($_POST[‘background_check’]==’yes’) $background_check = ‘yes’;
if($_POST[‘interview’]==’yes’) $interview = ‘yes’;
if($_POST[‘shadow’]==’yes’) $shadow = ‘yes’;

$convictions = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘convictions’])));
$wherelived = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘wherelived’])));
$experience = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘experience’])));
$motivation = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘motivation’])));
$specialties = trim(str_replace(‘\\\”,’\”,htmlentities($_POST[‘specialties’])));
d. Speculative Generality

As for speculative generality, thew helpfooter.inc file clutters the code, and could have easily been made into a txt file for the user. Or better yet it could have been added to another method that deals with all footers, instead of being a repetitive include file.

5.2 For each of these “bad smells” refactor the code to reduce the size of the code base.

a. Long Method
b. Too Few Comments
c. Data Clumps

For these three I had to do a good chunk of work. I separated the process_form into different functions. This made the code more manageable and alleviated the Long Method and Data Clumps issues. This yielded four methods: process_address, process_notes, process_meeting, and process_background. Then I had to add comments to each explaining what they did and why they were there. This de-cluttered the code and turned a long method into a reasonable one.

d. Speculative Generality- I combined the helpfooter.inc with the footer.inc to remove one include file, and changed the reference to helpfooter.inc within the help.php file.

5.3 Perform unit tests

I ran unit tests on personEdit.php and help.inc that yielded all passes. Overall I am satisfied with the results of my refactoring though I will need to add in more test cases later to make sure I have checked absolutely everything necessary.

This weekend I and my team will be working on setting up a project schedule for the rest of the semester. See you all then!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s