mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-28 13:42:57 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+334
-294
@@ -1,296 +1,336 @@
|
||||
---
|
||||
description: Enforces best practices for Java development, covering code style, performance, security, and testing. Provides guidelines for writing clean, maintainable, and efficient Java code.
|
||||
globs: *.java
|
||||
description: Definitive guidelines for writing high-quality, maintainable, and performant Java code, adhering to modern best practices (Java 21/25) and enterprise standards like Google Java Style.
|
||||
globs: **/*
|
||||
---
|
||||
- # Java Best Practices
|
||||
|
||||
This document outlines comprehensive best practices for Java development, covering code organization, common patterns, performance considerations, security, testing, common pitfalls, and tooling. Adhering to these guidelines will help you write clean, maintainable, efficient, and secure Java code.
|
||||
|
||||
- ## 1. Code Organization and Structure
|
||||
|
||||
- ### 1.1 Directory Structure
|
||||
|
||||
- **Maven Standard Layout:** Use the standard Maven directory structure for most projects. This provides a consistent and predictable layout that's easily understood by other developers and tools.
|
||||
|
||||
src/
|
||||
main/
|
||||
java/
|
||||
com/example/ <- Your package structure starts here
|
||||
resources/
|
||||
test/
|
||||
java/
|
||||
com/example/
|
||||
resources/
|
||||
pom.xml <- Maven project file
|
||||
|
||||
- **Gradle Layout:** Gradle supports the Maven layout and provides other ways to configure the source directories. Choose a layout that best fits your project's needs.
|
||||
|
||||
- **Package by Feature:** Organize packages by feature rather than by layer (e.g., controllers, services, repositories). This improves cohesion and reduces dependencies between features.
|
||||
|
||||
src/
|
||||
main/
|
||||
java/
|
||||
com/example/
|
||||
user/
|
||||
UserController.java
|
||||
UserService.java
|
||||
UserRepository.java
|
||||
product/
|
||||
ProductController.java
|
||||
ProductService.java
|
||||
ProductRepository.java
|
||||
|
||||
- **Modularization:** For large projects, consider using Java modules (Jigsaw, introduced in Java 9) to improve encapsulation and reduce dependencies.
|
||||
|
||||
- ### 1.2 File Naming Conventions
|
||||
|
||||
- **Classes and Interfaces:** Use `PascalCase` (e.g., `UserController`, `UserService`).
|
||||
- **Methods and Variables:** Use `camelCase` (e.g., `getUserById`, `userName`).
|
||||
- **Constants:** Use `UPPER_SNAKE_CASE` (e.g., `MAX_RETRIES`, `DEFAULT_TIMEOUT`).
|
||||
- **Packages:** Use all lowercase (e.g., `com.example.user`).
|
||||
- **Avoid abbreviations:** Use meaningful and descriptive names.
|
||||
|
||||
- ### 1.3 Module Organization
|
||||
|
||||
- **`module-info.java`:** Use `module-info.java` to define module dependencies and exported packages. This allows for strong encapsulation and controlled access to internal APIs.
|
||||
- **Explicit Dependencies:** Declare all module dependencies explicitly in `module-info.java`. Avoid relying on transitive dependencies.
|
||||
- **Minimize Exports:** Only export the packages that are intended for public use. Keep internal packages hidden from other modules.
|
||||
|
||||
- ### 1.4 Component Architecture
|
||||
|
||||
- **Dependency Injection:** Use dependency injection (DI) to manage component dependencies. Frameworks like Spring and Guice simplify DI.
|
||||
- **Inversion of Control (IoC):** Apply IoC to decouple components and improve testability.
|
||||
- **Layered Architecture:** Structure your application into layers (e.g., presentation, business logic, data access). This promotes separation of concerns and maintainability.
|
||||
- **Microservices:** For large, complex applications, consider a microservices architecture. This allows for independent development, deployment, and scaling of individual services.
|
||||
|
||||
- ### 1.5 Code Splitting
|
||||
|
||||
- **Feature Toggles:** Use feature toggles to enable or disable features at runtime. This allows for incremental deployment and testing of new features.
|
||||
- **Dynamic Loading:** Use dynamic class loading to load modules or components on demand. This can reduce the initial startup time and memory footprint of your application.
|
||||
- **Conditional Compilation:** Use conditional compilation (e.g., with Maven profiles) to include or exclude code based on the environment. This allows for different configurations for development, testing, and production.
|
||||
|
||||
- ## 2. Common Patterns and Anti-patterns
|
||||
|
||||
- ### 2.1 Design Patterns
|
||||
|
||||
- **Singleton:** Use the Singleton pattern sparingly and only when a single instance of a class is truly required. Consider dependency injection as an alternative.
|
||||
- **Factory:** Use the Factory pattern to create objects without specifying their concrete classes. This promotes loose coupling and allows for easy substitution of different implementations.
|
||||
- **Strategy:** Use the Strategy pattern to encapsulate different algorithms or behaviors. This allows you to switch between algorithms at runtime.
|
||||
- **Observer:** Use the Observer pattern to define a one-to-many dependency between objects. This allows for loose coupling and easy addition of new observers.
|
||||
- **Template Method:** Use the Template Method pattern to define the skeleton of an algorithm in a base class, allowing subclasses to override specific steps without changing the overall structure.
|
||||
- **Builder:** Use the Builder pattern to construct complex objects with many optional parameters. This improves readability and reduces the risk of errors.
|
||||
|
||||
- ### 2.2 Recommended Approaches
|
||||
|
||||
- **Resource Management:** Always use try-with-resources to ensure proper resource management (e.g., closing streams, connections). This prevents resource leaks.
|
||||
java
|
||||
try (FileInputStream fis = new FileInputStream("file.txt")) {
|
||||
// Use the file input stream
|
||||
}
|
||||
|
||||
- **String Concatenation:** Use `StringBuilder` or `StringBuffer` for string concatenation, especially in loops. Avoid using the `+` operator for repeated string concatenation.
|
||||
java
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
sb.append(i);
|
||||
}
|
||||
String result = sb.toString();
|
||||
|
||||
- **Collections:** Prefer Java Collections over arrays for their flexibility and utility. Use generics to ensure type safety.
|
||||
|
||||
- ### 2.3 Anti-patterns and Code Smells
|
||||
|
||||
- **God Class:** Avoid creating large classes that do too much. Break down large classes into smaller, more manageable components.
|
||||
- **Long Method:** Avoid creating long methods. Break down long methods into smaller, more focused methods.
|
||||
- **Shotgun Surgery:** Avoid making many small changes in multiple classes. This indicates a lack of cohesion and can make it difficult to maintain the code.
|
||||
- **Data Clumps:** Avoid passing the same group of data items together in multiple methods. Create a class to encapsulate the data items.
|
||||
- **Primitive Obsession:** Avoid using primitive types excessively. Create value objects to represent domain concepts.
|
||||
- **Switch Statements:** Limit use of switch statements especially with larger number of cases. Consider using polymorphism with Strategy pattern.
|
||||
- **Empty Catch Blocks:** Avoid empty catch blocks. Always handle exceptions appropriately, either by logging them, rethrowing them, or taking corrective action.
|
||||
|
||||
- ### 2.4 State Management
|
||||
|
||||
- **Immutability:** Prefer immutable objects whenever possible. Immutable objects are thread-safe and easier to reason about.
|
||||
- **Stateless Services:** Design services to be stateless. This improves scalability and simplifies testing.
|
||||
- **Session Management:** Use session management frameworks (e.g., Spring Session) to manage user sessions in web applications.
|
||||
|
||||
- ### 2.5 Error Handling
|
||||
|
||||
- **Exceptions for Exceptional Cases:** Use exceptions only for exceptional cases, not for normal control flow.
|
||||
- **Specific Exception Types:** Catch specific exception types rather than generic `Exception`. This allows you to handle different types of errors differently.
|
||||
- **Logging:** Log exceptions with sufficient context to aid debugging. Include the stack trace and any relevant data.
|
||||
- **Custom Exceptions:** Create custom exception types to represent application-specific errors.
|
||||
- **Don't Swallow Exceptions:** Never swallow exceptions without logging or handling them. It hides the exception making debugging much harder.
|
||||
|
||||
- ## 3. Performance Considerations
|
||||
|
||||
- ### 3.1 Optimization Techniques
|
||||
|
||||
- **Caching:** Use caching to store frequently accessed data in memory. Frameworks like Caffeine and Guava Cache provide efficient caching implementations.
|
||||
- **Connection Pooling:** Use connection pooling to reuse database connections. This reduces the overhead of creating and closing connections.
|
||||
- **Efficient Algorithms:** Choose appropriate algorithms for specific tasks. Consider the time and space complexity of different algorithms.
|
||||
- **Lazy Initialization:** Use lazy initialization to defer the creation of objects until they are actually needed.
|
||||
- **Minimize Object Creation:** Reduce unnecessary object creation. Use object pooling or reuse existing objects whenever possible.
|
||||
|
||||
- ### 3.2 Memory Management
|
||||
|
||||
- **Garbage Collection:** Understand how Java's garbage collector works. Avoid creating objects that are quickly discarded, as this puts pressure on the garbage collector.
|
||||
- **Memory Profiling:** Use memory profiling tools to identify memory leaks and optimize memory usage.
|
||||
- **Large Objects:** Be careful when handling large objects. They can cause fragmentation and increase garbage collection times.
|
||||
|
||||
- ### 3.3 Rendering Optimization (If Applicable)
|
||||
|
||||
- **Buffering:** Use buffering to reduce the number of I/O operations when rendering large amounts of data.
|
||||
- **Compression:** Use compression to reduce the size of rendered data.
|
||||
|
||||
- ### 3.4 Bundle Size Optimization (If Applicable)
|
||||
|
||||
- **Code Minification:** Use code minification to reduce the size of your codebase.
|
||||
- **Dead Code Elimination:** Remove unused code to reduce the bundle size.
|
||||
|
||||
- ### 3.5 Lazy Loading
|
||||
|
||||
- **On-Demand Loading:** Load resources or components only when they are needed.
|
||||
- **Virtual Proxy:** Use a virtual proxy to delay the loading of a heavy resource until it is accessed.
|
||||
|
||||
- ## 4. Security Best Practices
|
||||
|
||||
- ### 4.1 Common Vulnerabilities
|
||||
|
||||
- **SQL Injection:** Prevent SQL injection by using parameterized queries or prepared statements. Never concatenate user input directly into SQL queries.
|
||||
- **Cross-Site Scripting (XSS):** Prevent XSS by encoding user input before displaying it in web pages.
|
||||
- **Cross-Site Request Forgery (CSRF):** Prevent CSRF by using anti-CSRF tokens.
|
||||
- **Authentication and Authorization Issues:** Implement proper authentication and authorization mechanisms to protect sensitive resources.
|
||||
- **Denial of Service (DoS):** Protect against DoS attacks by limiting request rates and implementing rate limiting.
|
||||
- **Insecure Deserialization:** Prevent insecure deserialization by avoiding deserialization of untrusted data, or using secure deserialization methods.
|
||||
- **Dependency Vulnerabilities:** Use tools like OWASP Dependency-Check to identify and mitigate vulnerabilities in third-party libraries.
|
||||
|
||||
- ### 4.2 Input Validation
|
||||
|
||||
- **Whitelisting:** Use whitelisting to validate input against a list of allowed values. Avoid blacklisting, as it is difficult to anticipate all possible malicious inputs.
|
||||
- **Regular Expressions:** Use regular expressions to validate input patterns (e.g., email addresses, phone numbers).
|
||||
- **Length Limits:** Enforce length limits on input fields to prevent buffer overflows.
|
||||
- **Encoding:** Encode user input to prevent XSS attacks.
|
||||
|
||||
- ### 4.3 Authentication and Authorization
|
||||
|
||||
- **Strong Passwords:** Enforce strong password policies (e.g., minimum length, complexity).
|
||||
- **Hashing:** Hash passwords using a strong hashing algorithm (e.g., bcrypt, Argon2) and a salt.
|
||||
- **Two-Factor Authentication (2FA):** Implement 2FA to provide an extra layer of security.
|
||||
- **Role-Based Access Control (RBAC):** Use RBAC to control access to resources based on user roles.
|
||||
- **OAuth 2.0:** Use OAuth 2.0 for delegated authorization.
|
||||
|
||||
- ### 4.4 Data Protection
|
||||
|
||||
- **Encryption:** Encrypt sensitive data at rest and in transit.
|
||||
- **Data Masking:** Mask sensitive data in logs and error messages.
|
||||
- **Access Control:** Restrict access to sensitive data to authorized users only.
|
||||
|
||||
- ### 4.5 Secure API Communication
|
||||
|
||||
- **HTTPS:** Use HTTPS for all API communication.
|
||||
- **TLS/SSL:** Configure TLS/SSL properly to ensure secure communication.
|
||||
- **API Keys:** Use API keys to authenticate API clients.
|
||||
- **Rate Limiting:** Implement rate limiting to prevent abuse of your APIs.
|
||||
- **Input Validation:** Validate all input to your APIs to prevent injection attacks.
|
||||
|
||||
- ## 5. Testing Approaches
|
||||
|
||||
- ### 5.1 Unit Testing
|
||||
|
||||
- **JUnit:** Use JUnit for unit testing.
|
||||
- **Mockito:** Use Mockito for mocking dependencies.
|
||||
- **Arrange-Act-Assert:** Follow the Arrange-Act-Assert pattern in your unit tests.
|
||||
- **Test Coverage:** Aim for high test coverage.
|
||||
- **Independent Tests:** Write independent tests such that failure of one test doesn't affect another test.
|
||||
|
||||
- ### 5.2 Integration Testing
|
||||
|
||||
- **Testcontainers:** Use Testcontainers to create lightweight, disposable instances of databases and other services for integration testing.
|
||||
- **Spring Boot Test:** Use Spring Boot's testing support for integration testing Spring applications.
|
||||
|
||||
- ### 5.3 End-to-End Testing
|
||||
|
||||
- **Selenium:** Use Selenium for end-to-end testing of web applications.
|
||||
- **Cypress:** Consider Cypress as an alternative to Selenium for end-to-end tests.
|
||||
|
||||
- ### 5.4 Test Organization
|
||||
|
||||
- **Test Directory:** Place your tests in a separate `test` directory.
|
||||
- **Naming Conventions:** Use clear naming conventions for your tests (e.g., `UserServiceTest`).
|
||||
- **Test Suites:** Group related tests into test suites.
|
||||
|
||||
- ### 5.5 Mocking and Stubbing
|
||||
|
||||
- **Mockito:** Use Mockito to create mocks and stubs for your tests.
|
||||
- **Verify Interactions:** Verify that your code interacts with dependencies as expected.
|
||||
- **Avoid Over-Mocking:** Avoid mocking too many dependencies. Focus on mocking the dependencies that are critical to the test.
|
||||
|
||||
- ## 6. Common Pitfalls and Gotchas
|
||||
|
||||
- ### 6.1 Frequent Mistakes
|
||||
|
||||
- **NullPointerExceptions:** Handle null values carefully to avoid `NullPointerException`.
|
||||
- **Resource Leaks:** Ensure that all resources are properly closed to avoid resource leaks.
|
||||
- **Thread Safety Issues:** Be aware of thread safety issues when writing multithreaded code.
|
||||
- **Ignoring Exceptions:** Never ignore exceptions without logging or handling them. It hides the exception making debugging much harder.
|
||||
|
||||
- ### 6.2 Edge Cases
|
||||
|
||||
- **Boundary Conditions:** Test boundary conditions to ensure that your code handles edge cases correctly.
|
||||
- **Empty Collections:** Handle empty collections gracefully.
|
||||
- **Invalid Input:** Validate input to ensure that it is within the expected range.
|
||||
|
||||
- ### 6.3 Version-Specific Issues
|
||||
|
||||
- **Deprecated APIs:** Be aware of deprecated APIs and avoid using them.
|
||||
- **Compatibility Issues:** Test your code with different versions of Java to ensure compatibility.
|
||||
|
||||
- ### 6.4 Compatibility Concerns
|
||||
|
||||
- **JVM Compatibility:** Ensure that your code is compatible with different JVM implementations.
|
||||
- **Library Compatibility:** Be aware of compatibility issues between different libraries.
|
||||
|
||||
- ### 6.5 Debugging Strategies
|
||||
|
||||
- **Logging:** Use logging to track the execution of your code and identify errors.
|
||||
- **Debugging Tools:** Use debugging tools to step through your code and inspect variables.
|
||||
- **Remote Debugging:** Use remote debugging to debug applications running on remote servers.
|
||||
- **JVM Profilers:** Use profilers like JProfiler or VisualVM to identify performance bottlenecks and memory leaks.
|
||||
|
||||
- ## 7. Tooling and Environment
|
||||
|
||||
- ### 7.1 Recommended Tools
|
||||
|
||||
- **IntelliJ IDEA:** A powerful IDE for Java development with excellent code completion, refactoring, and debugging support.
|
||||
- **Eclipse:** Another popular IDE for Java development.
|
||||
- **Maven:** A build automation tool for managing dependencies, building, and deploying Java projects.
|
||||
- **Gradle:** A build automation tool that provides more flexibility and control than Maven.
|
||||
|
||||
- ### 7.2 Build Configuration
|
||||
|
||||
- **Dependency Management:** Use Maven or Gradle to manage dependencies.
|
||||
- **Version Control:** Use version control (e.g., Git) to track changes to your codebase.
|
||||
- **Build Profiles:** Use build profiles to configure different builds for different environments.
|
||||
|
||||
- ### 7.3 Linting and Formatting
|
||||
|
||||
- **Checkstyle:** Use Checkstyle to enforce coding standards.
|
||||
- **PMD:** Use PMD to find potential bugs and code smells.
|
||||
- **SpotBugs:** Use SpotBugs to find potential bugs.
|
||||
- **Formatter:** Use automatic code formatting tools like IntelliJ's built-in formatter or plugins like `google-java-format` for consistency.
|
||||
|
||||
- ### 7.4 Deployment
|
||||
|
||||
- **Docker:** Use Docker to containerize your applications.
|
||||
- **Kubernetes:** Use Kubernetes to orchestrate your containers.
|
||||
- **Cloud Platforms:** Deploy your applications to cloud platforms like AWS, Azure, or Google Cloud.
|
||||
|
||||
- ### 7.5 CI/CD
|
||||
|
||||
- **Jenkins:** Use Jenkins for continuous integration and continuous delivery.
|
||||
- **GitHub Actions:** Use GitHub Actions for CI/CD.
|
||||
- **GitLab CI:** Use GitLab CI for CI/CD.
|
||||
- **Automated Testing:** Automate your unit, integration, and end-to-end tests in your CI/CD pipeline.
|
||||
|
||||
By adhering to these best practices, you can improve the quality, maintainability, and performance of your Java code. Remember to adapt these guidelines to your specific project requirements and team preferences.
|
||||
# Java Best Practices
|
||||
|
||||
This document outlines the definitive best practices for Java development within our team. Adherence to these rules ensures consistent, high-quality, and maintainable code, leveraging modern Java features and enterprise standards.
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
### 1.1. Style Guide Adherence
|
||||
Always follow the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html) for formatting, naming, and structural conventions. Configure your IDE (e.g., IntelliJ IDEA, VS Code with appropriate plugins) to automatically apply this style.
|
||||
|
||||
### 1.2. File Naming and Encoding
|
||||
Source files must be named after the single top-level class they contain, with a `.java` extension. All source files must use **UTF-8** encoding.
|
||||
|
||||
### 1.3. Indentation and Whitespace
|
||||
Use **4 spaces** for indentation. **Never use tabs.** For wrapped lines, use **8 spaces** (twice the normal indentation).
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
public class MyClass {
|
||||
void myMethod() {
|
||||
// Tab used for indentation
|
||||
System.out.println("Hello");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
public class MyClass {
|
||||
void myMethod() {
|
||||
// 4 spaces for indentation
|
||||
System.out.println("Hello");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 1.4. Braces
|
||||
Always use K&R style braces for non-empty blocks. Braces are mandatory for `if`, `else`, `for`, `do`, and `while` statements, even for single-line bodies.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
if (condition)
|
||||
doSomething(); // Missing braces
|
||||
|
||||
while (true){ // Bad K&R style
|
||||
doSomething();
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
if (condition) {
|
||||
doSomething();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
doSomethingElse();
|
||||
}
|
||||
```
|
||||
|
||||
### 1.5. Package and Import Statements
|
||||
Every class must belong to a package. Use explicit imports; **wildcard imports (`*`) are forbidden**. Imports are ordered: static imports, then non-static imports, separated by a single blank line, both groups in ASCII sort order.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
import java.util.*; // Wildcard import
|
||||
import static com.example.Constants.*; // Wildcard static import
|
||||
|
||||
public class MyClass { /* ... */ }
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
import static com.example.Constants.DEFAULT_VALUE;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MyClass { /* ... */ }
|
||||
```
|
||||
|
||||
### 1.6. Naming Conventions
|
||||
Adhere strictly to these conventions:
|
||||
* **Packages**: `com.yourcompany.project.module` (all lowercase, dot-separated).
|
||||
* **Classes/Enums/Records/Interfaces**: `PascalCase` (nouns).
|
||||
* **Variables/Parameters**: `camelCase`.
|
||||
* **Constants**: `SCREAMING_SNAKE_CASE` (for `static final` fields).
|
||||
* **Methods**: `camelCase` (verbs).
|
||||
* **Boolean Variables/Methods**: Prefix with `is`, `has`, `can`, `should`.
|
||||
* **Collections**: Use plural names (e.g., `users`, `messages`).
|
||||
* **Test Methods**: `featureUnderTest_testScenario_expectedBehavior()`.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
public class userManager { // Class name not PascalCase
|
||||
public static final String default_user_name = "guest"; // Constant not SCREAMING_SNAKE_CASE
|
||||
private List userList; // Collection not plural
|
||||
public boolean checkUser(User u) { /* ... */ } // Boolean method name unclear
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
package com.example.app.users;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserManager {
|
||||
public static final String DEFAULT_USER_NAME = "guest";
|
||||
private List<User> users;
|
||||
|
||||
public boolean isValidUser(User user) { /* ... */ }
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
### 2.1. Prefer Immutability
|
||||
Design classes to be immutable whenever possible. This simplifies concurrency and reasoning about state. Use `record` classes (Java 16+) for simple immutable data carriers.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
public class User {
|
||||
private String name;
|
||||
public User(String name) { this.name = name; }
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; } // Mutable setter
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
public record User(String name) {} // Immutable record (Java 16+)
|
||||
// Or traditional immutable class:
|
||||
public final class ImmutableUser {
|
||||
private final String name;
|
||||
public ImmutableUser(String name) { this.name = name; }
|
||||
public String getName() { return name; }
|
||||
}
|
||||
```
|
||||
|
||||
### 2.2. Embrace Functional Programming with Streams and Optionals
|
||||
Leverage Java's Stream API for collection processing and `Optional` for handling potentially absent values.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
List<String> names = getUsers().stream()
|
||||
.filter(u -> u.getAge() > 18)
|
||||
.map(User::getName)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Handling null:
|
||||
User user = findUserById(123L);
|
||||
if (user != null) {
|
||||
System.out.println(user.getName());
|
||||
} else {
|
||||
System.out.println("User not found");
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
List<String> names = users.stream()
|
||||
.filter(user -> user.getAge() > 18)
|
||||
.map(User::getName)
|
||||
.toList(); // Java 16+ for .toList()
|
||||
|
||||
// Handling Optional:
|
||||
findUserById(123L)
|
||||
.map(User::getName)
|
||||
.ifPresentOrElse(
|
||||
System.out::println,
|
||||
() -> System.out.println("User not found")
|
||||
);
|
||||
```
|
||||
|
||||
### 2.3. Resource Management with Try-with-resources
|
||||
Always use try-with-resources for any resource that implements `AutoCloseable`.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream("file.txt");
|
||||
// ... read from fis
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
try (FileInputStream fis = new FileInputStream("file.txt")) {
|
||||
// ... read from fis
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading file: " + e.getMessage());
|
||||
}
|
||||
```
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
### 3.1. Efficient String Concatenation
|
||||
Use `StringBuilder` for concatenating multiple strings in loops or when building complex strings.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
String result = "";
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
result += i; // Creates many intermediate String objects
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
sb.append(i);
|
||||
}
|
||||
String result = sb.toString();
|
||||
```
|
||||
|
||||
### 3.2. Logging Performance
|
||||
Avoid expensive string formatting or method calls in logging statements unless the log level is enabled. Use parameterized logging.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
logger.debug("Processing user: " + user.getName() + " with ID: " + user.getId()); // String concatenation always happens
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
logger.debug("Processing user: {} with ID: {}", user.getName(), user.getId()); // Parameters only evaluated if debug is enabled
|
||||
```
|
||||
|
||||
## 4. Common Pitfalls and Gotchas
|
||||
|
||||
### 4.1. Avoid Mutable Static State
|
||||
Mutable static fields are a common source of concurrency bugs and make testing difficult. Minimize their use.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
public class Counter {
|
||||
public static int count = 0; // Mutable static field
|
||||
public static void increment() { count++; }
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
// Use dependency injection for stateful services or pass state explicitly
|
||||
public class CounterService {
|
||||
private int count = 0; // Instance field
|
||||
public synchronized void increment() { count++; } // Thread-safe instance method
|
||||
public int getCount() { return count; }
|
||||
}
|
||||
```
|
||||
|
||||
### 4.2. Dependency Version Management
|
||||
Always pin dependency versions. Use Bill-of-Materials (BOMs) for managing compatible versions across related libraries (e.g., Spring Boot BOM, Google Cloud Libraries BOM).
|
||||
|
||||
❌ BAD (Maven)
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<!-- No version specified, relies on parent or latest -->
|
||||
</dependency>
|
||||
```
|
||||
|
||||
✅ GOOD (Maven with BOM)
|
||||
```xml
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>3.2.5</version> <!-- Pin BOM version -->
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<!-- Version inherited from BOM -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
### 4.3. Correct `equals()` and `hashCode()` Implementation
|
||||
Always override both `equals()` and `hashCode()` together, or neither. Use `Objects.equals()` and `Objects.hash()` for robust implementations. For data classes, `record` types handle this automatically.
|
||||
|
||||
❌ BAD
|
||||
```java
|
||||
public class Point {
|
||||
private int x, y;
|
||||
// Only equals() overridden, or incorrectly
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Point point = (Point) o;
|
||||
return x == point.x && y == point.y;
|
||||
}
|
||||
// Missing hashCode()
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD
|
||||
```java
|
||||
import java.util.Objects;
|
||||
|
||||
public final class Point { // Make final if not intended for extension
|
||||
private final int x, y;
|
||||
public Point(int x, int y) { this.x = x; this.y = y; }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Point point = (Point) o;
|
||||
return
|
||||
Reference in New Issue
Block a user