I'd say the very existence of this question proves a bad thing - The job of a programmer has become less programming, more legal clutter and bureaucracy
–
K.SteffMay 12 '12 at 14:53

3

Copy it. A trivial function like min is not covered by copyright. See my answer for details.
–
Tony the PonyMay 12 '12 at 15:59

6

@Loki, you're missing it by a mile. Algorithms are covered by patents, not by copyright. Copyright expires 70 years after the death of the author, so works copyrighted in the 60's are still quite fresh. Finally, the question is about copying THIS code, not about the algorithm.
–
alexisMay 12 '12 at 22:22

14 Answers
14

Make sure the formatting of the code fits your coding standard and also you should probably remove or rewrite the comment. No one will ever know you copied it - when a piece of code is this simple, you might as well have written it from scratch. If your coding standard somehow requires the function to look exactly as it does in the snippet, so be it - as long as it looks as it would look if you had written it from scratch.

Think about it, this is hardly(!) the first time this exact piece has been written - when something is this trivial, there is little reason not to copy it, if you do not feel like writing it yourself.

Even having this discussion seems a little superfluous to me - we need to be pragmatic if we are to get any real work done!

the real questions is where is the barrier between trivial and non-trivial and is there way to be absolutely sure that people will not point my code with something alike "LOOK IT'S MY GPL MIN REALIZATION!"... I really afraid those people >_<
–
HeatherMay 12 '12 at 8:24

19

Well, there is the concept of threshold of originality in copyright. And such a simple method should hardly be copyrightable. IANAL, though, and jurisdictions vary. Still, something that every CS undergrad has written at least five times in quite exactly the same manner might very well fall under this.
–
JoeyMay 12 '12 at 8:30

4

@nilu: Let us then say that the programmer wrote his own version of MIN and it ended up looking exactly like the one from math.c - would it then be okay? Yes, if it was an accident. "What difference would it then make if he had just copied it?" Bits have color. ansuz.sooke.bc.ca/entry/23 This is not something most computer scientists can understand - it is indeed a complex topic, but it is something that is recognized by law. If you are in doubt about whether something is legal or not, you had best to talk to a lawyer.
–
Mark ByersMay 13 '12 at 16:37

4

@alexis: I understand your point, but I do not really agree - if the resulting code is identical, no one but the programmer can know if it was copied or written from scratch. Obviously, one should have little trouble implementing min and it would probably take less time than finding another implementation. However, if a programmer does indeed copy it and no else knows that he has not written the function himself, what damage has then been done? Unless the programmer is feeling some kind of 'guilt', the answer is none. The rest is simply semantics.
–
niluMay 13 '12 at 17:43

5

@MarkByers: When I say 'cannot prove', I mean that no physical evidence exist to show that a crime has even been committed - the crime which has been commited is completely confined to the mind of the programmer. Obviously, this cannot be the case if some sweets are stolen - this has a physical impact on the world (lollipop -= 1). If the 'crime' is confined to your own mind, whos to judge whether it is moral or immoral?
–
niluMay 13 '12 at 21:31

I hereby release this glorious, complex macro to the public domain. Though I may have to publish a paper on this technique.

If you're macro-phobic, try this inline version (really, unless you're coding on a SPARC, you'll save lots of CPU cycles by not wrapping a function as ubiquitous as min in enter/exit code):

inline int min(int a, int b)
{
return a < b ? a : b;
}

The issue of code licensing for trivial snippets is a bit of a nuisance, really. Check with a lawyer, but I'm tempted to assume that they can't really be licensed and/or the licensing can't be enforced in practice. Same as you can't copyright or trademark the word ‘word’, then lean on people who use it. (oops)

Oh, they can certainly copyright that code, but given the simplicity of the algorithm, they'd be hard pressed to prove in court that you copied it unless you did something stupid like left comments intact, etc. The rule is not that you can't use the same code, the rule is that you can only do so if you wrote it using your own brain, and did not copy the original in any fashion.
–
Steven BurnapMay 12 '12 at 4:57

2

