The simplest thing that could work - Hacking Journal [2006/02/01]

written by edavis on February 1st, 2006 @ 12:29 PM

If you are trying to add a new feature to software, the best place to start at is to try the simplest thing that probably will not work. I had to add a feature to my Rails project that required keeping track of the last 10 places a user visited. First I thought another database table would not be the answer so I tried to implement a ‘stack’ like object that is stored in a new column in the user table. This stack would push the new visit on the end and pop the oldest one off. I figured this would be easy and I could use REGEX to help grab the right area of the stack.

Well 2 hours later I had the column, and was pushing data in but I could not get it to pop out after 10 or even to REGEX it simply. After trying and trying I then reverted back to before I started, (source code management to the rescue) and thought about trying a dedicated table. Well going that route in 1 hour I had a fully testes, working feature. Guess I should have gone with my gut instead of my brain.

The min reason I tried the stack was I thought I would be adding another 2-3 queries for the table version. This was a classic case of trying to make the system go fast, before it went at all (Premature Optimization ). Turns out the stack version needed at least 2 queries plus all the regex overhead and it was only storing the id of the page. With the table route I am able to timestamp each entry and can even add more information later on as I need.

Eric Davis

Post a comment