Skip to content

fix: Define a constant instead of duplicating this literal "classpath:/" 4 ti#1

Open
ciadoh wants to merge 1 commit into
mainfrom
sonar-fix/a19cb449-33a4-47ee-88c1-7d8c610db92d-1782125924
Open

fix: Define a constant instead of duplicating this literal "classpath:/" 4 ti#1
ciadoh wants to merge 1 commit into
mainfrom
sonar-fix/a19cb449-33a4-47ee-88c1-7d8c610db92d-1782125924

Conversation

@ciadoh

@ciadoh ciadoh commented Jun 22, 2026

Copy link
Copy Markdown
Owner

SonarQube Issue

Key: a19cb449-33a4-47ee-88c1-7d8c610db92d
File: src/main/java/org/owasp/webgoat/container/AsciiDoctorTemplateResolver.java

AI Analysis & Fix

1. Explanation of the Issue

The issue flagged by SonarQube (java:S1192) indicates that the literal string "classpath:/" is being duplicated four times in the code. This is considered a code smell because it violates the DRY (Don't Repeat Yourself) principle. By repeating the same literal multiple times, you increase the risk of inconsistencies if you need to change the string in the future. It also makes the code less maintainable and harder to read.

2. Concrete Fix

To resolve this issue, you should define a constant for the string "classpath:/". This way, if you need to change the string in the future, you only have to update it in one place.

Here's a short code example:

Before:

public class AsciiDoctorTemplateResolver {

    public String resolveTemplate1() {
        return "classpath:/template1.xml";
    }

    public String resolveTemplate2() {
        return "classpath:/template2.xml";
    }

    public String resolveTemplate3() {
        return "classpath:/template3.xml";
    }

    public String resolveTemplate4() {
        return "classpath:/template4.xml";
    }
}

After:

public class AsciiDoctorTemplateResolver {

    private static final String PREFIX = "classpath:/";

    public String resolveTemplate1() {
        return PREFIX + "template1.xml";
    }

    public String resolveTemplate2() {
        return PREFIX + "template2.xml";
    }

    public String resolveTemplate3() {
        return PREFIX + "template3.xml";
    }

    public String resolveTemplate4() {
        return PREFIX + "template4.xml";
    }
}

3. Caveats or Edge Cases

  • Immutability: Ensure that the constant is immutable (i.e., use final in Java). This prevents accidental changes to the constant value.
  • Thread Safety: If the constant is being used in a multi-threaded environment, ensure that the way it is used does not introduce race conditions.
  • Refactoring: When refactoring, make sure that all occurrences of the literal are replaced with the constant to avoid inconsistencies.
  • Localization: If the string is subject to localization, consider using resource bundles instead of constants.

Generated by TechDebt AI

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 this pull request may close these issues.

1 participant