SA0183 : The commented out code reduces readability and should be deleted

Commented-out code reduces readability and creates maintenance challenges.

Description

Commented-out code blocks containing valid T-SQL statements lead to reduced readability and present potential issues in database scripts. This can complicate understanding for developers working with SQL Server, as discerning active code from inactive code becomes challenging. Moreover, storing unused SQL code in comments is considered a bad practice.

For example:

SQL
1-- SELECT * FROM Employees WHERE Department = 'Sales';

In the example above, the query is in a comment block. This practice is problematic because it can clutter the codebase and increase complexity in maintenance. Source control should be used to track changes and access historical code instead.

  • It clutters the codebase, making it harder to distinguish active code from inactive code, which impacts documentation and maintenance.

  • Developers may inadvertently maintain or update commented-out code, leading to wasted effort and confusion.

How to fix

Remove commented-out code to improve readability and maintainability in your SQL scripts.

Follow these steps to address the issue:

  1. Identify all commented-out code blocks within the SQL script using a text editor or SQL Server Management Studio (SSMS).

  2. Assess whether the commented-out code is necessary. If the code is required for future use, relocate it to a version control system instead of keeping it in the script.

  3. Delete the commented-out code block from the script to enhance readability. Use or /* */ to comment when providing necessary context or explanations, but avoid storing inactive code.

For example:

SQL
1-- Corrected SQL script without commented-out code
2SELECT Name, Position FROM Employees WHERE Department = 'Sales';

Scope

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

Parameters
Name Description Default Value
MinCommentedBlockLines

The minimum number of lines a commented block, in order to be considered by the rule.

2

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
2 minutes per issue.
Categories

Design Rules, Code Smells

Additional Information

There is no additional info for this rule.

Example Test SQL
SQL
 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!'
 9UNION ALL 
10SELECT 'Hi!'
11UNION ALL
12SELECT 'Hello, world!'
13 -- DROP TABLE Test.Greeting
14INSERT INTO Test.Greeting (Message) 
15VALUES ('How do yo do?'),
16        ('Good morning!'), -- 1
17--2
18--3
19/* 4 
20            5 
21    6
22    7
23            8
249*/
25
26
27        ('Good night!')
28--Delete the steps from the Approval Policy
29DELETE  Test.Greeting WHERE GreetingId = 3
30/*
31SELECT 1 * FROM1 zTest.Greeting g 
32WHERE 
33g.Message like 'Hello%'
34
35DROP TABLE Test.Greeting*/
36

Analysis Results

No violations found.

See Also

Other Resources