Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Copy and Paste, A Programmer's Nightmare!

5.00/5 (16 votes)
5 May 2016CPOL6 min read 18.6K  
Copy and paste - a programmer's nightmare

One of the biggest dangers in using an online programming forum is the available source code !

What? That may sound crazy, but let me explain.

Source code on programming forums is truly a benefit to all programmers and I am not suggesting that there is anything wrong with learning from it, but there is a tempting danger to such source code and it comes from one simple error, “copy and paste”.

Yes, copy and paste may be one of the biggest dangers there is in using freely available source code and here is why.

Real Life Experience

As a developer of programming tools, I have spent more than a decade and a half in learning how to accomplish many tasks using the Windows WIN32 API. When writing part of a graphic engine, I wanted to start using DIB Sections (device independent bitmaps) since they have been supported since Windows 95, but provide a powerful tool for writing low level graphic manipulation engines. Using DIBs seems easy enough, you would think. Just use the CreateDIBSection API function. I didn’t use any file mapping at all, just let Windows create the DIB in memory and get back a DIB handle.

But of course, I started searching for some code examples on the web, but not for the reason you may think. One rule I have when coding my programming tools, especially my GUI framework is “never use any code I find on the web or even in a book and I mean never”. Instead, I would look for examples of code simply to see how it approaches accessing the different APIs in the WIN32. I would write down on paper a list of each API call used and then the fun would start. I would research each and every WIN32 API used in the MSDN SDK documentation and try to fully grasp every aspect of each API function, making notes of anything critical to my coding task at hand. Then starting from scratch, without looking at any code examples I learned from, I would write my routines completely, testing it as I go for proper functioning (i.e., checking return values from WIN32 calls). I would fully test the code to make sure it works properly, before using it in any real world code I used in my tools.

The interesting thing about this method is because I don’t copy the code, but write it from scratch while researching the WIN32 documentations as I go along, I often find confusing API calls are being used in some of the code I looked at online. In the case of CreateDIBSection, one of my first examples of code was found on a programming forum I trust a lot. There were not a lot of examples of using CreateDIBSection, but the few there were posted by some very, very experienced programmers trusted by that forum community. The code was not in the typical WIN32 style C code many are familiar with, but was in the programming language I used. The strange thing though was that the code treated the return value from CreateDIBSection, not as a normal bitmap handle (which is deleted using DeleteObject), but as a Global memory handle and the code used Global memory API’s on the DIB handle. This didn’t make sense to me from what I read in the API documentation and I came to the conclusion the code was in error. I wrote my code properly using what I read in the API docs. It is slow going checking each API function in your code, but I do it regularly and follow the directions found in the API docs.

What struck me though, was how did such a reputable programmer on the programming forums make such an error?

An Example of Copy and Paste Even When Porting Code

I decided to search the web more extensively for code which created and used DIBSections, beyond the programming language I was using (a form of BASIC) and started to compare them. Most examples were in C and amazingly, the C examples often made the same error. When I looked at the BASIC version, it was almost identical to the C version (except syntax of course). It appears the programmer, like most of us, also searched the web for examples to learn from and simply ported a routine from C to BASIC, without checking to see if the original C version had any errors. The problem I assume is that if a programmer, like myself, hadn’t used an API before, that they might be unsure of themselves and simply be tempted to just copy the code someone else wrote, thinking they likely know better than I do how this works. Fortunately for me, I don’t do this and found the error existed in a good bit of C examples on the web as well as the BASIC I started from. But how did I find the error since I was using an API I hadn’t worked with before ?

The Key is to Fully Understand APIs Before You Use Them

I took the time to read the API docs carefully. I wrote my own code and tested it thoroughly, double checking return values from API calls. One beautiful thing about the WIN32 is that many functions return a pass/fail value so you can know the call worked. Sadly, some programmers fail to take the time to test these return values. One may not need to leave the test code (test return value) in the final code, but one should use it when first testing the code to make sure it functions properly.

There is one long time problem with the WIN32 API. Each version of Windows has been tolerant of many errors made when calling API functions. The bad code in the DIBSection code mentioned was tolerated by Windows, but this does not mean it wasn’t bad. Some errors when calling the WIN32 API don’t generate any flag to the user (or even developer) at all other than possibly a pass/fail return value to the call. No GPF, no crashing program and it appears to work. That's the danger. Bad code may be tolerated with no immediate backlash, but it is there none the less. Some errors like this though can bring down windows when they accumulate (multiple iterations of the same error). Newer versions of Windows may not tolerate the same error, which explains the “it worked fine in Windows XP, but crashed when I switched to Windows 7” type of experience. Bugs are bugs, no matter whether they are tolerated by the operating system or not.

Copying Code Never Benefits Anyone!

No matter how much you trust the source of publicly available code, never simply copy and paste. It is the biggest error programmers can make. Why? Even if the code works and has no bugs, a programmer does not learn how it all actually works, which is critical for producing quality applications. If you don’t know how or why your code works (because you copied and pasted it), you can’t properly debug the code in the future nor can you properly fine tune it (i.e., improve performance).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)