Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Is there a size where you can copy under fair use for code you don't have a license for the purpose? For example, what if I copy a snippet that is (normally) 3 lines of code? Is that fair use? If it is fair use, what length is required before I need a license?

share|improve this question
Laws vary depending on where you are, and there are people here from all over the world. When asking a legal question, please tell us where you live and work. Moreover, don't trust anything we say. If it's important to you in some way (like financially), consult a local lawyer with appropriate experience. – David Thornley Oct 15 '10 at 17:11

2 Answers 2

up vote 9 down vote accepted

In the US, the legal doctrine of Fair Use does not apply to embedding excerpts of copyrighted works into source code. How such doctrine is applied by the laws of your jurisdiction may vary.

I will excerpt from a US Copyright Office article on Fair Use, and of copyrights generally:

Section 107 contains a list of the various purposes for which the reproduction of a particular work may be considered fair, such as criticism, comment, news reporting, teaching, scholarship, and research [emphasis mine]. Section 107 also sets out four factors to be considered in determining whether or not a particular use is fair:

  1. The purpose and character of the use, including whether such use is of commercial nature or is for nonprofit educational purposes
  2. The nature of the copyrighted work
  3. The amount and substantiality of the portion used in relation to the copyrighted work as a whole
  4. The effect of the use upon the potential market for, or value of, the copyrighted work.

The distinction between fair use and infringement may be unclear and not easily defined. There is no specific number of words, lines, or notes [emphasis mine] that may safely be taken without permission. Acknowledging the source of the copyrighted material does not substitute for obtaining permission.

It summarizes the legal limits of copyright, which Fair Use further limits, like so:

Copyright protects the particular way an author has expressed himself. It does not extend to any ideas, systems, or factual information conveyed in the work.

The safest course is always to get permission from the copyright owner before using copyrighted material. The Copyright Office cannot give this permission.

What the limits of what copyright protects tells us is though you cannot copy "snippets" of code via Fair Use, you can rewrite the way ideas, systems (including algorithms), or factual information are expressed in those snippets.

In short, 3 lines of code should be small enough for you to rewrite it so as not to violate the original work's copyright. It probably took longer to write your question than it will to do the rewrite.

share|improve this answer
I don't see anything in here that says that copying a snippet of code cannot be fair use. It explains the criteria that will be used to judge whether your use is fair use, if you get sued. – KeithB Oct 15 '10 at 16:00
The list is intended to be exemplary, not complete. That's why it contains the words "such as". If the code is largely functional, only a small section of the original work is taken, and the two works do not compete in the market, it's quite possible courts would find fair use. – David Schwartz Sep 4 '11 at 4:00
And what qualifications do you have to "expound on the law", @Huperniketes? You've written this as though you are quoting an authority that definitively shows copying code not to be fair use, but in fact you're just performing your own interpretation of statute without even citing any case law. – Mark Amery Feb 20 at 22:23
@Huperniketes: Google specifically raised the issue of fair use in relation to the copying of actual code (the rangeCheck method). Oracle did not attempt to claim that fair use somehow doesn't apply to code, because that argument is simply laughable. You are misreading the phrase "such as." Your claim that "programmers just open mouth and insert foot" is amusing in this context. – Kevin Mar 4 at 0:49
That's actually irrelevant to my point. The problem is that a list set off with "such as" is not exhaustive in any version of English, not even the bizarre English-from-Mars that attorneys speak on a day-to-day basis. The list you italicized is a list of examples, nothing more. It contributes nothing whatsoever to the actual meaning of the law, beyond acting as a hint to judges and lawyers about the general intentions of the lawmakers. Fair use is an incredibly gray area of law, and presenting any part of it in such black-and-white terms does your readers an enormous disservice. – Kevin Mar 4 at 3:29

I am not a lawyer.

However, you are absolutely free to use the following 3 lines of code in anything that you write:

for (i = 0; i < 5; i++) {
    printf("I am not a lawyer!\n");

In all seriousness, your question is quite broad and highly subjective. Ten lines of code from what? A highly specialized sorting algorithm? Some kind of firmware? One of the millions of configuration file parsers that have been floating around since the dawn of usenet?

The golden rule, really, follow the license from which the snippet came. If that is not available, use your best judgement.

I participate in several free/open source projects, some of them require a copyright assignment for anything not 'trivial'. All of them that have this requirement define 'trivial' to be ten lines of code added or modified. That is no way a substitute for competent legal advice from someone in your country, however.

My snippet gallery consists of hundreds of functions, all of them have the original author's information in doxygen style comments, as well as license info (if any, most are just public domain).

Unless clearly trivial (as in my humorous example), I would not re-use code unless I know that I have permission to do so. I also, always, follow the rules of whatever license applies.

share|improve this answer
+1 for "Not a lawyer." – Josh K Oct 15 '10 at 4:38
I very much doubt many snippets are in the public domain. – Martijn Jul 23 '11 at 16:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.