Sunday, March 25, 2012

RMH Homebase Exercises: Editing the "Person" Class

The following exercises are from the SD book They involve editing and refactoring the "Person" class of the RMH Homebase open source project.

Ex. 6.1
This exercise involved adding in some setters and getters for some new instance variables that were added to provide new functionality.  The new variables were $status, $employer, $contact_person, and $contact_phone.
All of the setters look like this:

function set_employer ($value) {
   $this->employer = $value;
}

And all of the getters have this simple structure:

function get_employer () {
   return $this->employer;

Ex. 6.2
Here we are instructed to add our new parameters to the constructor for the "Person" class.  I went in and modified the constructor to include the following variables : $status, $employer, $contact, and $contact_phone.  I then initialized them like this: $this->status = $status; , etc...

Ex. 6.3
This problem presents us with a question of how to handle invalid input.  In the set_status function, there is currently no check in place to determine whether the $value is either 'active' or 'inactive'.  A good way to check this would be with an 'if' statement that would make sure that $value was either 'active' or 'inactive' before setting it to anything.  And if $value was an invalid string, then the best thing to do would be go ahead and set $status to inactive in the 'else' clause.  This way no one is accidentally set as ready to work a shift.

Ex. 6.4
Now that the code has been modified, it is time to refactor and check things out.  This exercise asks to go through all of the setters in the "Person" class that we have been working with, and check and see if they are called from anywhere in the code base.  It then says to remove any that are not used.  Not wanting to physically read through all the php files to do this, I opened up the files on the workbench in Eclipse.  I then went through each setter, highlighting them one by one, and checking the call hierarchy (ctrl-alt-h) to see if they were called from anywhere.  Surprisingly, I could not find a single one being used!  I didn't delete them yet, as I want to make sure I'm doing this right before proceeding. 

No comments:

Post a Comment