Skip to main content

Define a constant for a literal string that is duplicated n times (Sonar)

sonar:java/define-constant-for-duplicate-literal-s1192​

ImportanceReview GuidanceRequires Scanning Tool
HIGHMerge After ReviewYes (Sonar)

This change defines a constant for duplicated literal expression values, simplifying the refactoring process and mitigating the risk of overlooking any values during updates.

Our changes look something like this:


+ private static final String EXCEPTION_AT = "Exception at";

- LOG.error("Exception at", ex)
+ builder.add(EXCEPTION_AT)

- LOG.error("Exception at", ex)
+ builder.add(EXCEPTION_AT)

- LOG.error("Exception at", ex)
+ builder.add(EXCEPTION_AT)

F.A.Q.​

Why is this codemod marked as Merge After Review?​

This modification is intended to introduce no functional alterations. Nevertheless, we believe it would be beneficial for a developer to review the newly defined constant names to ensure they align with their expectations.

References​