Introduction
Inspired by this cover. The book is written in a deadpan manner.
根据封面的启发,这本书是在面无表情的方式下编写
cutting corners to meet arbitrary management deadlines. 投机取巧 来满足任意管理期限
Code Licensing Issues[编码许可问题]
Before you can copy and paste code, you have to make sure you have the legal right[合法权利] to copy and paste that code. Without that legal right, you would just be stealing[偷盗、抄袭] other people's intellectual property[知识产权]. That would just be lazy.
Before you can copy and paste code, you have to make sure you have the legal right[合法权利] to copy and paste that code. Without that legal right, you would just be stealing[偷盗、抄袭] other people's intellectual property[知识产权]. That would just be lazy.
Most code on Stack Overflow are 'snippets'[片断]. They do not meet standards of originality[标准的创意] and as a result have no copyright protection[版权保护]. You are free to copy and paste them without any worry about losing a lawsuit[诉讼].
Some code on Stack Overflow are much longer than 'snippets'. They therefore qualify for copyright protection[因此他们有资格获得版权保护]. By default[默认情况下], all content on Stack Overflow (including code) is licensed under the CC-BY-SA 3.0 license[署名-相同方式共享creative commons说明见:https://creativecommons.org/licenses/by-sa/3.0/deed.zh].
This license[CC-BY-SA 3.0 license] is not recommended[推荐] for use in software at all, and so is currently looking to replace this license for future code posted on its website[所以目前在其网站上发布的未来代码是用于取代此授权]. For now, though, the CC-BY-SA 3.0 is law. Even if so does change its future licensing policies, all previous[以前的]code will still remain[保持,归于] under CC-BY-SA 3.0.
CC-BY-SA 3.0 is a copyleft license just like[正如] GPL 3.0. Essentially[本质上], you can use all code licensed under CC-BY-SA 3.0 so long as[只要]:
● You give attribution[说明来源] to the website where you got the code.
● You license your codebase under the same terms[条款] as the CC-BY-SA 3.0. Again[此外], the CC-BY-SA 3.0 is not recommended for use in software, so licensing your software under CC-BY-SA 3.0 is pretty much[几乎] a non-starter[不值的考虑的方案].
However, there is some legal wizardy[法律向导] you can do to relicense[再次授权] your Stack Overflow code from ugly CC-BY-SA 3.0 to a proper Open Source license[适当
的开放源码许可] that is suitable for[适用于] software. Here how it works.
1. Any creative work licensed under CC-BY-SA 3.0 can be upgraded to[提高至] CC-BY-SA 4.0, automatically.
2. According to the Creative Commons website, CC-BY-SA 4.0 is compatible[兼容] with both GPL 3.0 and the Artistic License. Both the GPL 3.0 and the Artistic License are copy-left licenses[反版权许可].
There is no real way to distinguish[区分] between a 'code snippet' with no copyright protection and a much larger block of code that does contain copyright protection (and thus is licensed under CC-BY-SA 3.0). You could use common sense[常识] to decide whether some code qualifies as[作为] a "snippet" or not, but if the courts[法院] share a different definition of a "snippet" than you, you are out of luck.
If you are working on a permissive or proprietary codebase[许可或专有代码库], you have four options[选择].
● Change the codebase to GPL 3.0 or the Artistic License and start copying and pasting code with ease[熟练].
● Hire an expensive lawyer to defend your right to use code snippets from Stack Overflow...
● Hope that the person you're copying and pasting from doesn't care enough about your violation[违反] of the CC-BY-SA 3.0 license to sue[起诉] you.
○ I recommend this approach[推荐这种方式] because the odds[机会] of the other user stumbling[发现] upon your codebase and complaining[投诉] about you not adhering to[坚持] the terms of the CC-BY-SA 3.0 is lower than the odds of this book actually being published by O'Reilly. If you are very unlucky (and that can happen), then you can simply apologize and then rewrite the offending[不愉快] code.
● ...or write your own pseudo-Clean Room implementation of the Stack Overflow code.
Here is a step-by-step guide:
1. Write down some notes about the code from Stack Overflow, specifically how it works, how you should implement it, etc.
2. Close the web browser.
3. Wait a few minutes so that you forget the code.
4. Use the information in your notes to re-implement the Stack Overflow code in question.
5. You now own the copyright to your code in question...even if the final code is exactly the same as the original Stack Overflow answer. Since you now own the copyright to this code, you may integrate[成为一体] it into your permissive or proprietary codebase.
This approach is slower than the traditional "CTRL-C, CTRL-V" approach to copying and pasting. However, it does have the benefit[益处] of giving you a deeper understanding of the Stack Overflow code. This could be helpful as you will likely[很可能、或许] need to maintain this code in the future.
There is one last point to conclude[最后] in this chapter. Some users may add a permissive license to their code submissions to[提交] Stack Overflow. In that case, you can elect to[选择] adhere to[坚持] the terms of this permissive license instead of worrying about the copyleft CC-BY-SA 3.0. Check the profile[简况] of the user who posted the "code" to see whether the code is available[可供使用] under a different license.
Code Attribution[编码归属]
When you copy and paste code from Stack Overflow, it is best practice to provide a link to the place where you got that code. This is for two reasons:
1. The person has helped you greatly by giving you some valuable[有价值的] code. You should reward[奖励] them by giving the programmer a comment[意见] in a codebase that they are never ever going to see.
2. It's useful to look back[回顾] at the answer in case you need to debug the code later. This is very useful if the author of the code in question also wrote a detailed explanation of his code.
Sometimes, during the course of[在...期间] a project, the original[最初的] so code will be heavily modified[大量的修改] by you. In that case, it makes no sense[豪无意义] to keep the attribution, since you have thoroughly mutilated[彻底破坏] the work of the original author. There is no reason to praise[赞赏] the author anymore, and the author's detailed explanations may prove[证明是] useless thanks to[由于] your own modifications. Remove the comment from your code ASAP.
Use your professional judgment when deciding[决定] when to give attribution and when you should remove it.
Note while that attribution is generally not required for 'code snippets', they are usually required if you are complying with[遵守] the terms of a license (either the default CC-BY-SA 3.0 or a permissive license).
Code Selection[选码]
Generally, when you search for a question on Stack Overflow, you can usually find a answer to your problem. This is not always a good thing.
● Some of the answers on Stack Overflow may be plain wrong. Anyone can answer a question on Stack Overflow, so just because[只是因为] it is online does not mean it is true. While obviously wrong answers can be downvoted, not so obvious answers may be highly voted[投票]. In addition, you usually cannot be certain at all[完全确信] that the code in question actually compiles or work until you try it out yourself.
● Some of the answers may devoid[缺乏] of explanation and be composed of[由...组成] nothing more than[仅仅,只不过] code. While this may seem like a good idea for the eager[热切地] copy-and-paster, it really isn't. You need to know what the code is actually doing. After all[毕竟], you will have to maintain that code in the future.
● Some of the answers are legacy[遗留] answers. They worked in the past, but are now no longer best practice. Stack Overflow suffers from the "fastest gun in the west" synd more[综合比较] where whoever answers the question first usually gets the most votes, and thus is seen first. The more "modern" answers usually don't get as much votes and tend to[趋向于] languish[变衰弱] on the bottom of the page.
Appendix: Ready-Made Code[附录:现成的代码]
Stack Overflow is structured as a Q&A website. Users ask questions, and people respond with answers. The website officially[正式地] discourage[阻止] 'discussions', where people talk about the proper way of solving a problem endlessly[无休止地]. Instead, you should get your answer quickly to a real problem.
Therefore, on Stack Overflow, it is hard to stumble upon[偶尔发现] ready-made programs ready for you to copy and paste blindly[轻率地]. On Stack Overflow, code is designed to[被设计用于] solve unique and specialized problems, not just vomit out[呕出] a complete, working program.
This is not the case if you go to other Q&A websites located on[处于] the broader Stack Exchange platform (which is where 'Stack Overflow' is hosted at). Q&A sites such as "Code Review" or "Code Golf" serve as[担任...,充当...] repositories[资源库] of "ready-made code".
● "Code Review" is a website where coders review other people's working programs and make suggestions on how to improve them. (Example: Working Tic-Tac-Toe Game using Python, JavaScript, PHP and HTML)
● "Code Golf" is a website where users accept challenges to produce working programs[工作程序] that meets certain criteria[符合某些标准] (e.g., size). (Example: Na?ve Markov Chain Word Generation)
All code located on "Code Review" or "Code Golf" is licensed under the CC-BY-SA 3.0 (just like the code on "Stack Overflow"). Most of them are not code snippets...after all, they are usually fully-fledged[成熟的] working programs, not one or two lines of code.
Translating the code in question into another language would also count as[看作] a derivative work[演绎作品](after all, you were inspired by[受启发] somebody's code to write your own version of it), so you would still have to comply with[遵守] the terms of the CC-BY-SA 3.0 license.
If you are running a copyleft codebase, there's no legal problem[法律问题] involved in[涉及进...] using code on "Code Review" or "Code Golf". If you are working on a proprietary or permissive codebase though, then you may have consider[考虑] performing[覆行] the pseudo-"Clean Room" implementation that I discussed back in the Code Licensing chapter.
It is not a good idea to use Code Review and Code Golf to find "ready-made code". "Ready-made code" already exists on code hosting websites such as SourceForge, BitBucket and GitHub, in the form of open-source software with proper software licenses. It is easier to find "ready-made code" on those sites that matches your own unique needs.
Using code from Code Review and Code Golf may also be rather unethical[是很不道德的], since you are just taking advantage of[趁着,利用] the fact that everything is automatically licensed on Stack Exchange under CC-BY-SA 3.0 with no exception[无一例外; 毫无例外]. The people posting their FizzBuzz code on "Code Review" want to have their own code reviewed, not to have it be reused[重用] by you.
However, you can copy and paste code from Code Review and Code Golf legally[合法地]. The answers and comments on the provided code in question can be incredibly helpful as well[也是非常有帮助的] and could theoretically improve[理论上可以提高] your own coding ability.