Skip to content

Enforce Noninstantiability in Utility Class with Private Constructor #3265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ori0o0p opened this issue Apr 18, 2025 · 0 comments · May be fixed by #3266
Open

Enforce Noninstantiability in Utility Class with Private Constructor #3265

ori0o0p opened this issue Apr 18, 2025 · 0 comments · May be fixed by #3266

Comments

@ori0o0p
Copy link
Contributor

ori0o0p commented Apr 18, 2025

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.

@ori0o0p ori0o0p changed the title Enforce Noninstantiability in LettuceClassUtils with Private Constructor Enforce Noninstantiability in Utils Class with Private Constructor Apr 18, 2025
@ori0o0p ori0o0p changed the title Enforce Noninstantiability in Utils Class with Private Constructor Enforce Noninstantiability in Utility Class with Private Constructor Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant