Why this matters
Duplicated string literals make refactoring harder and increase the risk of inconsistencies in the codebase.
Store repeated string literals in constants or variables to improve maintainability and reduce redundancy.
Duplicated string literals make refactoring harder and increase the risk of inconsistencies in the codebase.
Side-by-side examples engineers can pattern-match during review.
class A {
void run() {
prepare('string literal'); // Noncompliant - "string literal" is duplicated 3 times
execute('string literal');
release('string literal');
}
}class A {
static const _const = 'string literal';
void run() {
prepare(_const); // Compliant
execute(_const);
release(_const);
}
}class A {
void run() {
prepare('string literal'); // Noncompliant - "string literal" is duplicated 3 times
execute('string literal');
release('string literal');
}
}class A {
static const _const = 'string literal';
void run() {
prepare(_const); // Compliant
execute(_const);
release(_const);
}
}From the same buckets as this rule.