Skip to content

Duplicate string literal (DuplicateStringLiteral)

Type Scope Severity Activated
by default
Minutes
to fix
Tags
Code smell BSL
OS
Minor Yes 1 badpractice

Parameters

Name Type Description Default value
allowedNumberCopies Integer Allowed number of copies string literals 2
analyzeFile Boolean Analyze file false
caseSensitive Boolean Case sensitive false
minTextLength Integer Minimum length of a string literal (quoted) 5

Description

It is bad form to use the same string literals multiple times in the same module or method: - it can lead to problems with further maintenance, if necessary, change the value - there is a high probability of missing one of the repetitions - it can be a consequence of "copy-paste" - the developer may have forgotten to change the code after copying a similar block of code.

Features of the implementation of diagnostic

  • Diagnostics with default settings does not respect the case of literal characters - the strings AAAA and AaaA are considered the same.
  • You cannot specify a minimum parsed literal value less than the default. Short service literals are often used, which will generate unnecessary comments. For example: empty string "", selector numbers "1", "0", etc.
  • You cannot reduce the allowed number of repetitions to less than 1, because it makes no practical sense.

Examples

Bad code

Procedure Test(Param)
    Result = "Value";
    If Param = "One" Then
        Result = Result + One("Value");
    Else
        Result = Result + Two("Value");
    EndIf; 
EndProcedure

Сorrected:

Procedure Test(Param)
    Result = "Value";
    If Param = "One" Then
        Result = Result + One(Result);
    Else
        Result = Result + Two(Result);
    EndIf; 
EndProcedure

Bad code

Procedure Test2(Param)
    Result = "Value";
    If Param = "One" Then
        Result = Result + One("Value");
    Else
        Result = Result + Two("Value");
    EndIf; 
EndProcedure

Procedure Test3(Param)
    If Param = "Five" Then
        Result = Result + Five("Value");
    EndIf; 
EndProcedure

Сorrected

Procedure Test2(Param)
    Result = "Value";
    If Param = "One" Then
        Result = Result + One(StringValue());
    Else
        Result = Result + Two(StringValue());
    EndIf; 
EndProcedure

Procedure Test3(Param)
    If Param = "Five" Then
        Result = Result + Five(StringValue());
    EndIf; 
EndProcedure

Function StringValue()
   Return "Value";
EndFunction

Sources

Snippets

Diagnostic ignorance in code

// BSLLS:DuplicateStringLiteral-off
// BSLLS:DuplicateStringLiteral-on

Parameter for config

"DuplicateStringLiteral": {
    "allowedNumberCopies": 2,
    "analyzeFile": false,
    "caseSensitive": false,
    "minTextLength": 5
}