Click or drag to resize

SA0187 : Duplicated string literals complicate the refactoring

The topic describes the SA0187 analysis rule.

Message

Duplicated string literals complicate the refactoring

Description

The rule reports more than one usage of a single string literal.

Duplicating the same string literal makes the process of refactoring more difficult and error-prone as all literal occurrences must be updated.

Consider storing the literal value in a variable and referencing this variable instead.

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters
NameDescriptionDefault Value
NumberOfDuplicatesThreshold

Maximum number of allowed duplicates of a string.

3

MinimalStringLength

Minimal length of a string that to be tested for duplicates.

3

Remarks

The rule does not need Analysis Context or SQL Connection.

Categories

Design Rules, New Rules

Additional Information

There is no additional info for this rule.

Example Test Script
 1CREATE TABLE Test.Greeting
 2(
 3GreetingId INT IDENTITY (1,1) PRIMARY KEY,
 4Message nvarchar(255) NOT NULL,
 5)
 6
 7INSERT INTO Test.Greeting (Message) 
 8SELECT 'Hello!','Hello','Hello12','123','123','123','123','123'
 9UNION ALL 
10SELECT 'Hi!'
11UNION ALL
12SELECT 'Hello, world!'
13
14INSERT INTO Test.Greeting (Message) 
15VALUES ('How do yo do?'),
16        ('Good morning!'),
17        ('Good night!')
18
19DELETE  Test.Greeting WHERE GreetingId = 3
20
21SELECT * FROM Test.Greeting g 
22WHERE 
23g.Message like 'Hello%'
24
25DROP TABLE Test.Greeting

Analysis Results

 MessageLineColumn
1SA0187 : The string literal '123' is duplicated 4 times in the current batch. Extract the string into a variable and use the variable instead.834
See Also

Other Resources