Back to all reviewers

DRY class hierarchies

maplibre/maplibre-native
Based on 3 comments
Java

Follow the Don't Repeat Yourself (DRY) principle when designing class hierarchies. Extract common functionality into base classes and avoid duplicating state or methods in subclasses. When extending a class, delegate to parent implementations rather than redefining functionality.

Code Style Java

Reviewer Prompt

Follow the Don’t Repeat Yourself (DRY) principle when designing class hierarchies. Extract common functionality into base classes and avoid duplicating state or methods in subclasses. When extending a class, delegate to parent implementations rather than redefining functionality.

Examples of issues to avoid:

// AVOID: Duplicating state in subclass
public class TransformAuto extends Transform {
    @Nullable
    private CameraPosition cameraPosition; // Duplicate of parent state
    
    @UiThread
    public CameraPosition getCameraPosition() {
        if (cameraPosition == null) {
            cameraPosition = invalidateCameraPosition();
        }
        return cameraPosition;
    }
}

// BETTER: Delegate to parent
public class TransformAuto extends Transform {
    // No duplicate cameraPosition variable
    
    @UiThread
    public CameraPosition getCameraPosition() {
        return super.getCameraPosition(); // Delegate to parent
    }
}

For similar implementations across different technologies (e.g., GL and Vulkan renderers), extract shared code into common base classes. This improves maintainability and prevents bugs from inconsistent implementations.

3
Comments Analyzed
Java
Primary Language
Code Style
Category

Source Discussions