@StevenBurnap Of course the original code would also have the same problems: can they prove they have copyright (ie. that they didn't copy it from some earlier implementation)?
–
RichardMay 12 '12 at 8:26

19

Problem with macro version of min comes when you try to do min(a++,b++).
–
user1249May 12 '12 at 8:45

4

"release this...to the public domain" is very US-centric. It doesn't mean the same thing in many countries and indeed in some is not legally possible.
–
user4051May 12 '12 at 11:46

9

Thanks, but on a compiler from the 2000s, a compiler can inline trivialities like min.
–
DeadMGMay 12 '12 at 14:34

This implementation of min is not copyrightable, since there are only very few reasonable ways of writing it.

In the legal world, this is known as the merger doctrine (also known as the idea-expression divide). The (non-copyrightable) idea of the function is said to have "merged" with the (potentially copyrightable) expression. Therefore, the code is not subject to copyright -- you are free to use it.

To be on the safe side, don't copy it verbatim. Use different names for variables, apply your own formatting, don't copy comments word for word. The exact literal representation of this code may be covered by copyright.

Keep in mind that this doesn't guarantee someone won't sue you, but at least you will have a strong defense.

Also, bear in mind that even if a piece of code isn't protected by copyright, it might still be covered by a software patent (This is not the case for your min function).

It's an absurd situation, I agree, but you have only yourself to blame. If you are writing proprietary code, you should not be reading GPL-licensed source files, period. Remove math.c from your filesystem, keep the documentation, and feel free to re-implement any part of the API that you need. If you don't read the code, you don't need to worry if your own code turns out similar.

Wasteful, you say? The GPL intentionally prevents you from benefiting from free code without giving back to the ecosystem. If you can't or won't meet its terms, and you don't want to implement yet another math library, you can always buy one or find a free one that comes with a license you can accept.

Microsoft's programmers are not allowed to read open source code, to protect the company from legal problems. For purely ethical reasons, you should do the same with any program whose license you don't intend to adhere to.

You don't have a right to use this math.c, but nobody is trying to corner the market on implementations of min. The goal of the GPL is to increase programmer "freedom" (and the end user's freedom), not to restrict it. The GNU license, the FSF, and the Free Software Movement started with Richard Stallman, and Stallman started with re-implementing from scratch each new version of Symbolics lisp as it came out. The FSF would not (and could not) go after anyone for re-implementing a GPL library.

So but, it's really strange example. It's example, that means I must to read it and follow it... but as far as I understand I'm free to not pick GPL license here. So I know who is Stallman ;)
–
HeatherMay 12 '12 at 11:50

2

Sholy, sorry but I've no idea what you're saying! What example? (Are you also asking me who Stallman is? Google it. Or just check wikipedia.)
–
alexisMay 12 '12 at 12:06

2

"Microsoft's programmers are not allowed to read open source code" oh my, that's horrible - is there anywhere i can read about that? Google isn't helping.
–
naiadMay 12 '12 at 12:52

Good question. I read about it in some Iron Python development forum, but unfortunately I cannot find it now. Summary: Jim Hugunin (hugunin.net/microsoft_farewell.html) mentioned that they aren't allowed to look at the python implementation and someone jokingly asked if they're allowed to look at their own code (Microsoft had made Iron Python open source). The answer is: They made a special exception for Iron Python.
–
alexisMay 12 '12 at 13:15

The people whose freedom the GPL is intended to increase are all the users of the program, not (specifically) programmers.
–
James YoungmanMay 12 '12 at 22:29

That's copyrighted code. You cannot copy it without permission of the author.

You will have to write your own version of min, or copy a variant of min that uses a different license.

Remember, with copyright, it's the code that is protected, not the algorithm. Copyright law comes from text. Even if "math.c" was nothing but comments and had no executable code at all, it'd still be protected under copyright.

What means own version? MY OWN version of MIN, sounds hilarious!
–
HeatherMay 12 '12 at 4:58

2

Remember, you don't own min, you own a particular sequence of characters that happens to implement min.
–
Steven BurnapMay 12 '12 at 5:00

