Friday, April 30, 2010

The CIO's Dilemma

Check out this SlideShare Presentation:

Friday, April 16, 2010

CodeSOD: Check Digit Check

CodeSOD: Check Digit Check: "

Seen any absurdly bad code lately? Send it on in!



Anne K has worked in the direct mail industry for almost twenty years, and has seen a bazillion ways to put names and addresses onto paper. One thing that's common across all mailing houses is that, in order to get discounts from the postal service, a postal barcode must be printed on every piece of mail. The barcode is made up of the ZIP code, the ZIP+4 code, and a couple more digits indicating (often) the house number. There's also a check digit on the bar code, which is calculated by adding up all the digits of the barcode, modding the result by 10, and then subtracting from 10. So, if all the digits of the barcode add up to 39, (10 - (9 mod 10)) leaves a result of 1.


It couldn't be simpler, right? It takes a teeny little loop and a mod. At least, that's what Anne thought. When she was doing developer training at a certain mailhouse, she just happened to be looking into the program that printed a simple name and address block on the piece. Curious as to why it was thousands of lines of code, she dove in and saw this.




counter=0;
if (zipcode >= 34200 AND zipcode <= 34299){
zzipcode = zipcode - 34200
counter = counter + 3 + 4 + 2;
}

if (zipcode >= 34600 AND zipcode <= 34799){
zzipcode = zipcode - 34600
if (zzipcode > 99){
counter = counter + 1
}
counter = counter + 3 + 4 + 6;
}

... snip ...

if (zzipcode < 10){
counter = counter + zzipcode;
}
if (zzipcode = 10) counter = counter + 1
if (zzipcode = 20) counter = counter + 2
if (zzipcode = 30) counter = counter + 3
if (zzipcode = 40) counter = counter + 4
... snip ...
if (zzipcode = 98) counter = counter + 17
if (zzipcode = 99) counter = counter + 18


Naturally, this is just a small subsection of the code (with proper indentation added). The original programmer apparently didn't realize there were math or, at the very least, string manipulation functions.


As for the most wondrous thing about this program? The coder took special care to find out exactly what ZIP code ranges aren't used by the postal service, so as to avoid unnecessary coding for those ranges.



"

Thursday, April 15, 2010

The Corruption of Dennis

The Corruption of Dennis: "

During water cooler conversation with his co-workers, whenever Dennis mentioned that he was responsible for supporting the Month End Closing system, reaction varied from a wide-eyed, agape look to a snide chuckle.


The Month End Closing system had a reputation throughout the department of being an ancient and legacy application that management had refused to upgrade over the years. Some of the comments put its true age at somewhere in the late 80’s early 90’s era which had earned it nicknames like “Ol’ Bertha” or “Methuselah” but most simply called it the “Legacy Dung Heap”.


Month-End Madness


Lady luck was not on Dennis's side that one fateful morning of his first month-end.


"OHMIGOSH! I JUST RAN MONTH END AND GOT AN ERROR CODE 40" panted the frantic caller, "IT...THE SYSTEM WON'T LET ME, OR ANYONE IN THE OFFICE, DO ANYTHING...CAN YOU GET US AN UNLOCK KEY???"


Dennis reassured the user that everything would be fine and to hang on literally for a minute while he investigated. While it was compeletely owned in-house, the Month End system harbored a funny quirk left over from when it was originally written by an outside software firm.


When the system hit certain conditions, usually catastrophic data problems, everything would be frozen until a support person could provide the "Hex Key of the Day." A secret value derived from the Error Code, Current Date, and software license key. Dennis opened the web form to generate the daily Hex Key but was surprised at the result:


CANNOT GENERATE HEX KEY FOR ERROR CODE - PLEASE INFORM
TIER 1 SUPPORT TO REFERENCE G-A24456

This bothered Dennis greatly as he was in the Tier 1 support group.


WTF is a G-A24456?


After re-assuring the customer that the solution was still only another minute away, Dennis knew that he would have to think fast. Desperate, he simultaneously searched the source code repository, documentation portal, and the department's shared network drive hoping that G-A24456 was unique enough to find a result and ended up hitting pay dirt.


In the source, G-A24456 was a function called from the section of the code responsible for handling month-end receipts and was shocked to find out what an Error 40 really stood for.


The error was a division by zero error.


Dennis quickly scoffed at the programmer who couldn't add a quick logic check to see if the number being used as the divisor was in fact zero. Then, Dennis looked at the source again.


There on the screen was a FORCED division by zero to stop program execution. No message, no exiting of a loop, nothing that would allow the user to self corrects and retry:



x=1/0; ## (DIVZEROHALT) Unprocessed receipts exist, instruct
## user to run the receipt register. Generate Hex Key
## using Error Code 99 (MISC) to unlock system.
## SNK 10/05/92

After picking his chin up off the floor, Dennis gave the hex key, instructed the user to process any outstanding receipts to get everything moving again, and immediately booked an appointment with his supervisor.


Leftover Secrets


In his meeting, Dennis relayed his findings – the forced division by zero, the developer note, and for good measure, a rant on the use of the Hex Key in general. Why not just show a pop-up window saying “You didn’t process the damned receipts – go do that and come back”? he argued.


"Close the door, Dennis. I have something to talk with you about."


Dennis's supervisor explained that whole idea of the hex keys were originally created long ago by the original developers with a dual purpose. The first was to force the user to stop everything if the system had some major mess-up, and second, to provide a steady stream of income for their support desk in the form of long-term support contracts.


“The way that our corporation works is that the number of hours we bill to our users for support directly impacts our funding for the next year,” explained Dennis’s supervisor,


"Why do you think that we are able to attend various conferences, have free coffee service, or enjoy our delightfully ergonomic work stations? All this is a result of our department's ability to bill for support."


He continued, "Frankly, if we remove all of the DIVZEROHALT statements and the concept of hex keys in general, we'll all probably have to be laid off!"


Dennis nodded to the supervisor's point while pondering his next move - go downstairs for some of that great free coffee or return to his desk or start work on polishing up his resume.



"