Java代码之美,从遵循样式规范开始
在软件开发的世界里,代码不仅是程序的基石,更是程序员交流的通用语言。而Java,作为一门广泛应用于企业级应用的编程语言,其代码的可读性和一致性对于项目的长期维护和团队协作至关重要。本文将带你探索Java代码的美学,揭示那些能够让你的代码既美观又高效的样式规范。无论是初出茅庐的新手还是经验丰富的老手,都能从中获得灵感,提升编码的艺术。
对于团队开发,不同的代码规范或不规范可能引起的问题包括但不限于:
1.代码可读性,代码是为了人阅读的,不是只为了机器执行;
2.代码format引发的大量的diff,干扰code review;
探索Java代码规范
我们将深入探讨Java社区广泛认可的编码规范,包括但不限于Google Java Style和Oracle官方的编码指南。从命名约定、代码格式化、注释的使用,到错误处理和测试代码的编写,每一个细节都是构建高质量Java代码的关键。
2.1 业界Java代码样式规范
Style Guide | |
https://github.com/google/styleguide •javaguide.html 是Google对Java编程语言代码编码标准的完整定义; •intellij-java-google-style.xml 是Google Java代码规范文件,适用于Intellij IDEA开发工具; | |
阿里 | https://github.com/alibaba/p3c •Java开发手册(黄山版).pdf 覆盖编程规约、异常日志、单元测试等七个维度; •Alibaba Java Coding Guidelines Jetbrains插件市场提供的代码指导插件, idea-plugin 提供编译、安装、使用手册; |
Sun/Oracle | The Original Sun Java Style Guide(https://www.oracle.com/java/technologies/javase/codeconventions-contents.html) |
Andriod | Android Open Source Project (AOSP) Style Guide(https://source.android.com/docs/setup/contribute/code-style?hl=zh-cn) |
Twitter’s Java Style Guide(https://github.com/twitter-archive/commons/blob/master/src/java/com/twitter/common/styleguide.md) | |
Coderanch | The CodeRanch Style Guide(https://coderanch.com/wiki/718799/Style) |
2.2 规范选择与制定
上面列举了多项代码规范,在决定使用哪种规范之前,可以考虑三个方面:
明确目标:使用代码规范的目的是什么,只是为了项目内代码风格一致,还是为了和其他团队分享或者其他公司分享或者开源?
使用环境:开发阅读代码的环境是什么,GitHub、GitLab、公司内部的代码平台?
工具支持:code format工具支持情况,code sytle check工具支持情况,编译工具:maven、gradle,IDE:IntelliJ、Eclipse、VS Code;
Google Java Style部分代码样式团队难以接受,比如2空格缩进等,可以在原始Google Java Style基础上,适当定制化调整。
2.3 规范使用
代码样式规范的使用分为两个方面:
代码格式化
代码样式校验
修改google_checks中的内容:
除了上述的长度和缩进的代码样式校验,Checkstyle还可以配置其它代码校验,可根据团队接受情况予以配置。
3.1 Code Style配置&使用
a. IDEA配置Code Style
在 IntelliJ IDEA下,使用intellij-java-jd-style.xml进行代码格式化之前,需要先进行配置。
设置配置文件,路径:IntelliJ IDEA→Preference→Editor→Code Style,参考下图:
完成上述配置后,在Mac环境的IntelliJ IDEA中,可以对选中的代码格式化(快捷键:Option+Command+L)或者对选中的文件格式化(快捷键:Shift+Option+Command+L)
3.2 Check Style配置&使用
Check Style的配置和使用有两种主要方式:
1. 在开发环境IDE中,开发者配置插件后,可触发代码样式规范的检查,根据提示项进行代码修改;
在 IntelliJ IDEA下,安装Plugin,路径:IntelliJ IDEA→Preference→Plugins,参考下图:
下载配置文件checkstyle.xml,配置CheckStyle-IDEA插件,路径:IntelliJ IDEA→Preference→Tools→Checkstyle,增加自定义配置文件并命名,参考下图:
安装配置CheckStyle插件后,工具窗口增加了CheckStyle Tab,在CheckStyle窗口进行check,可以选择:
Check Current File
Check Module
Check Project
参考Checkstyle maven插件的官方配置案例:多模块项目配置,通常我们在项目中新建build-tools模块,将checkstyle.xml等配置文件放在这个模块的resources目录下。
project-name| -- pom.xml| -- build-tools| | -- pom.xml| | -- src| | | -- main| | | -- resources| | | -- checkstyle.xml| | | -- checkstyle-suppressions.xml| -- core| -- gui| -- other-module
build-tools模块的pom.xml使用IDEA自动生成的配置文件,类似:
<?xml version="1.0" encoding="UTF-8"?><project><parent><artifactId>project-name</artifactId><groupId>com.jd.project-name</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>build-tools</artifactId></project>
配置文件checkstyle.xml、checkstyle-suppressions.xml可以从coding中下载
父项目的pom.xml中,增加如下配置:
<project>...<modules>...<module>build-tools</module></modules><properties>...<maven.checkstyle.version>3.6.0</maven.checkstyle.version></properties><build><pluginManagement><plugins><plugin><!-- Approach followed here is roughly the same as mentioned here:https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html--><groupId>org.apache.maven.plugins</groupId><artifactId>maven-checkstyle-plugin</artifactId><version>${maven.checkstyle.version}</version><configuration><configLocation>build-tools/src/main/resources/checkstyle.xml</configLocation><includeTestSourceDirectory>true</includeTestSourceDirectory><outputFile>checkstyle-report.xml</outputFile><consoleOutput>false</consoleOutput><failOnViolation>true</failOnViolation><excludes>target/**</excludes></configuration><executions><execution><id>checkstyle</id><phase>validate</phase><goals><goal>check</goal></goals></execution></executions></plugin></plugins></pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-checkstyle-plugin</artifactId></plugin></plugins></build><reporting><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-checkstyle-plugin</artifactId><reportSets><reportSet><reports><report>checkstyle</report></reports></reportSet></reportSets></plugin></plugins></reporting></project>
maven-checkstyle-plugin的配置<configuration>参数具体可参见checkstyle:check的参数说明。
plugins>plugin>executions>execution
id可以自行决定,这里选择"checkstyle"; phase是绑定到maven lifecycle的哪个执行阶段,这里绑定到"validate"上,即执行maven validate的时候会执行该plugin任务;选择validate阶段可以保障checkstyle:check在代码编译之前执行,如果checkstyle检查出违反样式规范的问题,在代码编译之前就会报告出来; goals>goal是只绑定执行plugin的哪个任务,这里绑定的是"check";
plugins>plugin>configuration>failOnViolation
检查到违反样式规范的问题,打印出来,打印的问题级别根据checkstyle.xml中配置的severity确定; 将severity级别配置为error,并将failOnViolation设为true,检查到违反样式规范的问题时,会停止maven命令继续执行;
d. maven使用CheckStyle插件
也可以单独执行命令:
mvn checkstyle:check注意:为了实现自动化代码样式规范检查,可以为项目配置行云流水线任务,设置代码评审的自动化检查:指定流水线作为卡点,指定为卡点的流水线需运行成功后MR才允许合并。
参考
The Original Sun Java Style Guide:https://www.oracle.com/technetwork/java/codeconvtoc-136057.html
Android Open Source Project (AOSP) Style Guide:https://source.android.com/setup/contribute/code-style
Twitter’s Java Style Guide:https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md
The CodeRanch Style Guide:https://coderanch.com/wiki/718799/Style
Jetbrains Java code style: https://www.jetbrains.com/help/idea/code-style-java.html
CheckStyle官网:https://checkstyle.org/index.html
CheckStyle发布:https://github.com/checkstyle/checkstyle/releases/
Apache Maven Checkstyle Plugin:https://maven.apache.org/plugins/maven-checkstyle-plugin/usage.html
推荐阅读