Java Coding Standards
Java Coding Standards
This document outlines standardized Java coding conventions and best practices
to be followed in real-time, production-grade projects. It serves as a
guideline for developers to write clean, maintainable, scalable, and secure
code. It covers naming conventions, project structure, code formatting,
exception handling, design principles. Adhering to these standards will ensure
code consistency across the team, reduce technical debt, and improve
collaboration in real-world enterprise applications.
└── 📁 com.companyname.projectname
│ │
├── 📁 controller
│ │ │
→ REST controllers
├── 📁 service
│ │ │
→ Business logic
├── 📁 repository
│ │ │
→ JPA repositories
├── 📁 model
│ │ │
→ Entities (JPA)
├── 📁 dto
│ │ │
→ Data Transfer Objects
├── 📁 mapper
│ │ │
→ MapStruct or custom mappers
├── 📁 config
│ │ │
→ Spring configs (e.g., `SecurityConfig`)
├── 📁 exception
│ │ │
→ Custom exceptions & handlers
├── 📁 util
│ │ │
→ Utility/helper classes
└── 📁 security
│ │ │
→ Authentication & authorization
└── 📁 resources
│ │ │
├── 📄 application.yml
│ │
→ Main config file
├── 📁 static
│ │
→ Static assets (JS, CSS, etc.)
├── 📁 templates
│ │
→ Thymeleaf/FreeMarker templates
├── 📁 i18n
│ │
→ Localization files
└── 📁 log
│ │
│ │ → Log file storage (if configured
to write here)
└── 📁 test
│ │
└── 📁 java
│
└── 📁 com.companyname.projectname
│
├── 📁 controller
│
├── 📁 service
│
│
└── ... (tests matching main structure)
├── 📁 target
│
→ Compiled classes and build artifacts
(generated)
├── 📄 pom.xml
│
→ Maven build configuration
├── 📄 .gitignore → Git ignored files list
└── 📄 README.md → Project overview & instructions
🔹 4. Method Naming
Standard: camelCase (verbs first, descriptive)
Common patterns:
→ getAllUsers()
→ findUserById()
→ saveUser()
→ deleteUserById()
→ assignRolesToUser()
🔹 5. Variable Naming
● Standard: camelCase, meaningful names (no abbreviations)
✦ Examples:
→ String userName;
→ Long userId;
→ List<Role> assignedRoles;
→ UserResponseDto userResponse;
🔹 6. Constant Naming (e.g., Roles like ROLE_AGENT)
Standard: UPPER_SNAKE_CASE, usually in enums or constants classes.
Defined in Enum: Best practice in Spring Security
✦ Example :
public enum ERole {
ROLE_USER,
ROLE_AGENT,
ROLE_ADMIN,
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
@ResponseStatus(HttpStatus.NOT_FOUND)
public class RoleNotFoundException extends RuntimeException {
public RoleNotFoundException(String message) { super(message); }
}
}
✦ Example :
log.info("User created: id={}, name={}", user.getId(), user.getName());
🔹 12. Validation (Always Validate Input)
● Use javax.validation (@NotNull, @Size, etc.) on DTOs
✦ Example:
public class UserRequestDto {
@NotBlank
private String username;
@Email
private String email;
}
@Service
@RequiredArgsConstructor
public class UserService {
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
@PostMapping
public ResponseEntity<UserResponseDto> createUser(@Valid @RequestBody
userRequestDto dto) {
return ResponseEntity.status(HttpStatus.CREATED).
body(userService.createUser(dto));
}
@GetMapping
public ResponseEntity<List<UserResponseDto>> getAllUsers() {
return ResponseEntity.ok(userService.getAllUsers());
}
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String email;
@Enumerated(EnumType.STRING)
private Status status;
}
➤ Note : ⚠️ Avoid @Data on JPA entities because:
● It adds toString(), which can cause circular reference issues in
bi-directional relationships.
● Adds equals() and hashCode(), which can break persistence behavior
(especially with proxies).
THANK YOU