2

You cannot change it at all without permission of the author except for personal use. You have to rewrite it from scratch. That's the whole point. Note that if you sat down at a blank editor and wrote something nearly identical to that, it'd be fine, because you didn't copy it. (Assuming you weren't typing from memory.)
–
Steven BurnapMay 12 '12 at 5:04

2

No, I am not joking. And yes, you can probably get away with it in cases like this. It'd be up to the copyright holder to prove that you'd done it. Note that I am just telling you what the law is, not defending it.
–
Steven BurnapMay 12 '12 at 5:10

3

Because if you don't recode these trivial things, you're using someone else's work in a way that they haven't given you permission to do. If it's so trivial that you don't need it, don't use it.
–
alexisMay 12 '12 at 11:47

Yes, but "common sense" isn't the bar for copyright. It's whether they copied and edited or wrote themselves from scratch. Whether something is "Common sense" is the test for patents, not copyrights. In other words, Oracle can copyright that text, but they can't patent that algorithm.
–
Steven BurnapMay 12 '12 at 16:49

These 9 lines are a classic example of the merger doctrine. How many other ways are there to perform a range check on an array?
–
Tony the PonyMay 12 '12 at 16:52

Few people have mentioned that the person who copied this text at Google is also the person who wrote it at Sun. Programmers have to realize that Stallman--the crazy bearded extremist--was right. Money, that root of all evil, makes people forget why they participate in technology in the first place. :-/
–
HostileForkMay 18 '12 at 0:44

I won't use this code anyway, because the comment doesn't match with what the code does. The comment talks about integer, while the code uses unsigned int. Who knows what errors are buried within the depths of this function?

Seriously, this is where "Don't reinvent the wheel" becomes ridiculous and you forgot to take it with a grain of salt. Just write your own version from scratch, match it with the naming convention and style guide of your code (e.g. math_min_uint(x, y)), make sure it works (unit test or whatever) and go on with life and without copyright issues.

With growing experience, these trivial snippets you need again and again are just in your toolbox and you will rewrite them without thinking too much, or simply get them from your own library.

People seem to be forgetting that copyright law, both in spirit and letter, means that the act of actually copying is prohibited - not the act of expressing the same thought yourself, not even if the result turns out suspiciously similar.

If you take the GPL'd code, modify it, use it in a way that is against the license under which you received it, and claim it is not a derived work, then you are breaking the law. Even if you use your second version - you have changed the names of all variables, and altered the formatting, but it's still derived from the original.

If, however, you write your own version of min, and due to the triviality of the task, the fact that there are only so many ways you can sensibly write such a thing, and a bit of coincidence, your version ends up being exactly identical to the GPL'd code, then you haven't copied anything.

Whether or not either story would pass in court is a different matter though. If a larger or more complex code sample shows up somewhere else, complete with comments, formatting, and spelling errors, you'll have a hard time convincing anyone that you wrote it independently, more so if it can be shown that you knew the original source (which, given the fact that you have posted it here, should be fairly trivial).

Just write it yourself in 30 seconds. You can use exactly the same algorithm. Just don't copy/paste.

Copyright only protects the expression of an idea, not the idea itself. You can use the idea directly providing you don't copy it.

FWIW, I doubt that copying such a trivial piece of code would get you into trouble anyway. Damages would be zero, and it would probably fall under fair use in most jurisdictions. But legal cases are such a pain that I wouldn't want to risk it.

That buys you generality, and it will work for any comparable type. Also: when whatever d-bag tries to claim they invented it and is covered by software patents, they'll be rumbling with the ISO committee...not you.

In the general case, take note of who published the code. Corporation, or sane individual? If it's a sane individual (as is the case with much open source), write a nice note to them and get permission. Save their response saying it's okay. Problem solved.

For the general case: More complex code that is definitely copyright-able, as opposed to min and max (that may or may not be copyright-able).

Then License your code in compliance with the library i.e. GPL!
Note however that there are LGPL libraries (and GPL with exceptions) that let you use their implementations while giving your code whatever licence you wish.