使用CheckStyle檢查編碼規(guī)范
編碼規(guī)范指的是編寫代碼的樣式規(guī)則。不同的程序員,往往有不一樣的風(fēng)格。單單是拋出使用空格鍵還是TAB鍵的問題,就足以讓程序員演化成針鋒相對的兩個派別。
為什么要在項(xiàng)目中統(tǒng)一編碼規(guī)范?
好的編程規(guī)范有助于寫出易于閱讀、質(zhì)量更高、錯誤更少、更易于維護(hù)的程序。
一般代碼編寫規(guī)范包含代碼結(jié)構(gòu),格式,命名、javadoc還有編碼的最佳實(shí)踐等內(nèi)容。
項(xiàng)目搭建之處,團(tuán)隊(duì)內(nèi)約定了使用google java code style編碼規(guī)范。在這個規(guī)范里,縮進(jìn)是使用2個空格的,追著團(tuán)隊(duì)新成員的加入,慢慢的在同一個文件中同時出現(xiàn)了2個空格和4個空格縮進(jìn),格式整個亂套了。此外對變量的命名也出現(xiàn)了a,b,c作為變量命名的情況。迫切需要引入一種機(jī)制,來保證項(xiàng)目代碼的和諧一致。我們使用了CheckStyle。
CheckStyle是什么
CheckStyle是一個幫助程序員來遵守一直的編碼規(guī)范的工具。默認(rèn),它支持google 和sun 的java style guide。而且它是高度可配置的,允許自定義編碼規(guī)范,并可以對各種IDE(eclipse、Intellij)和構(gòu)建工具(maven,gradle)的支持。
IDE中配置code style
在intellij中安裝code style 配置
下載 intellij-java-google-style.xml,拷貝到 ~/Library/Preferences/IntelliJIdea15/codestyles下.
打開IntelliJ,F(xiàn)ile->Setting->Editor->Code Style ,選擇 Google_Style作為項(xiàng)目的代碼樣式模板。

在Eclipse中安裝code style 配置
下載 eclipse-java-google-style.xml,在eclipse中打開Window/Preferences界面,選擇Java/Code Style/Formatter,進(jìn)行導(dǎo)入。
使用CheckStyle工具檢查編碼規(guī)范
CheckStyle能夠幫助程序員檢查代碼是否符合制定的規(guī)范。通過將CheckStyle的檢查引入到項(xiàng)目構(gòu)建中,可以強(qiáng)制讓項(xiàng)目中的所有的開發(fā)者遵循制定規(guī)范,而不是僅僅停留在口頭約定上。如果發(fā)現(xiàn)代碼違反了標(biāo)準(zhǔn),阻止構(gòu)建成功
CheckStyle有針對不同IDE和maven的各種插件,方便開發(fā)者隨時隨地對代碼進(jìn)行靜態(tài)檢查。
在intellij 中安裝CheckStyle-IDE插件
File->Setting->Plugins中安裝CheckStyle-IDE插件

File->Setting->Other Settings->Check Style中配置 配置自定義Configuaration,使用google_check.xml.
File->Setting->Editor->Inspections 配置CheckStyle中選擇是否進(jìn)行實(shí)時檢查。

也可以右鍵 check current file 對當(dāng)前文件進(jìn)行檢查
在Eclipse 中安裝 CheckStyle 插件
從插件下載
Preferences --> Checkstyle 配置check configuration

右擊操作對當(dāng)前文件進(jìn)行代碼檢查
使用maven執(zhí)行檢查
maven 配置
在項(xiàng)目構(gòu)建的時候加入對代碼格式的檢查,發(fā)現(xiàn)有違反的,直接讓構(gòu)建失敗。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>google_checks.xml</configLocation>
<encoding>UTF-8</encoding>
<failOnViolation>true</failOnViolation>
<linkXRef>false</linkXRef>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<maxAllowedViolations>0</maxAllowedViolations>
<violationSeverity>warning</violationSeverity>
</configuration>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>6.19</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<reportSets>
<reportSet>
<reports>
<report>checkstyle</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
goal
| Goal | Report or Not | Description |
|---|---|---|
| checkstyle:check | No | 檢查代碼,并可能導(dǎo)致構(gòu)建失敗 |
| checkstyle:checkstyle | Yes | 檢查代碼并生成代碼檢查結(jié)果報表 |
| checkstyle:checkstyle-aggregate | Yes | 對多模塊的項(xiàng)目代碼檢查,并生產(chǎn)檢查匯總的結(jié)果報表 |
另外再說幾句
美國的童子軍,有一條規(guī)則 Leave the campground cleaner than you found it. 翻譯為 要讓離開時的營地比進(jìn)入時更加干凈。 這也是我們對待代碼的態(tài)度。