In the programming industry, you must have been told or said: code to specification! JD also lists this requirement for job interviews: good programming habits. It’s all about Code Style.
Everyone has their own programming habits. For example, if you like two Spaces, he likes four Spaces. You like the opening brace at the end of the line, he likes the other line. Wait, these habits are not superior or inferior, just a personal programming hobby that everyone has developed over time.
When you’re developing a project on your own, there’s no code specification, just your personal programming habits. However, if multiple people work together to develop a project, and there is no uniform code specification, everyone in the project will follow their own habits, resulting in the whole project code looks disorganized, very poor readability, and continuously increase the subsequent maintenance costs.
As a result, code specifications are often involved in teamwork. Often, a company will also have a common code specification. In fact, experience has shown that using code specifications can reduce the number of bugs and error rates to some extent. In this article, we’ll look at how to use code specifications in Android projects, primarily in the Java language.
Code Style
Code specifications also vary from person to person, or company to company. Before, I worked in a company where a leader wrote a code specification using Word documents. Then my colleagues read it carefully, kept it in mind and paid more attention to Coding. In fact, you can configure code specification files using the IDE. In this way, most of the memory-based problems can be eliminated and the specification can be automated.
As for the code specification files, some well-known Internet companies also have their own code specifications publicly. These are excellent resources that we can directly learn from and use, and of course you can also modify them according to your own situation. Some of the more famous ones are:
-
Google Style Guides
-
Square Java Style
Using the Java language Code specification provided by Google as an example, we briefly describe how to configure Code Style using Android Studio.
First download the intellij IDE configuration file to your local computer:
intellij-java-google-style.xml
The contents of this file are some XML configurations, here is a sample of them:
<? The XML version = "1.0" encoding = "utf-8"? >
<code_scheme name="GoogleStyle">
<option name="OTHER_INDENT_OPTIONS">
<value>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="TAB_SIZE" value="2" />
<option name="USE_TAB_CHARACTER" value="false" />
<option name="SMART_TABS" value="false" />
<option name="LABEL_INDENT_SIZE" value="0" />
<option name="LABEL_INDENT_ABSOLUTE" value="false" />
<option name="USE_RELATIVE_INDENTS" value="false" />
</value>
</option>
.
</code_scheme>
Open Android Studio, go to the Code Style menu in the Preferences screen, select the Java language, click the Manage button, and Import the XML configuration file you downloaded earlier. You can then see the GoogleStyle option in the Scheme drop-down list. Toggle:
Later, in the Coding process, some shortcut key operations, such as formatting and TAB key, will be automatically implemented in accordance with the code specification. Of course, you can modify the code specification as long as you know what it means. Google also has an online document for this:
Google Java Style Guide
Code formatting
With the code specification, you can format the code of the current file directly using the shortcut keys. The default shortcut keys on the Mac are:
Option + Command + L
You can also directly select the entire project or a directory and right click on the Reformat Code option to batch format.
Of course, you can also download the Formatting Jar package provided by Google and use the command line tool to format the specified file. GitHub address is:
google-java-format
As an example, batch format all Java files in the current directory (note that the jar file directory is changed when in actual use) :
Find. -name "*.java" -exec java-jar ~/tools/google-java-format-1.3-all-deps.jar -r {} \;
Check Style
With code specification and formatting, we can normalize our code to a certain extent with the IDE, but there are parts that don’t completely constrain the developer. For example, naming variables, you can still name them whatever you want; You can write as many characters per line as you like. It’s up to you to do what you do, to follow the rules or not.
So, is there a way to check that the code you’ve written meets certain specifications? The answer is yes. CheckStyle is a plug-in that automatically checks your code for compliance.
The CheckStyle tool helps you check that Javadoc eye documents conform to the specification (for example, the first line must end with an English period), that variables, functions, and so on are named according to a given regular expression, that parentheses and braces are whitespace compliant, and so on. These specification requirements can be set freely by convention in an XML file.
CheckStyle’s GitHub download is:
https://github.com/checkstyle/checkstyle
You can also search for installations using Android Studio online installation:
The use of CheckStyle is required in conjunction with the XML check file. Google also provides a corresponding Java code specification inspection rules file and documentation, check the file download address please click:
google_checks.xml
In the same way, feel free to modify as you see fit. But it’s best to keep the same Code Style file you used earlier.
You can then configure the CheckStyle file to the CheckStyle tool, also in the Preferences window (the IDE also has a CheckStyle rule by default) :
Restart the IDE and you can see the CheckStyle action option in the bottom menu bar:
Open the CheckStyle window, select the previously configured Google-checkstyle check rule file, click the left side of the Run button, you can check the current open Java file specification:
As you can see, the number of lines and other information that do not meet the specifications are displayed. You can select and modify them one by one. CheckStyle: no problems (CheckStyle: no problems)
The advantage of the CheckStyle plug-in tool is that you can check the specification for a Java file in your project directly in the IDE and make changes easily by locating the error message directly to the relevant code. As an added bonus, you can get CheckStyle prompts in real time as you write code, such as:
As you can see, in the edit area you are prompted to use a space before the opening curly brace in the method to separate the rest of the content. In this way, we can know whether the current code conforms to the specification at any time when Coding, which is very convenient.
The downside of CheckStyle is that it can’t batch check multiple files. For example, if I want to know if a project or a Java file in a directory is compliant, the plug-in can’t. At this point, you can use the command line tool.
Download the Jar package and XML rule file provided by CheckStyle, then open the command line tool and enter the check command. Such as:
Java -jar ~/tools/checkstyle-7.8.2-all.jar -c ~/tools/google_checks.xml mainactivity.java
The downside of command line tools is that there is no way to locate source code, so you can only see where the current file meets the specification. For more details on the use of CheckStyle, please refer to the online documentation.
In fact, the CheckStyle tool is perfect for collaborative team development. For example, an administrator can deploy CheckStyle on a server where SVN or Git’s remote repository is located. If the code for push or Merge is found to be inconsistent with this specification, it will be rejected. In this way, it is possible to force every developer on the team to modify their code strictly to the specifications, resulting in a uniform code style for the entire project or the entire company.
Open source code
In addition to the Check rule file provided by Google, there are a few other open source options available from well-known companies:
-
HuaWei CheckStyle
-
Square CheckStyle
Other tools
CheckStyle is not the only static code analysis tool you can use to improve your code quality:
-
Android Lint
-
FindBugs
-
PMD
And so on. Just pick one that suits you. Tools are just a means. Improving team collaboration and ensuring code quality is our ultimate goal.
Refer to the link
You can also read these articles about code specifications and the use of related tools:
-
How to improve quality and syntax of your Android code
-
Code specification and some of the tools available in the Android project