I noticed today that stackoverflow's cc-wiki (aka creative commons attribution/share alike license) does not allow the answers to questions to be used in commercial products.

Very specificially, and perhaps I've misunderstood the license, it has a share-alike clause, meaning any derivative work would also be share-alike.

Corporations eschew such clauses, because while the answer might be necessary, or even key, they must retain the rights to their own work. Not just corporations, but anyone who holds a copyright.

So what's the right license to put on sample code? I don't want people ripping off my code or productizing it with little to no work of their own and making tons of cash. But I also want larger projects to be able to use it.

EDIT: Some clarification. I actually want commercial usage to be ok. Supposing I write a good caching algorithm in answer to a question; I don't mind if some larger project like Mozilla or IE wants to use it, even a commercial product, but I don't want someone to take the algorithm and start selling it.

IE, it's ok if they want to USE the algorithm in their product, not OK if their product is the algorithm.

  • 7
    WRONG! CC-BY-SA does allow usage in commercial products; to disallow such usage (like CC-BY-NC or CC-BY-NC-ND) would make it not a free license. See freedomdefined.org/Licenses/NC . It is true that it is Share-Alike meaning that derivative works are also share-alike. This makes it a copyleft license (like the GPL), and it entirely compatible with copyright. Commented Jun 4, 2009 at 17:25
  • Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. Meaning you can't distribute your larger work under your own for-fee nontransferrable license.
    – davenpcj
    Commented Jun 4, 2009 at 20:06
  • 1
    @davenpcj: That's right. But it does allow commercial usage, in the sense that someone can sell (derivatives of) your code as long as it's also available as free software. See gnu.org/philosophy/selling.html Using a non-commercial license prevents people from putting a derivative on a free-software CD they're selling, for example. Or for using it in any free-software project at all, because "non-commercial" is considered excessive restriction and is not compatible with any of the common free-software licenses. Commented Jun 5, 2009 at 1:56
  • @Shree: The problem is not sale, but the legal burden of having to provide the rest of your proprietary code.
    – davenpcj
    Commented Jun 6, 2009 at 19:46
  • A product can not be expected to share alike. SAMPLE code is intended to be copied. To avoid legal problems, a product developer has to make sure that code is not like the sample. This sort of defeats the purpose of a SAMPLE. A SAMPLE is different from a piece of library source be while library source share-alike seems fine, SAMPLE code is designed to be in a product to use the library. SAMPLES should be distributed as public domain, unless you believe that there is significant creative content, and then why are you calling it a SAMPLE?
    – AgilePro
    Commented Mar 18, 2016 at 23:29

5 Answers 5


For anything I'm willing to type into an answer, I'd suggest allowing free use. If it's something I want to keep, I simply won't answer with it. The amount of code I can reasonably type into one of these things is simply not worth protecting, and the amount of effort I'm willing to put in is also not worth protecting. Similarly, if you're posting code here that somebody could just run with and rake in the cash, I'd say you're posting too much.

Furthermore, it's dubious that the typical code snippet around here has the necessary creativity to be copyrightable in the US. It tends to be very short and based strictly on the necessary functionality. (In the US, at least, it is not possible to use copyright to protect functionality, and therefore a purely functional something isn't eligible.)

