注释

只对存在一定业务逻辑复杂性的代码进行注释

注释并不是必须的,好的代码是能够让人一目了然,不用过多无谓的注释。

反例:

  1. function hashIt(data) {
  2. // The hash
  3. var hash = 0;
  4. // Length of string
  5. var length = data.length;
  6. // Loop through every character in data
  7. for (var i = 0; i < length; i++) {
  8. // Get character code.
  9. var char = data.charCodeAt(i);
  10. // Make the hash
  11. hash = ((hash << 5) - hash) + char;
  12. // Convert to 32-bit integer
  13. hash = hash & hash;
  14. }
  15. }

正例:

  1. function hashIt(data) {
  2. var hash = 0;
  3. var length = data.length;
  4. for (var i = 0; i < length; i++) {
  5. var char = data.charCodeAt(i);
  6. hash = ((hash << 5) - hash) + char;
  7. // Convert to 32-bit integer
  8. hash = hash & hash;
  9. }
  10. }

不要在代码库中遗留被注释掉的代码

版本控制的存在是有原因的。让旧代码存在于你的 history 里吧。

反例:

  1. doStuff();
  2. // doOtherStuff();
  3. // doSomeMoreStuff();
  4. // doSoMuchStuff();

正例:

  1. doStuff();

不需要版本更新类型注释

记住,我们可以使用版本控制。废代码、被注释的代码及用注释记录代码中的版本更新说明都是没有必要的。

需要时可以使用 git log 获取历史版本。

反例:

  1. /**
  2. * 2016-12-20: Removed monads, didn't understand them (RM)
  3. * 2016-10-01: Improved using special monads (JP)
  4. * 2016-02-03: Removed type-checking (LI)
  5. * 2015-03-14: Added combine with type-checking (JR)
  6. */
  7. function combine(a, b) {
  8. return a + b;
  9. }

正例:

  1. function combine(a, b) {
  2. return a + b;
  3. }

避免位置标记

这些东西通常只能代码麻烦,采用适当的缩进就可以了。

反例:

  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Scope Model Instantiation
  3. ////////////////////////////////////////////////////////////////////////////////
  4. let $scope.model = {
  5. menu: 'foo',
  6. nav: 'bar'
  7. };
  8. ////////////////////////////////////////////////////////////////////////////////
  9. // Action setup
  10. ////////////////////////////////////////////////////////////////////////////////
  11. let actions = function() {
  12. // ...
  13. }

正例:

  1. let $scope.model = {
  2. menu: 'foo',
  3. nav: 'bar'
  4. };
  5. let actions = function() {
  6. // ...
  7. }

避免在源文件中写入法律评论

将你的 LICENSE 文件置于源码目录树的根目录。

反例:

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2016 Ryan McDermott
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE
  19. */
  20. function calculateBill() {
  21. // ...
  22. }

正例:

  1. function calculateBill() {
  2. // ...
  3. }