Article 39R9B CodeSOD: Pounding Away

CodeSOD: Pounding Away

by
Remy Porter
from The Daily WTF on (#39R9B)

"Hey, Herbie, we need you to add code to our e-commerce package to send an email with order details in it," was the requirement.

"You mean like a notification? Order confirmation?"

"Yes!"

So Herbie trotted off to write the code, only to learn that it was all wrong. They didn't want a human-readable confirmation. The emails were going to a VB application, and they needed a machine-readable format. So Herbie revamped the email to have XML, and provided an XML schema.

This was also wrong. Herbie's boss wrangled Herbie and the VB developer together on a conference call, and they tried to hammer out some sort of contract for how the data would move from system to system.

They didn't want the data in any standard format. They had their own format. They didn't have a clear idea about the email was supposed to contain, either, which meant Herbie got to play the game of trying his best to constantly revamp the code as they changed the requirements on the fly.

In the end, he produced this monster:

 private function getAdminMailString(){ $adminMailString = ''; $mediaBeans = $this->orders->getConfiguredImageBeans(); $mediaBeansSize = count($mediaBeans); $adminMailString .= '###order-start###'."\n"; $adminMailString .= '###order-size-start###' . $mediaBeansSize . "###order-size-end###\n"; $adminMailString .= '###date-start###' . date('d.m.Y',strtotime($this->context->getStartDate())) . "###date-end###\n"; $adminMailString .= '###business-context-start###' . $this->context->getBusinessContextName() . "###business-context-end###\n"; if($this->customer->getIsMassOrderUser()){ $customers = $this->customer->getSelectedMassOrderCustomers(); $customersSize = count($this->customer->getSelectedMassOrderCustomers()); $adminMailString .= '###is-mass-order-start###1###is-mass-order-end###'."\n"; $adminMailString .= '###mass-order-size-start###'.$customersSize.'###mass-order-size-end###'."\n"; $adminMailString .= '###mass-start###'."\n"; for($i = 0; $i < $customersSize; $i++){ $adminMailString .= '###mass-customer-' . $i . '-start###'."\n"; $adminMailString .= '###customer-start###' . $customers[$i]->getCompanyName() . '###customer-end###'."\n"; $adminMailString .= '###customer-number-start###' . $customers[$i]->getCustomerNumber() . '###customer-number-end###'."\n"; $adminMailString .= '###contact-person-start###' . $customers[$i]->getContactPerson() . '###contact-person-end###'."\n"; $adminMailString .= '###mass-customer-' . $i . '-end###'."\n"; } $adminMailString .= '###mass-end###'."\n"; } else { $adminMailString .= '###is-mass-order-start###0###is-mass-order-end###'."\n"; } for($i = 0; $i < $mediaBeansSize; $i++){ $adminMailString .= '###medium-' . $i . "-start###\n"; if($mediaBeans[$i] instanceof ConfiguredImageBean){ $adminMailString .= '###type-start###picture###type-end###' . "\n"; $adminMailString .= '###name-start###' . $mediaBeans[$i]->getTitle() . "###name-end###\n"; $adminMailString .= '###url-start###' . $mediaBeans[$i]->getConfiguredImageWebPath() . "###url-end###\n"; } else if($mediaBeans[$i] instanceof MovieBean){ $adminMailString .= '###type-start###movie###type-end###' . "\n"; $adminMailString .= '###name-start###' . $mediaBeans[$i]->getTitle() . "###name-end###\n"; $adminMailString .= '###url-start###' . $mediaBeans[$i]->getMoviePath() . "###url-end###\n"; } else { throw new Exception('Bean is wether of type ConfiguredImageBean nor MovieBean!'); } $adminMailString .= '###medium-' . $i . "-end###\n"; } $adminMailString .= '###order-end###'."\n"; return $adminMailString; }

Yes, that's XML, if instead of tags you used ###some-field-start###value###some-field-end#### in place of traditional tags. Note how in many cases, the tag name itself is dynamic: $adminMailString .= '###medium-' . $i . "-start###\n";

It was bad enough to generate it, but Herbie was glad he wasn't responsible for parsing it.

puppetlabs50.png[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today! TheDailyWtf?d=yIl2AUoC8zA6SOY3ovE9oQ
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments