Skip to content

Enforce Noninstantiability in Utility Class with Private Constructor #3265

@ori0o0p

Description

@ori0o0p

Feature Request

Is your feature request related to a problem? Please describe

The LettuceClassUtils, ReplicaUtils, ReflectionTestUtils class is a utility class designed to provide static helper methods, but its constructor is currently public. This allows instantiation of the class, which violates the intended usage and Java best practices for utility classes (Effective Java, Item 4: Enforce noninstantiability with a private constructor). Instantiating a utility class can lead to misuse and confusion.

Describe the solution you'd like

Change the Utility Class constructor from public to private to prevent instantiation. Additionally, add Javadoc to the class and constructor to document its purpose and noninstantiable nature. Review other utility classes (e.g., LettuceStrings, LettuceAssert) to ensure consistent use of private constructors across the project.

Proposed changes:

  • Update Utility Class constructor to private.
  • Add a unit test to verify that instantiation is not possible.

Drawbacks:

  • Minimal impact, as this is an internal change with no effect on the public API.
  • Requires review of other utility classes to ensure consistency, which may increase the scope slightly.

Describe alternatives you've considered

  1. Keep the public constructor: This allows potential misuse and does not align with utility class best practices.
  2. Use a static initializer: Less explicit than a private constructor and does not prevent instantiation as effectively.
  3. Make the class abstract: Not suitable, as utility classes should not be extended.

The private constructor approach is the most explicit and aligns with standard Java practices.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions