Java Coding Standards
This document outlines the Java coding standards to be followed in all development projects. These guidelines are based on the Google Java Style Guide and aim to ensure readability, maintainability, and consistency across codebases.
1. File and Class Organization
File Naming:
- Each top-level class must be in its own file.
- File name must match the class name (e.g.,
MyClass.java).
Class Naming:
Use
UpperCamelCasefor class and interface names.Example:
javapublic class CustomerOrder { ... }
Package Naming:
- Use all lowercase letters.
- Avoid underscores.
- Example:
com.example.project.module
Order of Members:
- Constants → Fields → Constructors → Methods → Inner Classes
2. Formatting
Indentation:
- 2 spaces per indentation level.
- Do not use tabs.
Line Length:
- Maximum 100 characters per line.
Braces:
Always use braces for
if,else,for,while, anddo.Opening brace goes on the same line as the statement.
Example:
javaif (condition) { doSomething(); } else { doSomethingElse(); }
Whitespace:
- Single space after keywords like
if,for,while. - No space between method name and parentheses when invoking or declaring.
- Single space after keywords like
3. Identifiers
Variables and Methods:
- Use
lowerCamelCase. - Example:
int totalCount;orvoid calculateTotal() { ... }
- Use
Constants:
- Use
UPPER_SNAKE_CASE. - Example:
static final int MAX_SIZE = 100;
- Use
4. Comments
Javadoc Comments:
Use for all public classes, methods, and fields.
Format:
java/** * Calculates the total order amount. * * @param quantity Number of items * @param price Price per item * @return Total cost */ public double calculateTotal(int quantity, double price) { ... }
Inline and Block Comments:
- Use
//for short explanations. - Use
/* ... */for longer, multi-line comments. - Keep comments up to date; remove obsolete comments.
- Use
5. Programming Practices
Use final when possible:
- Mark fields, parameters, and local variables as
finalwhen they should not change.
- Mark fields, parameters, and local variables as
Avoid magic numbers:
- Replace with named constants.
Prefer standard library methods:
- Avoid reinventing the wheel; use
Collections,Streams,Optional, etc.
- Avoid reinventing the wheel; use
Exceptions:
- Catch the most specific exception possible.
- Do not catch
Exceptionunless absolutely necessary.
Null handling:
- Minimize use of
null; preferOptionalfor return values that may be absent.
- Minimize use of
6. Testing
- Write unit tests for all public methods.
- Use descriptive names for test methods, e.g.,
calculateTotal_givenValidInput_returnsCorrectSum(). - Follow Arrange-Act-Assert (AAA) pattern for test clarity.
7. Annotations
- Use
@Overridewhen overriding methods. - Use
@Deprecatedwith a comment indicating replacement. - Use
@SuppressWarningsonly when absolutely necessary and document why.
8. Version Control Practices
- Use meaningful commit messages, e.g.,
Fix null pointer in calculateTotal. - Keep commits small and focused.
- Merge feature branches only after passing code review and tests.