Code Licensing Issues
Before you can copy and paste code, you have to make sure you have the legal right to copy and paste that code. Without that legal right, you would just be stealing other people's intellectual property. That would just be lazy.
Most code on Stack Overflow are 'snippets'. They do not meet standards of originality and as a result have no copyright protection. You are free to copy and paste them without any worry about losing a lawsuit.
Some code on Stack Overflow are much longer than 'snippets'. They therefore qualify for copyright protection. By default, all content on Stack Overflow (including code) is licensed under the CC-BY-SA 3.0 license.
This license is not recommended for use in software at all, and SO is currently looking to replace this license for future code posted on its website. For now, though, the CC-BY-SA 3.0 is law. Even if SO does change its future licensing policies, all previous code will still remain under CC-BY-SA 3.0.
CC-BY-SA 3.0 is a copyleft license just like GPL 3.0. Essentially, you can use all code licensed under CC-BY-SA 3.0 so long as:
- You give attribution to the website where you got the code.
- You license your codebase under the same terms as the CC-BY-SA 3.0. Again, the CC-BY-SA 3.0 is not recommended for use in software, so licensing your software under CC-BY-SA 3.0 is pretty much a non-starter.
However, there is some legal wizardy you can do to relicense your Stack Overflow code from ugly CC-BY-SA 3.0 to a proper Open Source license that is suitable for software. Here how it works.
- Any creative work licensed under CC-BY-SA 3.0 can be upgraded to CC-BY-SA 4.0, automatically.
- According to the Creative Commons website, CC-BY-SA 4.0 is compatible with both GPL 3.0 and the Free Art License. Both the GPL 3.0 and the Free Art License are copy-left licenses.
There is no real way to distinguish between a 'code snippet' with no copyright protection and a much larger block of code that does contain copyright protection (and thus is licensed under CC-BY-SA 3.0). You could use common sense to decide whether some code qualifies as a "snippet" or not, but if the courts share a different definition of a "snippet" than you, you are out of luck.
If you are working on a permissive or proprietary codebase, you have four options.
- Change the codebase to GPL 3.0 or the Free Art License and start copying and pasting code with ease.
- Hire an expensive lawyer to defend your right to use code snippets from Stack Overflow...
- Hope that the person you're copying and pasting from doesn't care enough about your violation of the CC-BY-SA 3.0 license to sue you.
- I recommend this approach because the odds of the other user stumbling upon your codebase and complaining about you not adhering to the terms of the CC-BY-SA 3.0 is lower than the odds of this book actually being published by O'Reilly. If you are very unlucky (and that can happen), then you can simply apologize and then rewrite the offending code.
- ...or write your own pseudo-Clean Room implementation of the Stack Overflow code.
Here is a step-by-step guide:
- Write down some notes about the code from Stack Overflow, specifically how it works, how you should implement it, etc.
- Close the web browser.
- Wait a few minutes so that you forget the code.
- Use the information in your notes to re-implement the Stack Overflow code in question.
- You now own the copyright to your code in question...even if the final code is exactly the same as the original Stack Overflow answer. Since you now own the copyright to this code, you may integrate it into your permissive or proprietary codebase.
This approach is slower than the traditional "CTRL-C, CTRL-V" approach to copying and pasting. However, it does have the benefit of giving you a deeper understanding of the Stack Overflow code. This could be helpful as you will likely need to maintain this code in the future.
There is one last point to conclude in this chapter. Some users may add a permissive license to their code submissions to Stack Overflow. In that case, you can elect to adhere to the terms of this permissive license instead of worrying about the copyleft CC-BY-SA 3.0. Check the profile of the user who posted the "code" to see whether the code is available under a different license.