Wednesday, January 16, 2008

Codal Fitness

This is the first (of what I hope to be many) posts about software development. If you're not interested in that, feel free to go back and read the one about podcasts and booze.

My Current Project

Anne and I have a home gym consisting of:
  • a Bowflex machine
  • an elliptical trainer
  • various hand weights
  • an exercise ball
  • a DVD player and TV (very important, as exercising is boring as hell)
Unlike 98% of Bowflex owners, we actually use ours several times a week.

Until now, Anne kept track of her workout progress on scraps of paper, while I used the vast wasteland that is my brain (What was I up to on this exercise? 50 lbs?). So, I decided to write an application to do this for us.

Since we both have iPhones, I thought it would be useful to write a web application for it since we can keep it in our pocket or even on the couch next to the Bowflex where it's convenient to enter data. Besides, it would be a good way to learn web programming, especially Ajax.

After a few weeks of off-and-on coding, I cobbled together an application using HTML, Javascript, PHP and PostgreSQL, which allows a user to enter in the weight and reps of each set of each exercise in a workout. It saves all this in a database, which can be retrieved and displayed in a chart format (I don't have pretty graphs yet). It was now ready to test.

I forged down into the den/gym, iPhone in hand, to attempt to record my first workout. All was going well: I was able to easily enter data, add more exercises, etc. As always happens, though, I noticed several improvements that could be made. Switching over to the iPhone mail application, I started an email to myself listing these ideas.

Then I switched back to the web browser and learned my first big lesson about web development for the iPhone: when the iPhone senses it is getting low on RAM, the first thing it gets rid of is web pages, figuring it will just save the URL and reload the page. This does not work so well when you have an entire page full of data entered by the user. Poof! Gone.

I'm still working on a solution to that (which involves periodically sending the current data up to the server, which it stuffs into the database in XML format in case it needs to retrieve it later), but in the meantime I know not to switch to other apps (or even other memory-hungry web pages) while doing a workout.

A few tests later, I learned my second big lesson: Safari is much stricter than Firefox with regard to the DOM.

In this case, I was testing the ability to remove an exercise from the page (not sure why you would want to do this, but I put it in there anyway). As it turns out, I had used the same "id" attribute for the <div> I was removing as for an element within that <div>. Firefox happily removed the intended <div>, but Safari (both on the Mac and on the iPhone) puked and did all sorts of strange things.

Sure, I had a corrupted DOM, but it wasn't like it was ambiguous: I was removing a particular child <div> that had a particular <id>. It shouldn't have been looking at the <div>'s child nodes.

The difficult thing in that situation is that Safari's debugging tools are not quite as good as Firebug, so it's harder to trace these problems through when they only happen on Safari.

The third big lesson I learned is that trying to learn Javasript, PHP, and SQL at the same time - without any books - is like trying to learn French, Spanish and German at the same time by going to Epcot every day. There is a lot of documentation on the web for each of these, but it's pretty dispersed and incomplete. I just got the Javascript and PHP O'Reilly books, and already they've paid off.

Overall it's been really fun learning all these technologies, and architecting and developing my own little app, that is actually quite useful. Not to mention it relieves some of the boredom of working out.

2 comments:

mottsc said...

Safari, and Opera, are the most strict in terms of standards compliance, followed by Firefox and then way in last is IE. So standards wise I think it's good that you're doing it under Safari.

It also sounds like a cool project in general.

David Smith said...

Absolutely. I'm all for strict adherence, otherwise we end up with, well, with what we've got now.

Just did some web pages at work, and of course IE6 completely munges them, and I don't have a Windows box to test on. Any guesses as to what the target platform is?