139

As a web developer over the years, I've done work in HTML, CSS, jQuery, PHP, and MySQL. And there have been numerous times I have found code snippets on Stack Overflow to be useful. I've probably copy/pasted small bits of code into a number of websites and projects. I personally feel like all of the code I've copy/pasted would probably be considered a "fair use" type of thing.

However, it looks like basically all Stack Overflow code snippets in the questions and answers are CC BY-SA with attribution required. I didn't find this out until recently! So now I am concerned about failing to comply with the CC agreement.

I already took a look at the attribution required link (at the bottom of all the pages). While helpful, it seemed geared toward "republishing content" whereas all I care about is simply copy/pasting some code here and there.

But if I were to attribute, how should I do it?

Some forms of attribution I thought of:

  • a public "credits/attribution" page on the website
  • lots of little attribution comments in the source code
  • one big "credits.txt" buried somewhere on the web server file system

Any answers or personal opinions on the matter are appreciated!

UPDATE (May 9th, 2014)

Well guys, as of the time of this edit, I must say this question is pretty subjective, but I've enjoyed the dialogue with all of you. Really the word "should" in the question can mean different things. I think @hakre's answer, in conjunction with @JeremyBanks gives the most "legal" answer. Whereas @Bill the Lizard and the other answers give the more practical, or reasonable approaches (though not necessarily "legal"). Personally, I am not settled completely on this matter, but for the most part will continue to copy and paste small bits of code without attributing them, and in rare circumstances perhaps I will message the "author"/copyright owner of the post to find out if I may freely use their code (without it being affected by viral CC).

It seems appropriate to me to bring up the quote "do unto others as you would have them do to you". And "love your neighbor as yourself".

11
  • 4
    related: meta.stackexchange.com/questions/12527/…
    – Wooble
    Commented May 5, 2014 at 15:30
  • @Wooble thanks. Definitely a variety of opinions on that question. I think Waffles' answer about declaring your own code to be in "public domain" on your profile is interesting at least. But it's unlikely that everyone will find his post and do something like that. It'd be useful if one of the ppl in charge of SO should consider (re)addressing this issue. I think most ppl are like me in that they just want to copy/paste small bits without worrying about attributing every little thing, especially in proprietary commercial code. And we don't want it affected by viral CC licensing.
    – D.Tate
    Commented May 5, 2014 at 19:44
  • 3
    I asked a similar question ages ago. For me, the takeaway was that code posted on StackOverflow is rarely, if ever, used verbatim. Thus, use SO links in code as a 'carry forward' attribution for sticking points that you -- or whoever reads the code -- may not readily understand. Commented May 6, 2014 at 22:38
  • 1
    Thanks @zourtney. Makes sense. There are plenty of times I've used SO code not verbatim, but rather as the barebones of something or a starting point. Then again, there are other times I have used, say, a regex snippet or some function nearly verbatim. For example, here is a function I think I copy/pasted one time and used verbatim: stackoverflow.com/a/5503957/923817. "Ketan" may not even be aware his function is now "licensed under CC" ... He probably intends ppl can just copy/paste his code and use however they want, even without attribution. That's my guess anyway.
    – D.Tate
    Commented May 6, 2014 at 23:16
  • 1
    I wonder if the authors here could manually relax that license thing. For example by adding "my answer is under the WTF license" to their contributions in case they really don't care. CC is there to protect them but even better would be to only protect those who want to be protected. A flag in the profile about the licensing of the contributed content (CC, PD, ...) would be nice. Commented May 7, 2014 at 18:59
  • I don't think that a code snippet of 5 lines is a that big thing to copy. Even if you use this verbatim, it's hard to get you nailed on copying this without credit; you'd probably need to copy a whole document for it to be even traceable. It's nice if you give credit to the original authors, but from a legal perspective you won't get any problems IMHO.
    – Sebb
    Commented May 7, 2014 at 19:49
  • 2
    Related request for some legal sanity: Propose license choice checkbox between CC BY-SA and CC BY. This situation is a completely unnecessary mess because Jeff didn't care enough about licensing to sort this out when he had a chance. At this point, it's probably unfixable, and an implicit encouragement to ignore copyright law. Commented May 9, 2014 at 2:31
  • 1
    @JeremyBanks Definitely messy I'll say. But you know, a lot of things are. Like there's a road I drive on with a speed limit of 55mph, but most people not only exceed that, but drive even 75mph. Consistently. Should the law change to match people's behavior?.. or maybe police officer's should start enforcing the law more?.. It's somewhat of a public policy and also a ethical/moral issue I would say. At the end of the day, I try to apply basic rules like "Do unto others as you would have them do to you". But it is still hard to figure this stuff out sometimes.
    – D.Tate
    Commented May 9, 2014 at 14:40
  • But you know, I can see a potential solution here: SO can trigger alerts (at the top of page) or send emails, inviting people to "opt in" to a new "profile licensing feature" which allows users to specify a blanket license for all their posts. It could work retroactively even. But it would be a one-time choice for the retroactive ones b/c otherwise people could just change the license back and forth, which would be really hairy lol.
    – D.Tate
    Commented May 9, 2014 at 14:44
  • Excellent point to bring up. Sounds like SO needs to switch to the BSD license, like Apache uses, so there won't be a concern with use of a SO snippet contaminating one's entire source code base.
    – Warren Dew
    Commented May 11, 2014 at 1:31
  • I understand the owner of the code can change the license agreement any time they want. if the site provides for such. maybe having the ability to post your own license(s) for your profile could be added with the ability to choose a license, even for a given snippet? just an idea. would take a little more work, and maybe a chunk more db space. if the db can compress it somehow, this would lessen the space used, especially since licenses are usually just plain text. I don't know of current db server features regarding this, but it would be worth looking into... Commented Jul 24, 2014 at 4:02

