Do you copy and paste code from online websites such as C# Corner or Microsoft Docs? Do you find code snippets on online forums such as StackOverflow and copy and paste into your project? Do you download open-source projects and libraries and use them in your projects? Do you use Github or Nuget package for your projects?
This all is copying and pasting code. This means you have copied someone else’s work in your project. But nothing to worry about. You’re not alone. According to a poll by C# Corner, 75% of coders have copied and pasted code in their projects.
Many open source believers love to share their code. I started C# Corner in the year 2000 just to share my code with the world. I shared my first open-source project in 1999 written on the Mindcracker.com website in C++.
Is copying and pasting someone else’s code legal? Or ethical? Or even allowed?
The brief answer is, YES. BUT there is a catch. It’s not as simple as it seems. If you did not pay attention to the licensing, you may get in trouble. As a matter of fact, many companies do not allow their coders to copy and paste public code. If you work for a large company, you may want to check with your company’s best practices. This is exactly what I will discuss in this article. I will clearly explain the best practices of copying and pasting someone else’s code in your projects and also not get in trouble.
Note: Most of the code available on C# Corner is free to copy and use anyway you see it fit unless otherwise author has some notes in their code. Let me be clear. If you just copy some code snippet from a website like C# Corner, you're free to learn, make minor tweaks to the code, and use in your projects. If you're copying large files and projects, make sure you read licensing topic below in this article.
First may not be the best.
When you are looking for a code snippet and Google it, you get so many search results. The first result may not be the best result. You must compare a few results and see which one is the best option for you. Before you copy and paste the code, look for few important things such as when was the code published, what version of the product used, who the creators is, are the standards used and met in the code, and read if there are any comments and remarks.
I know I published an article on C# Corner with code snippet in it in 2001 that was written for .NET 1.1 and that article still comes in top Google searches.
Read copyrights and ownership.
Just because someone has written a blog or shared a code snippet or even uploaded a project on Github or other open source platforms doesn’t mean you can copy code and do whatever you want. While most of the open source licenses allow you to copy and paste code in your own projects the way you like, but some of them come with some restrictions.
A whopping 63% of developers don’t read or understand that licensing is a major issue when it comes to copy and paste publicly available code in their projects.
The following is a list of some of the most popular open source licenses.
- Apache License 2.0
- BSD 3-Clause/BSD 2-Clause
- GNU General Public License (GPL)
- GNU Library or "Lesser" General Public License (LGPL)
- MIT license
- Mozilla Public License 2.0
- Common Development and Distribution License
- Eclipse Public License version 2.0
While they all allow you to use source code for private and commercial use, most of them require you to include a copy of the license and copyright notice with the material used. This is very important.
So, before you copy and paste a code snippet, large code files, or open source projects, make sure to read the licensing and copyright details. If a website does not have any licensing and copyright, I highly suggest not to use that code in your projects.
Check versions and date of release.
Software products change each year. If you work with a programming language such as C#, it is changing every year. There is a new product release with new and improved features
So, before you copy and paste code, don’t forget to look for the version of the product and its published date
Check author’s credibility.
This is probably the most important factor when copying code from a public website. There are millions of software developers who publish and share their code online and many of these developers are students and inexperienced developers. It’s not that they cannot write good quality code but it's possible that they may not understand the code quality, speed, and performance. Also, many websites write generic and basic code that is not written by expert developers. Some websites that are great in SEO also have very old and obsolete content that probably should not be referenced today.
The code published by a Program Manager at Microsoft or from a team member of C# language or React Dev team is more likely to have high quality and optimized than a young student who has just started learning the language.
Scan for vulnerabilities and performance.
There are several tools out there that can look a project or code for vulnerabilities. While, in most cases, you may not need it but most companies have tools to scan code from vulnerabilities and security holes. If you’re downloading code from a public repo, don’t forget to read the notes, unfinished features, bugs, issues, and users’ comments. Many open source projects have known issues.
Understand exactly how it works.
After agreeing with all the above, you’ve decided to use a piece of code in your project. Some developers directly copy and paste code without understanding it as long as it works.
You as a good developer must understand what exactly the code does and how it works.
Rewrite it in your own language.
Now, you know how a piece of code works. The next thing I highly recommend is, rewrite it in your own language and syntaxes. If there can be any improvements made, make them. If code can be optimized, optimize it. But when you’re done with the final code, nobody should be able to say, this is the exact code copied from somewhere else.
The simplest thing you can do is, change method names, types, properties, and variable names. Comment code, and remove unwanted code.
Optimize it and update it.
As I said earlier, code can always be improved. If it is not optimized, make sure to optimize it while you’re rewriting it. As I said in the previous point, you must rewrite any copied code in your own language, comment it properly so anyone can understand it, and also write unit tests if possible.
Many open source projects are constantly in development and often gets updates. If you are using any of these types of projects, have a plan to update code when new version or fixes are available.
Document it. Include original source.
Document your code. The commenting in code should be easy enough so if someone reads your code, they should understand by just reading comments, what your code does and how it works. If you have copied code somewhere and changed it, keep a reference to the original source. Not only that will credit the original author but also give new developers a reference if anything has changed recently.
Test it. Don’t trust your own code.
Never trust your code. Test it like its someone else’s code. Test it like you’re going into production now.
Let’s understand licensing
Licensing is probably the most important thing to understand if you are planning to copy publicly available code. No matter where you copy code from such as Github, online forums, blogs, or technical samples, you must understand licenses, and terms of use. Some public websites clearly say that their code should be used for learning purposes only and not be used for commercial and for profit.
The following table summarizes some of the common open-source licenses, their terms, and requirements referenced from https://choosealicense.com/licenses/.
License |
Permissions |
Conditions |
Limitations |
GNU AGPLv3 |
Commercial use
Distribution
Modification
Patent use
Private use |
Disclose source
License and copyright notice
Network use is the distribution
Same license
State changes |
Liability
Warranty |
GNU GPLv3 |
Commercial use
Distribution
Modification
Patent use
Private use |
Disclose source
License and copyright notice
Same license
State changes |
Liability
Warranty |
GNU LGPLv3 |
Commercial use
Distribution
Modification
Patent use
Private use |
Disclose source
License and copyright notice
Same license (library)
State changes |
Liability
Warranty |
Apache License 2.0 |
Commercial use
Distribution
Modification
Patent use
Private use |
License and copyright notice
State changes |
Liability
Warranty
Trademark use |
MIT license |
Commercial use
Distribution
Modification
Private use |
License and copyright notice |
Liability
Warranty |
Mozilla Public License 2.0 |
Commercial use
Distribution
Modification
Patent use
Private use |
Disclose source
License and copyright notice
Same license |
Liability
Warranty
Trademark use |
Common Development and Distribution License (CDDL), Eclipse Public License version 2.0, and New BSD License are other licenses with similar terms and conditions.
Meanings
- Commercial use – The licensed material may be used for commercial purposes.
- Distribution – The licensed material may be distributed.
- Modification – The licensed material may be modified.
- Patent use – The license provides an express grant of patent rights from contributors.
- Private use – The licensed may be used and modified in private.
- Disclose source – Source code must be made available when licensed material is distributed.
- License and copyright notice – A copy of the license and copyright notice must be included with the licensed material.
- Network use is distribution – Users who interact with the licensed material via network are given the right to receive a copy of the source code.
- Same license – Modifications must be released under the same license when distributing the licensed material.
- State changes – Changes made to the licensed material must be documented.
- Liability – The license includes a limitation of liability.
- Warranty – The license explicitly states that it does not provide any warranty.
- Trademark use – The license explicitly states that it does not grant trademark rights, even though licenses without such a statement probably do not grant any implicit trademark rights.
Summary
In this article, I shared some of my tips for copying and pasting publicly available code. When you find some code you want to use in your work or personal projects, make sure you read licensing and terms and conditions to use the code. If you plan to distribute and sell your products with copied code, I highly recommend that you review licensing.
Once you are good with the licensing, it is important to understand and rewrite the code in your own style and language.
I hope you found these tips useful. If you have any of your own tips to share, please share here in comments.
Please share this article with your co-workers and friends so they all can benefit from it. Thank you!