What Code To Copy?

Generally, when you search for a question on Stack Overflow, you can usually find a answer to your problem. This is not always a good thing.

  • Some of the answers on Stack Overflow may be plain wrong. Anyone can answer a question on Stack Overflow, so just because it is online does not mean it is true. While obviously wrong answers can be downvoted, not so obvious answers may be highly voted. In addition, you usually cannot be certain at all that the code in question actually compiles or work until you try it out yourself.

  • Some of the answers may devoid of explanation and be composed of nothing more than code. While this may seem like a good idea for the eager copy-and-paster, it really isn't. You need to know what the code is actually doing. After all, you will have to maintain that code in the future.

  • Some of the answers are legacy answers. They worked in the past, but are now no longer best practice. Stack Overflow suffers from the "fastest gun in the west" syndrome where whoever answers the question first usually gets the most votes, and thus is seen first. The more "modern" answers usually don't get as many votes and tend to languish on the bottom of the page.

This is why it is an absolutely horrible idea to blindly copy and paste the first answer to a Stack Overflow question. I've done it. It never works.

Instead, scroll down to read all answers to a question, as well as all the comments to the answers as well. They will provide you some more information and clarification so you can make a more informed choice on what code to copy and paste. Also, consider looking at the sidebar of Stack Overflow and explore the linked questions and related questions as well.

Viewing the date of a question and its answers is also helpful as well. Stack Overflow is a website with a long history, and knowing the publication date of content is really helpful in knowing whether the content is current or if it may be rendered obsolete by more updated content. This does not mean that you should reject an answer just because it is a year or two old. But it does give you an additional data point to help you make a professional judgment call.

If you are unhappy with a question and its answers, don't worry. Search further. Stack Overflow tends to frown on people asking the same question over and over again. Yet it still happens regularly. Usually those questions can end up being closed as being a 'duplicate', but they are not deleted and can still be accessed normally. Search for these "duplicated" questions, and you may find answers that are much better than the so-called "canonical" version.

Finally, keep in mind that you don't have to copy and paste only one code snippet. You can certainly mix-and-match different approaches. This can be slightly problematic if you wish to provide attribution for all the different code snippets you copied and pasted from, but you can usually handle this issue if it comes up.

results matching ""

    No results matching ""