7 Answers 7

141

If you're republishing content (code, or an explanation of code) that you found on Stack Overflow in a blog post or article, the attribution must be public as well. You must1: A) clearly indicate that it comes from the Stack Exchange Network, and give credit to the author by B) linking to the original answer/question, C) clearly indicating the author(s)' username, and D) providing link(s) to their profile page(s).

If you're just using bits of code you found on Stack Overflow to get your program to work, then you're using the site as it was intended to be used. The attribution doesn't need to be publicly displayed, but you should put a URL in your source code comments so that you and anyone else who reads your code can go back to the original source if they need to.


1. These four requirements are explicit in the Terms of Service.

19
  • 16
    Thanks, I think there is wisdom in you saying "intended to be used". I don't think this has ever been a place where legal restrictions have been the priority, but rather, the sharing of information. (And racking up reputation points!!! :) )
    – D.Tate
    Commented May 5, 2014 at 15:15
  • 4
    @D.Tate Well all codes, questions and answers are being made available under cc by-sa 3.0 for a reason. And this is quite a legal restriction TBH. Some people posted proprietary code and were "black-inking" their question quite hard on few occasions =)
    – luk32
    Commented May 5, 2014 at 17:45
  • 84
    Any time you use code from SO you should always upvote it if it worked and was helpful at the very least. Commented May 5, 2014 at 18:01
  • 2
    @D.Tate No, they posted code from work i.e. company/academia, and were editing questions not to get caught with publication of the proprietary code, or after solving a problem tried to delete questions not to be accused of plagiarism, when they used code that was posted as an answer. Both could have had legal consequences. I've seen 2 or 3 cases like that.
    – luk32
    Commented May 5, 2014 at 19:08
  • 7
    @luk32 People flag their posts for deletion on the grounds "I don't want to get caught cheating" every day. I don't know what people are thinking sometimes.
    – Bill the Lizard Mod
    Commented May 5, 2014 at 19:12
  • 1
    @BilltheLizard Probably sometimes they are not thinking at all =). I just saw 2-3 extreme cases. Like this guy who edited all his posts and account. Funny stuff.
    – luk32
    Commented May 5, 2014 at 19:16
  • 4
    Bill, while this is no doubt how the site is meant to be used, it's not how the license is written. I like the whole "spirit of the law" thing, but a mention that re-use without a CC-compatible license violates that pesky "letter" thing would be worthwhile. The contributors hold the copyright, not SE, so intent wouldn't go very far with the lawyers. Commented Jul 19, 2014 at 0:12
  • 6
    Someone might read your second paragraph and think that's all she has to do to comply with the cc-by-sa license. The big one is the ShareAlike bit, summarized by CC as "If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original" and captured in section 4b of the license text. Basically, what Jeremy said. Your answer isn't wrong, but it'd be for the best if the highest-voted answer didn't leave that implication. Commented Jul 19, 2014 at 20:12
  • 2
    And anecdotally, this isn't just theoretical - it caused legal headaches for one of my projects recently. I would love if SE picked a more friendly default license going forward. Wonder if there's a request for this already... Commented Jul 19, 2014 at 20:13
  • 1
    But I write closed-source software most of the time. Why should I provide pointers to somebody who has just reverse-engineered my software. (by the way, I have yet to use code from SO).
    – delete me
    Commented Oct 12, 2016 at 10:39
  • 1
    Bill, I have adjusted the first paragraph to match the explicit requirements stated in the Terms of Service. Obviously, it's your answer, but it felt like you were trying to match that information more-or-less when stating the requirements for off-SE Network internet use.
    – Makyen Mod
    Commented Aug 5, 2017 at 6:10
  • 2
    I agree with others re: the 2nd paragraph. While just having the post's URI in your source code is probably what most users intend, it's not what's required by the license. The CC BY-SA 3.0 terms are actually more strict. Including "Share Alike" (SA), a big issue if publishing source code, particularly for languages distributed as source code (e.g. JavaScript/HTML/CSS, etc.), but SA, arguably, does not infect the rest of the code, only the portion originally under CC BY-SA (distinction between "Adaptations" and "Collections").
    – Makyen Mod
    Commented Aug 5, 2017 at 7:18
  • 1
    Other than "Share Alike", there are also requirements to include: A) the copyright notice for the work, B) at least a link to the license URI), C) the title of the work D) the original author(s)' username(s), E) a URI to the post, and F) clearly indicate any changes you've made to the code (as "reasonable to the medium or means You are utilizing").
    – Makyen Mod
    Commented Aug 5, 2017 at 7:20
  • 1
    So, if this answer contained code, which someone was incorporating (without changes) into their source code what would be required is something like: The following code is from the answer to "If I use SO code on my website, how should I give attribution? - Meta Stack Overflow" at https://meta.stackoverflow.com/a/253622, copyright 2014 and 2017 by Bill the Lizard and Makyen, licensed under CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/): Personally, I'd also include the date it was copied, but that is not required.
    – Makyen Mod
    Commented Aug 5, 2017 at 7:21
  • 1
    I don't see those 4 requirements listed explicitly in terms and conditions. but they are in the cc-by-sa 3.0 also explained here: stackoverflow.blog/2009/06/25/attribution-required Commented May 31, 2018 at 8:13
32

I've been wondering about the same thing, so after reading your question I did some searching.

From an answer by Jeff Atwood (by way of a comment by kajmagnus to this answer):

The cc-wiki license seems pretty clear to me on this point: free to remix and reuse, as long as you attribute and use a similar license.

That said, a snippet of code falls under excerpt category and thus should be free to use under fair use. Heck, we don't even support giant masses of code being posted, so to me, by definition, everything would be an excerpt. We're not sourceforge, github, or codeplex.

(emphasis mine)

I would still add a link to the SO/SE post to give credit where credit is due -- and also since most posts have good discussions which can be helpful.

10
  • 4
    Well if nothing else, it's pretty clear that Jeff doesn't think most (or any) code on SO really needs to be attributed since it's (presumably) fair use. So then perhaps the question becomes "why even have the license in the first place"? And, I imagine the reason is mostly to address republishing situations -- to ensure SO (and SO contributors) get appropriate credit.
    – D.Tate
    Commented May 7, 2014 at 14:43
  • 9
    If you are ever copy-pasting enough code that it's not just an excerpt, I'd argue you are doing something wrong. Commented May 7, 2014 at 17:34
  • 1
    You are most likely not excerpting (whatever the verb is :)) - Then it would have been a quote in a publication (and not working code). so it's more that you create a work with multiple copyright holders (combination, perhaps that remix Jeff was talking about). Hence Creative Commons is not really explicitly about the change of form of work, this might lead to some gray area and rights passed might not work with attribution only. E.g. if the form of remix does not allow to attribute adequately as the original author did intend it. CC licenses not working well for code.
    – hakre
    Commented May 7, 2014 at 19:47
  • @D.Tate I wonder if the license has any effect on people making contributions to the site?
    – paul
    Commented May 8, 2014 at 20:47
  • @paul Well, if I understand correctly, the answer is "yes". when you post anything to this website (even these comments) you are basically saying to SO (and the world) "feel free to use any of my contributions in accordance with cc by-sa 3.0". That being said, some (most?) people probably don't really know (or care) that this is happening. It's kind of fine print-ish IMO. Look at the bottom of this page... it says user contributions licensed under cc by-sa 3.0 with attribution required
    – D.Tate
    Commented May 8, 2014 at 20:57
  • @D.Tate I guess I should have said: "I wonder if the license has any effect on people deciding to make contributions"
    – paul
    Commented May 8, 2014 at 21:01
  • @paul Haha, oh I see what you're saying.. doh! You know that is something I have no idea of, but I doubt it does for most people. I mean, maybe if you are some kind of author, or have some proprietary code, then it would affect you. Then again, they probably wouldn't just post something in any kind of public forum anyway, regardless of the license. Unless it's like some super duper top-secret forum.
    – D.Tate
    Commented May 8, 2014 at 21:14
  • 8
    Jeff is no lawyer. Commented Mar 14, 2015 at 17:49
  • @JeremyBanks True; I guess the only way to be sure is to ask a lawyer.
    – paul
    Commented Aug 15, 2015 at 13:21
  • it is not at all clear you have to use the same license
    – user128511
    Commented Mar 17, 2020 at 15:55
13

Everybody is focused on the "attribution" clause of the Creative Commons license, but Stack Exchange isn't just using the Attribution license. Stack Exchange is using the Attribution-ShareAlike license, which also requires:

If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.

When you are legally required to display attribution (it is not obvious that fair use always applies) then you are also required to distribute your software under the Attribution-ShareAlike license. It is unclear whether this refers to your compiled software or the source code, but in any case this would prohibit conventional commercial software licensing, and many other restrictive licensing options.

As evidence, an explicit goal of the newest version of the Attribution-ShareAlike license is compability with the GPL. This strongly implies that it is the legal view of Creative Commons contributors that the previous versions of the license, such as the one Stack Exchange is using, are not GPL-compatible.

Putting an attribution comment in your source code is a good idea, but it isn't going to legally protect you.

8
  • 1
    The share-alike refers to the work as provided. There is no source-form requirement in CC. At least AFAIK, perhaps with 4.0 this changed (partially)?
    – hakre
    Commented May 9, 2014 at 15:40
  • Good catch on the point: Putting an attribution comment in your source code is a good idea, but it isn't going to legally protect you. I think you can also say it does not necessarily protect you, though it still could play a factor I would say. One other thing: I've been under the understanding that cc-by-sa 3.0 actually is like GPL because of it's viral nature. In other words, once it "touches" your code, you must not only include license disclaimers, etc., but that section of code / project, etc., is then forever under that license. or something like that..!
    – D.Tate
    Commented May 9, 2014 at 15:43
  • As far as I remember the GPL compatibility is because of that a copyleft license needs to explicitly allow to license under GPL as well because otherwise copyleft does not work (ongoing of the licence). So the "viral" nature alone does not help here, the licensing of the DVD you buy from a shop is also of "viral" nature. It does not change even you copy the DVD, the licence reciprocally stays on the copy, too.
    – hakre
    Commented May 9, 2014 at 15:45
  • @hakre You know, the whole source-form point is a huge aspect of all of this huh. Because technically, for web development, you have THE CODE but you also have THE WEBSITE.
    – D.Tate
    Commented May 9, 2014 at 15:45
  • This is why I personally as well as many others do not want to see DRM inside the browser. It will only cripple the usage of the web. Source form is essential to transport knowledge.
    – hakre
    Commented May 9, 2014 at 15:46
  • 2
    Yes, well I at least think people need to be more cautious about slapping licenses everywhere, without really understanding the ramifications. Things get HAIRY
    – D.Tate
    Commented May 9, 2014 at 15:49
  • 1
    I would assert that it's arguable as to if including a snippet of code from SO into a larger project would be considered an "Adaptation" or a "Collection". Personally, I'd argue that it would depend on the relative size of the copied code with respect to the overall project. IMO, if placed into the code of a significantly larger project it would be a "Collection", while the code that was copied from SO would be an "Adaptation". A "Collection" is not required to be distributed under a compatible license (see section 4c).
    – Makyen Mod
    Commented Aug 5, 2017 at 7:41
  • CC-BY-SA appears to be more like LGPL than GPL
    – user128511
    Commented Mar 17, 2020 at 15:56
12

This is an anecdotal answer, but I'll post it anyway.

I've used this good man's answer in a good many of my personal Swing projects where I needed an output stream hooked up directly to a JTextArea. I actually put a large block comment at the head of the class each time, like so:

/*
 * Thanks to Mikhail Vladimirov for the idea/implementation for this class.
 * https://stackoverflow.com/a/14706922/1435657
 */

It credits him specifically and harcodes a link to the answer for other people to see his work, view his profile if they want to, and maybe even hand him an upvote. If I ever actually published one of the programs or had it become widespread, I would make this attribution even more public, perhaps in whatever README file or official blog post I had about it.

If you're adding the code to your website, I would definitely put in a comment in the source code. Then anyone who visits your web page automatically downloads the attribution right into their browser!

4
  • Thanks Jeff Gohlke . I think you've made a commendable effort to give @Mikhail attribution for his work. On another note, I suppose it's debatable whether the code he posted is an excerpt though. It is somewhat long. I guess if you go more public with the code, you might as well just shoot him a question about it.
    – D.Tate
    Commented May 8, 2014 at 15:14
  • 1
    As @hakre pointed out in his answer, CC license has several factors involved in an appropriate attribution. I think if we are all downright honest though, probably few people on this site really (want to) follow CC attribution requirements "to the T", but are more akin to simply writing a blurb and a link. (like you did). That's my hunch anyway. Seems to be what @Bill the Lizard suggests in his answer too. I mean, who really wants to write ...a copyright notice, a license notice, a disclaimer notice... ... lol.. (see hakre's answer). Alas, maybe it's time for me to learn how to do so.
    – D.Tate
    Commented May 8, 2014 at 15:18
  • @D.Tate Yeah, there's not a chance in hell I'm going to do all that for a personal, non-commercial app. Haha
    – asteri
    Commented May 8, 2014 at 15:52
  • 3
    Also, I don't use his entire class. I just used the general idea and technique. I just felt that I took enough of it that I ought to give a bit of attribution to him.
    – asteri
    Commented May 9, 2014 at 2:24
6

On stackexchange.com/legal, SE has a very clear answer to this question (I bolded the steps to follow):

