Java Code Style

Recently, I am puzzled by the different code styles of team members and the uncontrollable code quality. As an old driver, I am worried about the maintainability of post-service services. Many one-to-one code reviews are time-consuming and labor-intensive, and the effect is not obvious. After a lot of hard thinking, after many reflections, I came to the conclusion that no rules can be made without rules. Reliable services must be based on a unified code style. It is not enough to just advocate. Effective executable mechanisms must be used to ensure the final effect.
 
The executable mechanism here includes the following aspects:
 
(1) There are unified code style constraints;
(2) IDE assists in checking whether the code style constraints are satisfied, and real-time detection and reminder;
(3) The process of building the project is forced to check whether the code style constraints are satisfied, and if not, the construction is terminated;
 
This brief document records the practice of using "Google Java Code Style" as a code style constraint.
 
1.Google Code Style Guide
 
 
2. IDEA configures Google Style
 
IntelliJ IDEA --> Preferences... --> Editor -> Code Style --> Java --> Import Scheme --> IntelliJ IDEA code style XML
 
 
 
 
 
After this step is configured, the "Google Java Code Style" style will be used for formatting when formatting the code, such as code indentation.
 
3.IDEA配置CheckStyle-IDEA Plugin
 
(1) Install CheckStyle-IDEA Plugin;
 
IntelliJ IDEA --> Preferences... --> Plugins
 
 
 
(2) Configure CheckStyle-IDEA Plugin to use "Google Java Code Style";
 
IntelliJ IDEA --> Preferences... --> Other Settings --> Checkstyle
 
 
After this step of configuration is completed, IDEA will detect in real time whether the code style meets the "Google Java Code Style", if not, there will be an exception reminder.
 
4.Maven configure checkstyle Plugin
 
The following configuration can be done in Project or Moudle pom.xml:
 
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <version>3.0.0</version>
      <executions>
        <execution>
          <id>validate</id>
          <phase>validate</phase>
          <configuration>
            <configLocation>google_checks.xml</configLocation>
            <encoding>UTF-8</encoding>
            <consoleOutput>true</consoleOutput>
            <failsOnError>true</failsOnError>
            <violationSeverity>warning</violationSeverity>
          </configuration>
          <goals>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
 
After this step is configured, the project build process (eg: mvn clean package) will check whether the code style satisfies the "Google Java Code Style", if not, the build process will be terminated.
 
5.Default file template
 
 
 
/**
* @author ${USER}
*/
 
After this step is configured, it is used to unify the JavaDoc style.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817731&siteId=291194637