Prompt
Establish consistent null handling patterns at API boundaries to prevent null pointer exceptions and improve code clarity:
- Validate method parameters using explicit null checks:
public void setHttpClient(HttpClient httpClient) { Assert.notNull(httpClient, "HttpClient must not be null"); this.httpClient = httpClient; } - Return empty collections instead of null:
public List<PropertyAccessor> getPropertyAccessors() { return Collections.emptyList(); // Instead of returning null } - Use Optional only as a return type, never as a field:
```java
// DON’T
private Optional
errorStatus; // Avoid Optional as field
// DO
public Optional
4. For nullable return values in internal APIs, prefer explicit @Nullable annotation over Optional:
```java
@Nullable
protected Class<?> getReturnType(Method method) {
// ...
}
These patterns ensure consistent null handling, improve code readability, and reduce the likelihood of null-related bugs. They also help maintain clear contracts between components while avoiding unnecessary Optional usage.