More like this

Software

This is why copy'n'paste should be banned from developers' IDEs

More mind-boggling code unfortunately encountered in the wild

Line Break Welcome to the latest instalment of Line Break, the column in which we share ghastly code readers have sadly encountered in the wild.

Pen is mightier than the source

We'll crack on with this appetizing anecdote sent in by Ian:

A long time ago I was working on the driver for a touchscreen controller chip. This driver was supplied to our customers as sample source code which they then incorporated into their product.

One part of the touchscreen controller code detected whether the stylus (pen) is touching the screen (down) or not (up) to determine whether to send points to the operating system. I was updating the end-of-scribble detection code, and decided to change a variable name before it went to customers. I reordered the words in it to isPenUp. I'll leave it to you work out the original variable name.

Well done, Ian, for avoiding that cockup. Lesson: It's said that there are only two hard things in computer science: cache invalidation and naming things. 'nuff said.

Where and tear

Moving right along, Jamie sent in this complete and utter car crash. It looks as though it's trying to construct an SQL statement in a manner similar to building a house by firing bricks smothered in cement out of a cannon. Hold onto your hats.

   if ( Length( Where1 + Where2 + Where3 + Where4 + Where5 + Where6
      + Where7 + Where8 + Where9) > 0 ) then
   begin

        if Length( Where1 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
                  WhereClause := WhereClause + Where1;
          end;

        if Length( Where2 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
              WhereClause := WhereClause + Where2;
          end;

        if Length( Where3 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
              WhereClause := WhereClause + Where3;
          end;

        if Length( Where4 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
              WhereClause := WhereClause + Where4;
          end;

        if Length( Where5 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
              WhereClause := WhereClause + Where5;
          end;

        if Length( Where6 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
              WhereClause := WhereClause + Where6;
          end;

        if Length( Where7 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
              WhereClause := WhereClause + Where7;
          end;

        if Length( Where8 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
              WhereClause := WhereClause + Where8;
          end;

        if Length( Where9 ) > 0 then
                begin
                  if Length( WhereClause ) > 0 then
                  WhereClause := WhereClause + ' AND ' ;
              WhereClause := WhereClause + Where9;
          end;

        WhereClause := 'WHERE ' + WhereClause;

Enough. We can't take it any more. "A really simple example of why sometimes we wish copy and paste was banned from IDEs," notes Jamie. Lesson: Duplicated code is a code smell – and repetition like the above should stink like a skunk's dirty laundry basket. With a dead body inside.

Can't bear to look

After the previous abomination, Peter cheered us up with this anecdote:

Many, many years ago I administered a quite large Symbolics LISP machine network. One day, whilst browsing through the OS source code, I came across this comment:

"You may want to close your eyes, the next bit is a kludge."

Further on down.

"OK, you can open your eyes now."

Lesson: Comments are nice, but they are not get-out-of-jail cards. Write crap, get smacked.

Point of no return

OK, time for one more story – this time from another reader called Peter:

I remember back in the late 1970s looking through some Honeywell Level 6 communications software and seeing an interesting comment in some assembler code.

After making an operating system call, it was customary to check the register R1 for errors and act according to the value returned from the call. At this particular point in the assembler code, no such check was made and the comment was:

"Error at this point too horrible to contemplate."

Lesson: You can be certain of two things in life, no wait, three: death, taxes, and errors. Always check for them.

Keep your awful code snippets and tales of woe coming in, please – we'll feature a selection every Wednesday. We'll finish off on some code your humble hack stumbled across this week. It's a bit long, so it's over on the next page.

Sponsored: Customer Identity and Access Management

Next page: Scream GNU murder