Jackson Coding Style Guide
December 18, 2023 ยท View on GitHub
This document serves as the coding conventions for Java source code in the Jackson project. Some parts of this document only apply to Java source code, while others apply to all source code in the project.
Table of Contents
Source Formatting
Indentation
- Rule: Use spaces instead of tabs.
- Amount: 4 spaces per indentation level.
Import Statements
Wildcard Imports
- Usage: Wildcards are permissible.
- Threshold: Employ wildcards for 5 or more imports from the same package.
- Exceptions: For frequently used packages like
java.util,java.io, and main Jackson packages ( e.g.,com.fasterxml.jackson.databind), wildcards may be used for fewer imports.
Ordering
Import statements should be grouped and ordered in the following manner:
- JDK Imports: Begin with standard Java (JDK) imports.
- General 3rd Party Imports: Follow with imports from third-party libraries (e.g., JUnit).
- Jackson Core Types: Next, import Jackson core types, in the order of annotations, core, and databind.
- Component-Specific Types: For non-core Jackson components, import component-specific types last.
Static import statements should be grouped after non-static ones, using same ordering.
So, for example we might have:
import java.io.*; // JDK imports
import java.util.*;
import org.junit.*; // General 3rd party imports
import com.fasterxml.jackson.annotation.*; // Jackson core types: annotations
import com.fasterxml.jackson.core.*; // Jackson core types: core
import com.fasterxml.jackson.databind.*; // Jackson core types: databind
import com.fasterxml.jackson.other.modules.*; // and some Component-specific imports (if any)
// same mechanism for static imports
...static import statements...
Naming Conventions
Field Naming
Standard Java Language naming recommendation (camel-case) is followed with following modifications:
- Non-Public Fields: Prefix non-public member fields with an underscore (e.g.,
_fieldName).
Method Naming
- Public Methods: Adhere to standard Java naming conventions.
- Internal Methods: Prefix methods used within the class with an underscore.
- Sub-Class Methods: Optionally, use a leading underscore for methods intended for subclass use, but not for external class calls.
Use of final keyword
- Fields: Strongly encourage the use of
finalfor fields, assigning them in the constructor to promote immutability. - Method Parameters: Allowed but not necessarily encouraged.
- Local Variables: Encouraged where applicable.
Maximum Line Length
- Recommendation: Lines should not exceed 100 characters.
- Javadocs: Strict adherence to a maximum of 100 characters per line is recommended.
Testing Conventions
Test Class Naming
- XxxxTest: Now The preferred naming convention has shifted to "XxxTest". In the past, test classes were typically named "TestXxx".
- JUnit and Maven Compatibility: It is important to note that for
JUnitintegration withMaven, test classes must either start or end with"Test"to be automatically included in test runs.
Failing Tests
- Dedicated Directory for Failing Tests: For reproduction of bugs, Jackson projects keep a set of failing tests
under
src/test/java/**/failing. - Execution Policy: These tests are excluded from automatic execution. They are specifically designed for manual runs to facilitate the reproduction and investigation of reported issues.