I'd like to be able to post snippets of my codes in questions, answers, code reviews and whatever and be able to learn/copy/change/profit from other questions, answers, code reviews and whatevers without having to worry about legal consequences. I want Stackoverflow to be a place where I get help and I can help, not a place to cause me legal consequences. Is this achievable?
There are two separate issues: Contributing your code to Stack Overflow, and taking code from Stack Overflow.
As long as you solely own the copyright for the code, and you accept that by contributing it you are agreeing to license it to Stack Overflow so they can license it to their users, then you can go ahead and contribute your code verbatim.
As long as you have been given permission by the copyright holder you can do the same. This might be the case where you have code that answers a question inside existing code who you've already assigned the rights to your employer or client. They need to give permission, and once you've received that you're fine.
If neither are true, but you know the answer, just don't cut and paste from existing code bases. Start writing the answer, write the code with the caveat, if needed, that you haven't tested it, or simply refer to the methods, classes, generic algorithm name they need to look up in documentation, and link to those on other sites. There's no need to copy from your existing code to answer most questions. Most code answers are short enough, anyway, that you can write something altogether new.
This only covers code snippet copyright and licensing, though. If there's a trade secret, or client/company specific knowledge that would help the asker, you might want to make sure that you own the idea and haven't signed it away before giving that away - it may not be code, but it may still be covered by agreements and contracts you've made with others.
It really doesn't matter what license Stack Overflow adopts. I recommend you never copy code as-is from the site. Instead, learn a little about clean room reverse engineering and follow those tenets as closely as you believe necessary to separate your output from your learning.
A simple process might be the following:
- Develop per your normal methods, using official documentation and other relevant resources which you won't be tempted to use cut-n-paste on.
- Write down questions and problems you have if they don't prevent you from proceeding. Make notes in text and code where these problems exist so you can easily revisit them later. Code for as long as you can.
- When you can't proceed any further, shut down your development environment and open your learning resources where you might be tempted to cut-n-paste from, along with your text notes.
- Research your questions. Write the notes and answers needed in your text notes. Do not cut-n-paste. Particularly valuable are references to classes and methods you might need, and short descriptions and sketches of algorithms. Don't write code at this point - it's too easy to write what you're seeing, which isn't much different legally from cutting and pasting. Use a fake human readable code if you need to take step by step notes about a complex algorithm. Do not go back to your development environment if you have questions, make a note and answer it later.
- Close your research windows. Restart your development environment. Open the resources you need from the notes that point to official sources of documentation and other resources that are "safe" to use while developing. Start writing code again.
This isn't a perfect clean room reverse engineering, you can go much more deeply down this, but with proper notes you may be better protected from possible legal issues. You will not need to perform any attribution except since you never copied anything, but you might want to keep links in your notes should you have to refer to them later.
If you properly organize your notes then they will act as excellent documentation for later maintainers as well as legal evidence of a valid clean room reverse engineering method.