Monday, March 24, 2014

Coding session

On Saturday, Fernando and I met with Matt S. and had an impromptu coding session for about 6 hours. Fernando and I were able to get most of the logic for the progress bar complete. This included checking that the user is checking in only once per 24 hours and is completing the requisite number of check-ins per week. This task turned out to be (in my opinion) a little more difficult than expected because it required some logic that would keep track of check-ins on a per week basis. After some brainstorming, Fernando and I decided that this would be best accomplished by creating another model in the database called Week. The purpose of Week was to keep track of how many check-ins the user has done during a week (which is defined based on when the user first started the challenge; so, if the user started the challenge in a Wednesday, then one week is 7 days from the start date, or, in this example, the following Wednesday). Adding this model made it relatively easy to quickly determine if the user is staying on track or whether they are missing check-ins.

Also Fernando and I had put some thoughts and suggestions in a Google doc, which we showed to Matt S. during the coding session, so we could agree on a resolution to the issues. One of these issues was what to do if the user misses a check-in for a given week. We decided that we would not take points away from the user since we want to keep them encouraged. However, we felt that there should be some sort of repercussion, which, in this case, would be to "redo" the week. Since we had implemented the Week model, keeping track of the check-ins and any "redone" weeks was straightforward. However, it was a little trickier to modify the progress bar Javascript code to work with this new penalty system. Since the users' controller was responsible for determining when the user had missed one or more check-ins and then performing the penalty calculations, we had to add some additional Ajax calls to the Javascript code in order to obtain the updated progress and number of check-ins from the controller. Last week, I had added a simple text display below the progress bar, whose function was to simply display the number of check-ins the user had so far completed (out of the total number required for the challenge). This was helpful in debugging the issue that I had mentioned in my previous blog post regarding the display and storage of the progress value. It has also been helpful as we have been debugging the code for validating the check-ins.

So far, we have most of the code working for this check-in validation. We have had to use a compressed timescale for testing this code since obviously it is impractical to continually test these for the "normal" timescale of weeks or months. We have been using 10 seconds to represent a day and 70 seconds to represent a week. However, this compressed timescale also presents its own issues as you have to be very aware of exactly how much time has passed and know when you need to "check-in" in order to test certain features. Using 1 minute to represent a day and 7 minutes to represent a week is a little easier to use for testing, but can get very tedious when you are continually making small changes and then retesting. I have just started using 20 seconds for a day and 140 seconds for a week and this seems to be working fairly well.

While working on getting the new functionality for the progress bar implemented, I learned two things that I should remember for future coding and debugging. First, to print out the value of a variable in Rails, you type:

raise @<variable_name>.inspect

Otherwise you will get an error since it is trying to print out the object, not the object's value. Second, when updating the attributes of a model (which is represented in the current context by an instance variable), you must use either "update_attribute" or "update_attributes" (for updating more than one attribute). You cannot just assign a new value to the attribute by calling "@instance_var.attribute = "some value". I lost about 20 minutes trying to figure out why my code wasn't updating the database before Matt pointed this out. So to do an update of a model's attribute:

@instance_var.update_attribute(:field_name, @instance_var.field_name + <some modifications or updates here such as +, -, etc.>)

There are some more test cases to run through and a few code adjustments to be made, but, overall, the progress bar functionality is mostly complete.

No comments:

Post a Comment