So, I'd say allow free use of snippets, and only worry about protecting larger projects. For that, you could use the GPL or some non-commercial license or something like that.

  • 1
    GPL is decidedly not "free use". There are myriad ways to violate the terms of the GPL. For example, putting such code into an MIT-licensed or proprietary project puts me into a litigious gray area (I'll likely get replies arguing both the defense and prosecution...). The free use license is the Unlicense.
    – bukzor
    Commented Mar 10, 2014 at 18:18

I think the LGPL does exactly what you want -- if a person wants to incorporate your code in their code, they can; but if when the product is your code, they have to distribute the source with the binary. I would use v3.0, because it's better written IMHO but YMMV.

  • 1
    gnu.org/copyleft/lesser.html Upvoted, because I think it's as close as I can find, but it still has some issues. Producing a "Combined Work" requires providing enough of your application in linkable form to relink it (Clause 4d). 4d1 allows using a shared library instead, which is closer, but that doesn't work all the time, because some projects don't have replacable libraries. Think firmware, kiosks, phone apps, anywhere the base platform is closed.
    – davenpcj
    Commented Jul 8, 2009 at 15:22
  • No, LGPL does not work for EXAMPLES. The point of an EXAMPLE is that it is expected to be copied. An EXAMPLE is to illustrate the use of the library. If the EXAMPLE requires LGPL license, then a product can NOT follow the EXAMPLE! If it does it is considered stealing. Say you have a library and you have an EXAMPLE of how to use the library. By restricting the use of EXAMPLE you then guarantee that products can NOT call your library in the way that the EXAMPLE does. In fact, the product would need to make their code different from the example, to avoid plagerism accusation.
    – AgilePro
    Commented Mar 18, 2016 at 23:15

Did you take a look at the Creative Commons License?


Example: Attribution/Noncommercial
means that everyone that uses your work has to give you credit.
and none of your work can be used for commercial purposes

This might be along the lines of what you are looking for because you can choose a level of control you want over your work.

Also, if you are writing a library you might be interested in checking out the LGPL. People could still technically make money using your library here, but at least they would have to license it under the GPL/LGPL


I hope this helps.

  • Which Creative Commons license? The problem with Creative Commons is not that the licenses are bad in some way, but that there are several of them and too few people specify which one they're talking about. Commented Jun 4, 2009 at 17:12
  • the specific license choice is up to the author (which is what I really like about the CC). you can give certain freedoms or place controls over your work on a case by case basis using the same licensing framework. Commented Jun 4, 2009 at 17:16
  • It's a nice framework, but CC on its own doesn't designate the specific license. I'm upvoting based on the edit, which I think covers CC much better. Commented Jun 4, 2009 at 17:21
  • right, I thought it needed some clearing up after your question. Thanks for your input. Commented Jun 4, 2009 at 17:25
  • 2
    Oh god, please don't suggest non-commercial. See freedomdefined.org/Licenses/NC -- the -NC licenses are *not free software licenses, and a non-commercial restriction needlessly cripples usage of the code with no gains (unlike the share-alike restriction, which ensures that derivative works will also be freely available). Commented Jun 4, 2009 at 17:27
  • 2
    When I was a maintainer of an Angband variant, I found the "non-commercial" part of the license a real pain. I couldn't host on Sourceforge, and it was never clear if somebody could charge for a CD or floppy with Cthangband on it. I'd never use it. Commented Jun 4, 2009 at 17:30
  • i'd never use it either, but davenpcj asked specifically about keeping people from making money, so I was using it as an example. Commented Jun 4, 2009 at 17:43

I am not a lawyer and you should be asking a lawyer instead of listening to me here.

Code samples are such a small part of an overall application it's likely a stretch to claim derivation, for starters.

I must admit I don't really see the point of licensing sample code. If said code does something extraordinary it might actually be a patent (I know, I know) you're looking for and not a license, at least to start with. If the code merely shows how something is accomplished then I'd wonder whether the code sample even qualifies for IPR protection.

Oh, and SO's license does not prohibit commercial exploitation of the content here. ShareAlike does not preclude commercial exploitation (there's a CC license variant which does; Noncommercial.

I'm not sure I am understanding the distinction you're making between people ripping off your code and larger projects using it. What is the variable there, size of the product using your sample code? I don't know of a license which discriminates in favor of larger derivative works but you're always free to take an existing license you like and add a clause to it specifying for example what proportion of the final product your sample code must constitute as a maximum in order for the license to be valid. I think that that kind of clause would be a pretty big deterrent to use though.

Note also that slapping a license on code does not mean foregoing any rights to the work. License and copyright go hand in hand. Copyright says "I own this code" and license says "since I own this code, I am empowered to let you use it given the following conditions".

  • 1
    Part of the reason I wanted to ask here is because I suspect the cc-wiki license SO is using might be improved.
    – davenpcj
    Commented Jun 4, 2009 at 19:00

Is this a question to the management, the elite, or the masses?

I will answer it from my own opinion down here in the masses:

What's wrong with using the GPL or a variety of similar copyleft licenses?

Why not have an "attach code" box that allows you to specify from a list of approved licenses, all of which are copyleft to some degree?

I'll share how this directly affects people here with an example that happened to me just this evening.

I recently got screamed at, well-actually, typed at, by a few people for posting GPL'd code for HAMP Home Loan Modifications here. This is a hot topic important not only to many people's finances and whether they can stay in their home but also as a public policy topic that gets argued back and forth.

The spec of how to do HAMP modifications is a US Federal public document, subject to change, and outside my control so I lose very little releasing GPL'd code into a big forum and would have a lot to gain trying to see if I could learn some good refactoring and UI techniques or possibly find partners by demonstration. I made clear, which I think is only honest, that I wanted to create a subject oriented website from it and that my intentions were to find some of the partners right here and now.

The immediate remarks in the initial minutes were a mishmash of the following and so I voted to delete:

  • stackoverflow is not the place to start your business
  • stackoverflow is not the place to refactor code [despite several postings of that nature already and studies of, say, how to write factorial algorithms in 50 languages (I contributed "Oh fork, another example in Perl" that forks log(2,N) times)]
  • You must use the cc-share alike license
  • You may have lost all the rights to your code the moment you posted it here (an attempt to annoy and scare? I thought only that big software company people hate uses FUD. Anyway not true, I'm afraid; I had to go read and find it but cc-share alike is based in the rights of original creators and allows alternative licenses from the original creators and on this point is similar to the GPL)
  • Go read legal, or, hand it to your lawyer and run up a bill if you are at all serious about this (implying suspicion that I don't know WTF I'm doing)
  • You can't take ideas from here and make proprietary, non-free code with it (actually, as someone above points out -- mere ideas have no copyright; as to suggesting I want to steal code, aint that a great way to greet someone who wants to do something positive?)
  • Why don't you go to refactormycode.com? (this might have been helpful as there is such a place or it could mean just go away...)

I don't know if any of the people who were upset are partners of the stack overflow business or just users like myself.

Of course I am aware of the meritocracy driving the site, to give super duper users powers similar to partners, and that there are a few arrogant people mixed among all the helpful ones. People who unfortunately believe that a 2,500 in front of their name means they are definitely not a user "just like myself" with only a 450.

A lot of programmers and others are out of work..... so what's wrong with trying to start a shoestring startup here, based on free code, and meet similar enthusiasts? If it goes nowhere, no one is hurt. If it goes somewhere, it doesn't have to be by stealing ideas or robbing the customer.

So what's the deal with the specificity in licensing and the attitudes. Can't the forum be adapted to accept various licenses, goals, and attitudes? Thats my answer at least... the license should be anything reasonably compatible with the goals of the forum.... Or is this place only about one liners, homework questions, and the like...?

  • Without addressing the other issues, most code on SO is so short or so trivial that multiple licenses is a lot of work and complication for little to no benefit. Various goals and attitudes are definitely encouraged, but if the code can't be posted under SO's license then it probably just shouldn't be posted – in the interests of keeping the already complicated issues of copyright and licensing as simple as we can. Note you can instead use pseudocode or example code that expresses exactly what you want to ask/answer, too.
    – Gnome
    Commented Jul 15, 2010 at 6:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .