Stack Overflow is without doubt a very useful resource of practical knowledge. However, it’s not as useful to think of it as a repository of reusable code snippets, not only because of the obvious problems with copy-pasted code, but also because of the license of the code posted on Stack Overflow and how it works (or doesn’t) with other software licenses (free or non-free.)
Every user-supplied answer (including code snippets) on Stack Overflow is covered by Creative Commons Attribution Share-Alike 3.0 Unported License (CC BY-SA for short), which is a great choice for collaborative documentation and many many other things. It’s also the same license that covers the content of Wikipedia.
However, Creative Commons specifically advises against using any of their licenses for software. Apart from the fact that the CC BY-SA license is not tailored for software, it’s a copyleft (a.k.a. share-alike) license, which means that if you create a work based on a CC BY-SA-covered work, you may only publish it under the same license or a compatible one. As the Creative Commons FAQ states, “our licenses are currently not compatible with the major software licenses, so it would be difficult to integrate CC-licensed work with other free software.”
This means you can not take code licensed under CC BY-SA 3.0 and release it under the MIT license, the GPL or anything else.
As a side note, the CC BY-SA 4.0, under some conditions, may be compatible with GPLv3 (yet not GPLv2.) But since Stack Overflow uses CC BY-SA 3.0 and not 4.0, this possibility doesn’t apply.
Last but not least, the use of CC BY-SA code in proprietary software contradicts the core idea of the license, which was designed to make sure that adaptations of a work remain under the same share-alike conditions (unlike some other Creative Commons licenses, say cc by or cc0).
It could be argued, of course, that reusing small code snippets can be considered fair use, or that they are too small to be copyrightable. (Copyright doesn’t cover ideas, after all. It covers the specific form in which an idea, or something else, was expressed.) However, “the place where it would be argued is a court room”, as one StackExchange commentator has put it and it also highly depends on what kind of snippet is discussed.
While some answers contain simple one-liners, others may provide an encapsulated solution that is nearly suitable for releasing as a package or added to an existing package/library as a feature.
In other words, what is fair use and what isn’t is not all that clear, especially in the context of software and especially taking into account the differences in copyright law from country to country.
Stack Overflow has briefly considered moving to MIT license for new code, but they afterward decided to postpone the decision indefinitely. And that makes sense, since the content of Stack Overflow is not only code, and MIT is a software license.
So before reusing code from Stack Overflow in your project, free/libre or closed-source, consider using the idea instead, and expressing it in your own form that is organic to your project.
I’d like to also mention that I don’t view Stack Overflow as a kind of universal commons, since, albeit the content on the site is free and “generated” by its community, the source code of the site itself is not, and the whole network is owned and operated by a for-profit company that sets the rules of the game, has access to the user data, etc. This isn’t an evil thing in itself, but I think it is an important difference from non-profit community projects (which might have their own set of issues.)
It’s also interesting to note that while Stack Overflow seems to emphasize the attribution requirement of cc by-sa (see the website footer or the legal page), they hardly mention the share-alike requirements of the license. This suggests to me that the license they chose to use for the site was not such a key element of the philosophy or mission of the site, as it is for other projects where the spirit is really about copyleft and free/libre knowledge, and where the mechanics of reuse are an important philosophical part of the project.
Disclaimer is I edit Wikipedia from time to time and I’m also connected to the Wikimedia movement, so my comparisons to Wikipedia may be biased. However, I don’t think there is any overlap between Wikipedia and Stack Overflow in their project scope, so at least there isn’t any conflict of interest between the two.
Ah, and by the way. I’m also not a lawyer, by any means. Just saying.