Tuesday, March 20, 2012

OBFUSCATED COBOL

Today, i ran into a snippet of COBOL with a bug. It was of the form

IF LONGWINDEDNAMEA = 3 OR 4
AND LONGWINDEDNAMEB(1:1) = C
MOVE...

but it should have been

IF (LONGWINDEDNAMEA = 3 OR 4)
AND LONGWINDEDNAMEB(1:1) = C
MOVE...

because AND has higher precedence than OR. So what the first example does is, uhm, what the heck? Oh. COBOL has implied subjects and implied operators. If omitted, it uses the previous subject and/or operator. The first example expands to this:

IF LONGWINDEDNAMEA = 3 OR LONGWINDEDNAMEA = 4
AND LONGWINDEDNAMEB(1:1) = C
MOVE...

so the compiler directs the computer to compute

IF LONGWINDEDNAMEA = 3 OR (LONGWINDEDNAMEA = 4
AND LONGWINDEDNAMEB(1:1) = C)
MOVE...

Which is not at all what the author wanted.

When I was a good deal younger, the incredible verbosity that is COBOL was explained away like this. The language is designed so that business managers could read it. It's hard to imagine what business manager audience COBOL was aimed at. None that i've ever met could have told the difference between COBOL that does what it looks like and obfuscated COBOL.

IMO, a large fraction of computer bugs are actively encouraged by languages that think programmers don't know how to type. Computer languages also have absurdly complex precedence tables. Who could get that right every time? Only the guys who look it up every time, or use parenthesis every time. Why not just go with left to right everywhere? 1 + 2 * 3 = 9, instead of 7 (because you need to * before +).

Now, i suppose programmers don't, by and large, know how to type. I could type 22 words per minute by the end of high school. I could type 22 words per minute by the end of college (plenty of practice, but no additional instruction). Over the next 20 years, at a keyboard for 2000+ hours a year (40,000 hours), my typing speed doubled to about 40 words per minute. But about ten years ago, i got addicted to a typing game for a couple months, and my typing speed shot up to over 70 words per minute. What a wasted 20 years. With a little instruction, i could have save huge amounts of time typing.

No comments: