almos-kolumban/javarules icon
public
Published on 3/19/2025
almos-kolumban/javarules

Rules
  • Prefer constructor injection over field injection for better testability and immutability.
  • Use proper logging (e.g., SLF4J) instead of System.out.println().
  • Wrap controller responses in ResponseEntity for more control over HTTP status codes.
  • Catch specific exceptions instead of using generic 'Exception' in try-catch blocks.
  • Use Lombok annotations like @Data, @Value, and @Builder to reduce boilerplate code.
  • Keep controllers lean — delegate business logic to service classes.
  • Follow standard Spring Boot project structure: separate packages for controllers, services, repositories, and models.
  • Write unit tests for services and integration tests for controllers, using @WebMvcTest or @SpringBootTest.
  • Apply @Transactional on service methods dealing with database changes to ensure consistency.
  • Use @ControllerAdvice for centralized exception handling.
  • Methods should do one thing and do it well — follow the Single Responsibility Principle (SRP).
  • Favor meaningful method and variable names that clearly express intent.
  • Avoid magic numbers and strings — use constants or enums for better readability and maintainability.
  • Limit method parameters to 3 or fewer — use objects or builders for complex inputs.
  • Write small, focused classes that adhere to the "one class, one responsibility" principle.
  • Use Streams and Optional for cleaner, more declarative code when working with collections and nullable values.