Saturday, August 23, 2008

Two Coding Styles Face Off

I notice that anytime I'm programming a new (CakePHP) site I'm often fighting internally about my coding practices.

In the blue corner is quick and serviceable. Quick and serviceable has come in winning many fights. His speed makes him difficult to corner, and he can be very adept at completing many things quickly. Unfortunately, this also leads to more problems as situations arise that were not considered originally making him more vulnerable.

In the red corner is longer and more thorough. He often takes a while to get going initially, leading to losses in shorter bouts. However, when entering the later rounds, he becomes a more serious opponent, and his record becomes even more impressive in closing out projects, I mean fighters, effectively.

We've been working on a new shopping cart site lately, and I have an add to cart script that checks the current product being added (along with any product attributes that may differentiate it) against the current shopping cart to see whether to add it as a new product or to an existing one.

My original inclination was to check by pulling all attribute groups for a product. Since they are associated with only that product I could easily assume that those attributes were also in the product in the cart. A simple loop through and comparison with the attributes in the cart should do the trick.

However, what if someone administering the site changed an attribute after someone added something to their cart, but before they want back and decided to buy some more. While that may not happen very often, it's just that type of thing that causes headaches and late night calls from clients wondering why a customer is angry with an issue with the website.

So instead of making that assumption, I loop through all the product attribute groups. Then, I check specifically if each product attribute group has actually been submitted by the customer (which originally I was going to just assume actually took place), and then check to make sure that the actual attribute values correspond before adding to a previous cart item or making a new cart entry.

What practices give you fits? Let me know in the comments.