In the event that You post or otherwise use Subscriber Content outside of the Network or Services, with the exception of content entirely created by You, You agree that You will follow the attribution rules of the Creative Commons Attribution Share Alike license as follows:

  • You will ensure that any such use of Subscriber Content visually displays or otherwise indicates the source of the Subscriber Content as coming from the Stack Exchange Network. This requirement is satisfied with a discreet text blurb, or some other unobtrusive but clear visual indication.
  • You will ensure that any such Internet use of Subscriber Content includes a hyperlink directly to the original question on the source site on the Network (e.g., https://stackoverflow.com/questions/12345)
  • You will ensure that any such use of Subscriber Content visually display or otherwise clearly indicate the author names for every question and answer so used.
  • You will ensure that any such Internet use of Subscriber Content Hyperlink each author name directly back to his or her user profile page on the source site on the Network (e.g., https://stackoverflow.com/users/12345/username), directly to the Stack Exchange domain, in standard HTML (i.e. not through a Tinyurl or other such indirect hyperlink, form of obfuscation or redirection), without any “nofollow” command or any other such means of avoiding detection by search engines, and visible even with JavaScript disabled.
1
  • 1
    This is pretty much The Answer. Commented Jun 22, 2016 at 21:15
6

Stack Overflow requires attribution:

If supplied, you must provide the name of the creator and attribution parties, a copyright notice, a license notice, a disclaimer notice, and a link to the material. CC licenses prior to Version 4.0 also require you to provide the title of the material if supplied, and may have other slight differences.

Source; SO is CC-BY-SA 3.0 and this is perhaps a little different of what Jeff did comment back then (I assume he had spam websites in mind).

So have fun. I wonder how this makes sense when you build upon Stack Overflow. I've seen a project really written by Stack Overflow (a noob copies together from SO here and SO there), attribution normally never is done which results in rights termination under CC.

6
  • Thanks @hakre for the research. (I can see that the quote was pulled from the "appropriate credit" popover/tooltip on the CC page you linked). Do you care to give your personal opinion of all of this though?.. Do you find it annoying or reasonable that SO has the CC-BY-SA 3.0 on ~everything? (Also, if you know of one, can you point to an SO CC rights termination example)
    – D.Tate
    Commented May 8, 2014 at 15:01
  • Well, CC-BY-SA is in the spirit of sharing and protecting shared content in an accepted manner that works globally, so I'd say this is a good choice. The only problem I see is about source-code as CC does not work specifically well with it (for example there is no requirement of the source-form of a work which often is a pre-condition to create derivative works). As attribution must be given to the concrete source(s), this might not be that harsh, however, using such code the tends to become impractical as for little bits, long attribution overhead has to be done.
    – hakre
    Commented May 8, 2014 at 20:15
  • For copyright termination: wiki.creativecommons.org/…
    – hakre
    Commented May 8, 2014 at 20:17
  • Thanks for the link, that's useful. But if you know of an actual example (like a "case" or lawsuit) involving using code with regards to CC, I would be interested in seeing that. I agree with you that CC-BY-SA seems like a good choice, at least on a certain conceptual level. But can become impractical when it comes to snippets. And I think I understand what you are saying: code is not as "concrete" as something like, say, a song or picture. So "remixing" etc. is much less defined.
    – D.Tate
    Commented May 8, 2014 at 21:05
  • That's like it is. However, code on site is mostly exemplary so I do not think this really is a problem. I mean for that exmaple when someone copied that together large scale it is a problem, however only idiots do and you should not argue with idiots.
    – hakre
    Commented May 9, 2014 at 6:40
  • ah well yeah sometimes people are just going to do whatever. But you know, I can't blame people for wanting free stuff. I'm just trying to figure out how free the stuff is.. lol. Just a gray area it looks like.
    – D.Tate
    Commented May 9, 2014 at 15:04
2

In copyright lingo (see U.S. Copyright Office), a "derived work" is something that is reworked from a copyrighted work. It's still copyrighted by the owner. You can copyright their work if you obtain permission from them to do so. But I am unsure if that has anything to do with putting copyright lines in your code that says "this function is Copyright 1990 X Software, All Rights Reserved", etc.

Also, according to U.S. Copyright Office, if I remember right, when someone authors something, they own an automatic copyright on it, however, it's not defensible (you have to buy that privilege for about $35). and if you change the code, that's another $35 next year I think.

I noticed the GPL mentions that you can't GPL anything under 10 lines of code (that means no short utility scripts). This is not entirely from copyright law, just a GPL requirement because they wanted a number I think.

Note: the last one is regarding other people's code and what you may do with it/how/permissions. Interesting read.

Copyright Basics mentions the allowable formats of a copyright string (page 4).

This says that there isn't a fixed number of lines/percentage/whatever for "fair use"; it should be just "limited". One book publisher (I won't name names) charge fees for permission, even for little stuff that should actually fall under "fair use" under copyright law. So picking engineering books and book publishers takes a little work for me.

But when you see "All rights reserved", I don't know how fair use comes into play... maybe it still does, but that phrase does come with certain restrictions.

1

You must log in to answer this question.

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