├── .gitignore ├── LICENSE ├── README.md └── docs ├── .nojekyll ├── README.md ├── _coverpage.md ├── _navbar.md ├── _sidebar.md ├── assets └── images │ ├── add-blinkfox-checks-success.png │ ├── add-blinkfox-checks.png │ ├── idea-check.png │ ├── idea-checkstyle-plugin.png │ ├── import-idea-style-xml.png │ └── logo.png ├── checks ├── blinkfox-checks.xml ├── blinkfox-idea-java-style.xml └── google-checks.xml ├── favicon.ico ├── guide ├── blinkfox-java-style-guide.md ├── google-java-style-guide-cn.md └── google-java-style-guide.md ├── index.html └── styles ├── blinkfox-checks.md ├── blinkfox-idea-java-style.md └── google-checks.md /.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | .idea 3 | *.iws 4 | *.iml 5 | *.ipr 6 | 7 | ### VS Code ### 8 | .vscode/ 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 |
8 | Blinkfox Java 编程风格指南,且包含 CheckStyle 和 IDEA 格式化文件。 9 |
10 | 11 | [](http://hits.dwyl.io/blinkfox/java-style) [](https://github.com/blinkfox/java-style/blob/master/LICENSE) [](https://blinkfox.github.io/java-style) 12 | 13 | > 这是关于 Java 编程风格相关的仓库。核心来自于 Google 的 Java 编程风格指南([Google Java Style Guide](https://checkstyle.sourceforge.io/styleguides/google-java-style-20180523/javaguide.html#s3.3.3-import-ordering-and-spacing),并对该份英文指南做了翻译,并最终整理出了适合我和更多人的一份规范来。 14 | 15 | - [查看文档 https://blinkfox.github.io/java-style](https://blinkfox.github.io/java-style) 16 | 17 | ## 主要内容 18 | 19 | 本仓库核心内容如下: 20 | 21 | ```bash 22 | - docs 23 | - checks 24 | - blinkfox-checks.xml # 经过部分新增、修改和详细注释的,符合 Blinkfox 编程风格的 checkstyle 文件. 25 | - blinkfox-idea-java-style.xml # 符合 Blinkfox 编程风格的,可导入 Intellij IDEA 中的 Java code style 的格式化文件. 26 | - google-checks.xml # 未经修改的符合 Google 编程风格的原生 checkstyle 文件. 27 | - guide 28 | - blinkfox-java-style-guide.md # 基于 Google 原生的编程风格而新增、修改的,且符合 Blinkfox 编程风格的简要中文指南 29 | - google-java-style-guide-cn.md # 翻译的 Google 原生的编程风格指南的中文文档 30 | - google-java-style-guide.md # Google 原生的编程风格指南英文文档 31 | - LICENSE # 仓库协议,Apache License2.0 32 | ``` 33 | 34 | 本仓库的核心产出结果主要是《Google Java 编程风格指南.md》、《Blinkfox Java 编程风格指南》、《blinkfox-checks.xml》、《blinkfox-idea-java-style.xml》。 35 | 36 | 其中《Google Java 编程风格指南.md》和《Blinkfox Java 编程风格指南》是文档型,主要供大家阅读参考。而[《blinkfox-checks.xml》](https://github.com/blinkfox/java-style/blob/master/checks/blinkfox-checks.xml)和[《blinkfox-idea-java-style.xml》](https://github.com/blinkfox/java-style/blob/master/checks/blinkfox-idea-java-style.xml) 才是真正可实际用于工具和项目中的代码风格检查和格式化文件。 37 | 38 | ## 开源协议 39 | 40 | 本项目基于 Apache License 2.0 协议开源。 41 | -------------------------------------------------------------------------------- /docs/.nojekyll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blinkfox/java-style/cce3b132cb9c8bcb3d91b089f0cd28164dd4df1d/docs/.nojekyll -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # 💡快速使用 2 | 3 | [Blinkfox Java 编程风格指南](guide/blinkfox-java-style-guide.md)是 Blinkfox 根据 [Google Java 编程风格](https://checkstyle.sourceforge.io/styleguides/google-java-style-20180523/javaguide.html#s3.3.3-import-ordering-and-spacing)规范并根据自身使用情况而修改出的一份 Java 编程风格指南。相对于 Google 的 Java 编程风格文档而言,修改了部分规范,增加了一些更加确定性的规则说明,这样能最大程度上的让团队的代码风格统一、易读。 4 | 5 | 本文档仓库中[《blinkfox-checks.xml》](styles/blinkfox-checks.md)和[《blinkfox-idea-java-style.xml》](styles/blinkfox-idea-java-style.md) 是真正可实际用于项目和工具中的代码风格检查和格式化文件。 6 | 7 | 下面将介绍如何在项目或 Intellij IDEA 中集成使用相关的配置检查文件格式化文件等。 8 | 9 | ## 1 Maven 中集成 blinkfox-checks.xml :id=maven-blinkfox-checks 10 | 11 | 以下以 Maven 项目为例,介绍如何在 Java Maven 项目中集成使用 `blinkfox-checks.xml` 的 checkstyle 文件,你也可以查看此文件的内容,其中有详细的中文注释,你也可以对部分内容进行新增或修改,直到符合你自己或团队的代码风格要求。 12 | 13 | ### 1.1 下载 blinkfox-checks.xml 14 | 15 | 首先,下载复制 [blinkfox-checks.xml](#) 文件到你的项目中,建议你将该文件放在与 `pom.xml` 文件**同级**的目录的中,当然你也可以放其他目录中,只不过需要额外在 Maven checkstyle 插件中配置目录位置。 16 | 17 | ### 1.2 集成 maven-checkstyle-plugin 18 | 19 | 然后,在你项目的 `pom.xml` 文件的 `build` -> `plugins` 层级下,添加 `maven-checkstyle-plugin` 插件,复制如下即可: 20 | 21 | ```xml 22 |注意:你不需要去管代码逻辑,着重看代码块、缩进、每一个空行以及空格等的使用.
176 | * 177 | * @author intelij idea on 2019-09-25. 178 | * @author blinkfox on 2020-03-18. 179 | * @since v1.2.0 180 | */ 181 | @Slf4j 182 | @Annotation(param1 = "value1", param2 = "value2") 183 | @SuppressWarnings( {"ALL"}) 184 | public class Foo这是详细描述示例,这是行内注释的代码 {@code int} 的使用. 436 | * 这是注释内链接 {@link java.util.concurrent.atomic} 的使用示例.
437 | * 438 | * 这是一个 HTML 超链接的使用示例. 439 | * 440 | * @param p1 参数说明 441 | * @return 返回值说明 442 | * @see java.util.concurrent.atomic.AtomicInteger 443 | * @since v1.2.0 444 | * @author blinkfox add on 2020-03-20. 445 | * @author zhangshan modify on 2020-04-05. 446 | */ 447 | public int method(String p1) { ... } 448 | ``` 449 | 450 | 或者是以下单行形式: 451 | 452 | ```java 453 | /** 一小段的 Javadoc. */ 454 | ``` 455 | 456 | - 每个 Javadoc 都以一个简短的**摘要片段**开始,用于对所注释的类、属性、方法等进行简要的描述。并以**英文句号**(`.`)结尾。因为 Java 在生成 Javadoc 时,会以注释中第一个英文句号作为拆分点,解析出摘要片段和详细描述信息。 457 | - 空行是只包含最左侧星号(`*`)的空行会出现在段落之间和 Javadoc 注解标记(`@XXX`)之前(如果有的话)。除了第一个段落,每个段落第一个单词前都有标签``,并且它和第一个单词间没有空格。 458 | - Javadoc 中几个重要的注解标记: 459 | - 对于具有泛型参数的类或方法,对于具有参数的方法,Javadoc 中都必须加上 `@param` 注解标记和对应的注释; 460 | - 对于具有返回值的方法,Javadoc 中都必须加上 `@return` 注解标记和对应的注释; 461 | - 如果方法签名中有抛出异常,则必须有 `@throws` 注解标记和对应的注释; 462 | - 对于要弃用的类或方法,则使用 `@deprecated` 注解标记,并解释为什么弃用的原因。 463 | - 如果你的类、接口、方法等需要参考或关联其他类或接口,可以使用 `@see` 标签,在 Javadoc 注释内容中,则可以通过 `@link` 去链接,但注意,`@link` 只能链接该类或方法可访问类、属性或方法。 464 | - 当是第一次新增类或方法时。需要再新增的类或方法上额外加上作者、日期和新增该代码时的版本的注解,即要加上 `@author`、`@since` 注解,其中 `@author` 可以写多个,由于 Javadoc 中没有日期相关的 Javadoc 标签,建议将日期写在 `@author`的作者之后,格式形如:`@author blinkfox add on 2020-03-20.`。**注**:如果写上 Javadoc 不支持的 `@date` 标签,在生成 Javadoc 时会报错,且 `@date` 所表示的日期与具体的作者难以对应上。 465 | - 标准的 Javadoc 注解标记按以下顺序出现:`@param`, `@return`, `@throws`, `@deprecated`、`@author`, `@see`, `@since`, 这几种标记如果出现,描述都不能为空。当描述无法在一行中容纳,连续行需要至少在 `@` 符号的位置处缩进 `4` 个空格。 466 | - 行内的注释中如果包含“代码”,则可以使用 `@code` 包裹起来. 467 | 468 | ### 7.2 什么时候需要写 Javadoc 469 | 470 | - 在每个 `public` 类及它的每个 `public` 和 `protected` 成员处使用 Javadoc。 471 | - 对于**具有业务含义**的属性字段建议写上 Javadoc。 472 | - 对于包外不可见的类和方法,如有需要,也是要使用 Javadoc 的。 473 | - 如果一个注释是用来定义一个类,方法,字段的整体目的或行为,即时是 `private` 的,那么这个注释应该写成 Javadoc,这样更统一、更友好。 474 | 475 | ### 7.3 可以不需要 Javadoc 的例外情况 476 | 477 | - 对于 `getter` 和 `setter` 方法,Javadoc 是可选的,但建议在其字段属性上写明 Javadoc 注释,比如:`canonicalName` 属性,如果不加注释的话,可能后续的维护者难以理解其真正含义。 478 | - 如果一个方法重写了超类中的方法,那么 Javadoc 也是可选的。 479 | 480 | ### 7.4 JDK 源码中的 Javadoc 示例 481 | 482 | 下面是 JDK 源码中 `Callable` 接口的 Javadoc 示例,大家也可以参考借鉴下: 483 | 484 | ```java 485 | /** 486 | * The {@code String} class represents character strings. All 487 | * string literals in Java programs, such as {@code "abc"}, are 488 | * implemented as instances of this class. 489 | *
490 | * Strings are constant; their values cannot be changed after they 491 | * are created. String buffers support mutable strings. 492 | * Because String objects are immutable they can be shared. For example: 493 | *
494 | * String str = "abc"; 495 | *
496 | * is equivalent to: 497 | *
498 | * char data[] = {'a', 'b', 'c'}; 499 | * String str = new String(data); 500 | *
501 | * Here are some more examples of how strings can be used: 502 | *
509 | *503 | * System.out.println("abc"); 504 | * String cde = "cde"; 505 | * System.out.println("abc" + cde); 506 | * String c = "abc".substring(2,3); 507 | * String d = cde.substring(1, 2); 508 | *
510 | * The class {@code String} includes methods for examining 511 | * individual characters of the sequence, for comparing strings, for 512 | * searching strings, for extracting substrings, and for creating a 513 | * copy of a string with all characters translated to uppercase or to 514 | * lowercase. Case mapping is based on the Unicode Standard version 515 | * specified by the {@link java.lang.Character Character} class. 516 | *
517 | * The Java language provides special support for the string 518 | * concatenation operator ( + ), and for conversion of 519 | * other objects to strings. String concatenation is implemented 520 | * through the {@code StringBuilder}(or {@code StringBuffer}) 521 | * class and its {@code append} method. 522 | * String conversions are implemented through the method 523 | * {@code toString}, defined by {@code Object} and 524 | * inherited by all classes in Java. For additional information on 525 | * string concatenation and conversion, see Gosling, Joy, and Steele, 526 | * The Java Language Specification. 527 | * 528 | *
Unless otherwise noted, passing a null argument to a constructor 529 | * or method in this class will cause a {@link NullPointerException} to be 530 | * thrown. 531 | * 532 | *
A {@code String} represents a string in the UTF-16 format 533 | * in which supplementary characters are represented by surrogate 534 | * pairs (see the section Unicode 535 | * Character Representations in the {@code Character} class for 536 | * more information). 537 | * Index values refer to {@code char} code units, so a supplementary 538 | * character uses two positions in a {@code String}. 539 | *
The {@code String} class provides methods for dealing with
540 | * Unicode code points (i.e., characters), in addition to those for
541 | * dealing with Unicode code units (i.e., {@code char} values).
542 | *
543 | * @author Lee Boynton
544 | * @author Arthur van Hoff
545 | * @author Martin Buchholz
546 | * @author Ulf Zibis
547 | * @see java.lang.Object#toString()
548 | * @see java.lang.StringBuffer
549 | * @see java.lang.StringBuilder
550 | * @see java.nio.charset.Charset
551 | * @since JDK1.0
552 | */
553 | public final class String
554 | implements java.io.Serializable, Comparable `,并且它和第一个单词间没有空格。
671 |
672 | #### 7.1.3 Javadoc 标记
673 |
674 | 标准的 Javadoc 注解标记按以下顺序出现:`@param`, `@return`, `@throws`, `@deprecated`, 前面这 4 种标记如果出现,描述都不能为空。当描述无法在一行中容纳,连续行需要至少在 `@` 符号的位置处缩进 `4` 个空格。
675 |
676 | #### 7.2 摘要片段
677 |
678 | 每个 Javadoc 都以一个简短的**摘要片段**开始。这个片段是非常重要的,它是文本在某些上下文(如类和方法索引)中出现的唯一部分。
679 |
680 | 这只是一个小片段,可以是一个名词短语或动词短语,但不是一个完整的句子。它不会以 `A {@code Foo} is a...` 或者 `This method returns...` 开头, 它也不会是一个完整的祈使句,如 `Save the record.`。但是,由于开头大写且被加了标点符号,它看起来就像是个完整的句子。
681 |
682 | > **提示**:一个常见的错误是把简单的 Javadoc 写成 `/** @return the customer ID */`,这是不正确的。它应该写成 `/** Returns the customer ID. */`。
683 |
684 | ### 7.3 在哪里使用 Javadoc
685 |
686 | 至少在每个 `public` 类及它的每个 `public` 和 `protected` 成员处使用 Javadoc,下面会提到一些例外情况:
687 |
688 | 额外的 Javadoc 内容也可能存在,如第 7.3.4 节[非必需的Javadoc](#)。
689 |
690 | #### 7.3.1 例外:不言自明的方法
691 |
692 | 对于简单明显的方法如 `getFoo`,Javadoc 是可选的。这种情况下除了写 “`Returns the foo`”,确实也没有什么值得写了。
693 |
694 | > **重要提示**:如果有一些相关信息是需要读者了解的,那么以上的例外不应作为忽视这些信息的理由。例如,对于 `getCanonicalName` 方法,就不应该忽视文档说明,因为读者很可能不知道词语 “`canonical name`” 指的是什么。
695 |
696 | #### 7.3.2 例外:重写
697 |
698 | 如果一个方法重写了超类中的方法,那么 Javadoc 并非必需的。
699 |
700 | #### 7.3.3 可选的 Javadoc
701 |
702 | 对于包外不可见的类和方法,如有需要,也是要使用 Javadoc 的。如果一个注释是用来定义一个类,方法,字段的整体目的或行为,那么这个注释应该写成 Javadoc,这样更统一、更友好。
703 |
704 | #### 7.3.4 不需要 Javadoc 的情况
705 |
706 | 其他类和成员种根据实际是否需要来写 Javadoc。
707 |
708 | 每当将注释用于定义类或成员的总体目的或行为时,该注释就会改为使用 Javadoc 来编写(使用`/**`)。
709 |
710 | 非必需的 Javadoc 并不要求严格遵循第 7.1.2、7.1.3 和 7.2 节的格式化规则,当然是推荐遵循的。
711 |
--------------------------------------------------------------------------------
/docs/guide/google-java-style-guide.md:
--------------------------------------------------------------------------------
1 | # Google Java Style Guide
2 |
3 | ## 1 Introduction
4 |
5 | This document serves as the **complete** definition of Google's coding standards for source code in the Java™ Programming Language. A Java source file is described as being in Google Style if and only if it adheres to the rules herein.
6 |
7 | Like other programming style guides, the issues covered span not only aesthetic issues of formatting, but other types of conventions or coding standards as well. However, this document focuses primarily on the **hard-and-fast** rules that we follow universally, and avoids giving advice that isn't clearly enforceable (whether by human or tool).
8 |
9 | ### 1.1 Terminology notes
10 |
11 | In this document, unless otherwise clarified:
12 |
13 | 1. The term `class` is used inclusively to mean an "ordinary" class, enum class, interface or annotation type (`@interface`).
14 | 2. The term `member` (of a class) is used inclusively to mean a nested class, field, method, or constructor; that is, all top-level contents of a class except initializers and comments.
15 | 3. The term `comment` always refers to implementation comments. We do not use the phrase "documentation comments", instead using the common term "Javadoc."
16 |
17 | Other "terminology notes" will appear occasionally throughout the document.
18 |
19 | ### 1.2 Guide notes
20 |
21 | Example code in this document is **non-normative**. That is, while the examples are in Google Style, they may not illustrate the only stylish way to represent the code. Optional formatting choices made in examples should not be enforced as rules.
22 |
23 | ## 2 Source file basics
24 |
25 | ### 2.1 File name
26 |
27 | The source file name consists of the case-sensitive name of the top-level class it contains (of which there is [exactly one](https://checkstyle.sourceforge.io/styleguides/google-java-style-20180523/javaguide.html#s3.4.1-one-top-level-class)), plus the `.java` extension.
28 |
29 | ### 2.2 File encoding: UTF-8
30 |
31 | Source files are encoded in **UTF-8**.
32 |
33 | ### 2.3 Special characters
34 |
35 | #### 2.3.1 Whitespace characters
36 |
37 | Aside from the line terminator sequence, the **ASCII horizontal space character (`0x20`)** is the only whitespace character that appears anywhere in a source file. This implies that:
38 |
39 | 1. All other whitespace characters in string and character literals are escaped.
40 | 2. Tab characters are **not** used for indentation.
41 |
42 | #### 2.3.2 Special escape sequences
43 |
44 | For any character that has a [special escape sequence](http://docs.oracle.com/javase/tutorial/java/data/characters.html) (`\b`, `\t`, `\n`, `\f`, `\r`, `\"`, `\'` and `\\`), that sequence is used rather than the corresponding octal (e.g. `\012`) or Unicode (e.g. `\u000a`) escape.
45 |
46 | #### 2.3.3 Non-ASCII characters
47 |
48 | For the remaining non-ASCII characters, either the actual Unicode character (e.g. `∞`) or the equivalent Unicode escape (e.g. `\u221e`) is used. The choice depends only on which makes the code **easier to read and understand**, although Unicode escapes outside string literals and comments are strongly discouraged.
49 |
50 | > **Tip**: In the Unicode escape case, and occasionally even when actual Unicode characters are used, an explanatory comment can be very helpful.
51 |
52 | Examples:
53 |
54 | | Example | Discussion |
55 | | ------------------------------------------------------ | ------------------------------------------------------------ |
56 | | String unitAbbrev = "μs"; | Best: perfectly clear even without a comment. |
57 | | String unitAbbrev = "\u03bcs"; // "μs" | Allowed, but there's no reason to do this. |
58 | | String unitAbbrev = "\u03bcs"; // Greek letter mu, "s" | Allowed, but awkward and prone to mistakes. |
59 | | String unitAbbrev = "\u03bcs"; | Poor: the reader has no idea what this is. |
60 | | return '\ufeff' + content; // byte order mark | Good: use escapes for non-printable characters, and comment if necessary. |
61 |
62 | > **Tip**: Never make your code less readable simply out of fear that some programs might not handle non-ASCII characters properly. If that should happen, those programs are **broken** and they must be **fixed**.
63 |
64 | ## 3 Source file structure
65 |
66 | A source file consists of, **in order**:
67 |
68 | 1. License or copyright information, if present
69 | 2. Package statement
70 | 3. Import statements
71 | 4. Exactly one top-level class
72 |
73 | **Exactly one blank line** separates each section that is present.
74 |
75 | ### 3.1 License or copyright information, if present
76 |
77 | If license or copyright information belongs in a file, it belongs here.
78 |
79 | ### 3.2 Package statement
80 |
81 | The package statement is **not line-wrapped**. The column limit (Section 4.4, [Column limit: 100](#)) does not apply to package statements.
82 |
83 | ### 3.3 Import statements
84 |
85 | #### 3.3.1 No wildcard imports
86 |
87 | **Wildcard imports**, static or otherwise, **are not used**.
88 |
89 | #### 3.3.2 No line-wrapping
90 |
91 | Import statements are **not line-wrapped**. The column limit (Section 4.4, [Column limit: 100](#)) does not apply to import statements.
92 |
93 | #### 3.3.3 Ordering and spacing
94 |
95 | Imports are ordered as follows:
96 |
97 | 1. All static imports in a single block.
98 | 2. All non-static imports in a single block.
99 |
100 | If there are both static and non-static imports, a single blank line separates the two blocks. There are no other blank lines between import statements.
101 |
102 | Within each block the imported names appear in ASCII sort order. (**Note**: this is not the same as the import statements being in ASCII sort order, since '.' sorts before ';'.)
103 |
104 | #### 3.3.4 No static import for classes
105 |
106 | Static import is not used for static nested classes. They are imported with normal imports.
107 |
108 | ### 3.4 Class declaration
109 |
110 | #### 3.4.1 Exactly one top-level class declaration
111 |
112 | Each top-level class resides in a source file of its own.
113 |
114 | #### 3.4.2 Ordering of class contents
115 |
116 | The order you choose for the members and initializers of your class can have a great effect on learnability. However, there's no single correct recipe for how to do it; different classes may order their contents in different ways.
117 |
118 | What is important is that each class uses **some logical order**, which its maintainer could explain if asked. For example, new methods are not just habitually added to the end of the class, as that would yield "chronological by date added" ordering, which is not a logical ordering.
119 |
120 | ##### 3.4.2.1 Overloads: never split
121 |
122 | When a class has multiple constructors, or multiple methods with the same name, these appear sequentially, with no other code in between (not even private members).
123 |
124 | ## 4 Formatting
125 |
126 | **Terminology Note**: block-like construct refers to the body of a class, method or constructor. Note that, by Section 4.8.3.1 on array initializers, any array initializer may optionally be treated as if it were a block-like construct.
127 |
128 | ### 4.1 Braces
129 |
130 | #### 4.1.1 Braces are used where optional
131 |
132 | Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement.
133 |
134 | #### 4.1.2 Nonempty blocks: K & R style
135 |
136 | Braces follow the Kernighan and Ritchie style (["Egyptian brackets"](http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html)) for nonempty blocks and block-like constructs:
137 |
138 | - No line break before the opening brace.
139 | - Line break after the opening brace.
140 | - Line break before the closing brace.
141 | - Line break after the closing brace, only if that brace terminates a statement or terminates the body of a method, constructor, or named class. For example, there is no line break after the brace if it is followed by `else` or a comma.
142 |
143 | Examples:
144 |
145 | ```java
146 | return () -> {
147 | while (condition()) {
148 | method();
149 | }
150 | };
151 |
152 | return new MyClass() {
153 | @Override public void method() {
154 | if (condition()) {
155 | try {
156 | something();
157 | } catch (ProblemException e) {
158 | recover();
159 | }
160 | } else if (otherCondition()) {
161 | somethingElse();
162 | } else {
163 | lastThing();
164 | }
165 | }
166 | };
167 | ```
168 |
169 | A few exceptions for enum classes are given in Section 4.8.1, [Enum classes](https://checkstyle.sourceforge.io/styleguides/google-java-style-20180523/javaguide.html#s4.8.1-enum-classes).
170 |
171 | #### 4.1.3 Empty blocks: may be concise
172 |
173 | An empty block or block-like construct may be in K & R style (as described in Section 4.1.2). Alternatively, it may be closed immediately after it is opened, with no characters or line break in between ({}), unless it is part of a multi-block statement (one that directly contains multiple blocks: if/else or try/catch/finally).
174 |
175 | Examples:
176 |
177 | ```java
178 | // This is acceptable
179 | void doNothing() {}
180 |
181 | // This is equally acceptable
182 | void doNothingElse() {
183 | }
184 | ```
185 |
186 | ```java
187 | // This is not acceptable: No concise empty blocks in a multi-block statement
188 | try {
189 | doSomething();
190 | } catch (Exception e) {}
191 | ```
192 |
193 | ### 4.2 Block indentation: +2 spaces
194 |
195 | Each time a new block or block-like construct is opened, the indent increases by two spaces. When the block ends, the indent returns to the previous indent level. The indent level applies to both code and comments throughout the block. (See the example in Section 4.1.2, [Nonempty blocks: K & R Style](#).)
196 |
197 | ### 4.3 One statement per line
198 |
199 | Each statement is followed by a line break.
200 |
201 | ### 4.4 Column limit: 100
202 |
203 | Java code has a column limit of 100 characters. A "character" means any Unicode code point. Except as noted below, any line that would exceed this limit must be line-wrapped, as explained in Section 4.5, [Line-wrapping](#).
204 |
205 | > Each Unicode code point counts as one character, even if its display width is greater or less. For example, if using [fullwidth characters](https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms), you may choose to wrap the line earlier than where this rule strictly requires.
206 |
207 | Exceptions:
208 |
209 | 1. Lines where obeying the column limit is not possible (for example, a long URL in Javadoc, or a long JSNI method reference).
210 | 2. `package` and `import` statements (see Sections 3.2 [Package statement](#) and 3.3 [Import statements](#)).
211 | 3. Command lines in a comment that may be cut-and-pasted into a shell.
212 |
213 | ### 4.5 Line-wrapping
214 |
215 | **Terminology Note**: When code that might otherwise legally occupy a single line is divided into multiple lines, this activity is called line-wrapping.
216 |
217 | There is no comprehensive, deterministic formula showing exactly how to line-wrap in every situation. Very often there are several valid ways to line-wrap the same piece of code.
218 |
219 | > **Note**: While the typical reason for line-wrapping is to avoid overflowing the column limit, even code that would in fact fit within the column limit may be line-wrapped at the author's discretion.
220 |
221 | > **Tip**: Extracting a method or local variable may solve the problem without the need to line-wrap.
222 |
223 | #### 4.5.1 Where to break
224 |
225 | The prime directive of line-wrapping is: prefer to break at a **higher syntactic level**. Also:
226 |
227 | 1. When a line is broken at a non-assignment operator the break comes before the symbol. (Note that this is not the same practice used in Google style for other languages, such as C++ and JavaScript.)
228 | - This also applies to the following "operator-like" symbols:
229 | - the dot separator (`.`)
230 | - the two colons of a method reference (`::`)
231 | - an ampersand in a type bound (` ` immediately before the first word, with no space after.
665 |
666 | #### 7.1.3 Block tags
667 |
668 | Any of the standard "block tags" that are used appear in the order `@param`, `@return`, `@throws`, `@deprecated`, and these four types never appear with an empty description. When a block tag doesn't fit on a single line, continuation lines are indented `four` (or more) spaces from the position of the `@`.
669 |
670 | ### 7.2 The summary fragment
671 |
672 | Each Javadoc block begins with a brief **summary fragment**. This fragment is very important: it is the only part of the text that appears in certain contexts such as class and method indexes.
673 |
674 | This is a fragment—a noun phrase or verb phrase, not a complete sentence. It does not begin with `A {@code Foo} is a...`, or `This method returns...`, nor does it form a complete imperative sentence like `Save the record.`. However, the fragment is capitalized and punctuated as if it were a complete sentence.
675 |
676 | Tip: A common mistake is to write simple Javadoc in the form `/** @return the customer ID */`. This is incorrect, and should be changed to `/** Returns the customer ID. */`.
677 |
678 | ### 7.3 Where Javadoc is used
679 |
680 | At the minimum, Javadoc is present for every `public` class, and every `public` or `protected` member of such a class, with a few exceptions noted below.
681 |
682 | Additional Javadoc content may also be present, as explained in Section 7.3.4, [Non-required Javadoc](#).
683 |
684 | #### 7.3.1 Exception: self-explanatory methods
685 |
686 | Javadoc is optional for "simple, obvious" methods like getFoo, in cases where there really and truly is nothing else worthwhile to say but "`Returns the foo`".
687 |
688 | Important: it is not appropriate to cite this exception to justify omitting relevant information that a typical reader might need to know. For example, for a method named `getCanonicalName`, don't omit its documentation (with the rationale that it would say only `/** Returns the canonical name. */`) if a typical reader may have no idea what the term "canonical name" means!
689 |
690 | #### 7.3.2 Exception: overrides
691 |
692 | Javadoc is not always present on a method that overrides a supertype method.
693 |
694 | #### 7.3.4 Non-required Javadoc
695 |
696 | Other classes and members have Javadoc as needed or desired.
697 |
698 | Whenever an implementation comment would be used to define the overall purpose or behavior of a class or member, that comment is written as Javadoc instead (using `/**`).
699 |
700 | Non-required Javadoc is not strictly required to follow the formatting rules of Sections 7.1.2, 7.1.3, and 7.2, though it is of course recommended.
701 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |