The WET Cart

"I help out design firms with 'backend' coding for websites and web applications," writes Josiah, "usually this involves anything that's not HTML/CSS or copy/paste JS."

"Every now and then, I see something... a little more involved. Recently, I was working on a client's site that had an embedded 'store' where users can purchase promotional items. Aside from the tables used for layout everywhere, there was about 750 lines of javascript to calculate the cart. I'm pretty sure the original coder didn't quite get DRY principles... but he was well versed in WET (Write Everything Twice, or in this case, 76 times) principles.

  item1 = parseFloat(document.form1.Stitches.value);
  item1price = parseFloat(document.form1.StitchesPrice.value);
  item2 = parseFloat(document.form1.FirstAidKit.value);
  item2price = parseFloat(document.form1.FirstAidKitPrice.value);
  item3 = parseFloat(document.form1.Wristband.value);
  item3price = parseFloat(document.form1.WristbandPrice.value);

  // ...

  item76 = parseFloat(document.form1.HushCD10.value);
  item76price = parseFloat(document.form1.HushCD10Price.value);

  var subtotal = 0;

  if (item1 > 0) {
      var item1total = item1 * item1price;
      subtotal = subtotal + item1total;
  }
  if (item2 > 0) {
      var item2total = item2 * item2price;
      subtotal = subtotal + item2total;
  }
  if (item3 > 0) {
      var item3total = item3 * item3price;
      subtotal = subtotal + item3total;
  }

  // ...

  if (item76 > 0) {
    var item76total = item76 * item76price;
    subtotal = subtotal + item76total;
  }

Josiah continues, "thankfully, code to handle the 76-item shopping cart only appeared in three or four different places."