Skip to content

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

Commented-out code reduces readability and creates maintenance challenges.

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:

-- 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.

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:

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

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

Name Description Default Value
MinCommentedBlockLines The minimum number of lines a commented block, in order to be considered by the rule. 2

The rule does not need Analysis Context or SQL Connection.

2 minutes per issue.

Design Rules, Code Smells

There is no additional info for this rule.

CREATE TABLE Test.Greeting
(
GreetingId INT IDENTITY (1,1) PRIMARY KEY,
Message nvarchar(255) NOT NULL,
)
INSERT INTO Test.Greeting (Message)
SELECT 'Hello!'
UNION ALL
SELECT 'Hi!'
UNION ALL
SELECT 'Hello, world!'
-- DROP TABLE Test.Greeting
INSERT INTO Test.Greeting (Message)
VALUES ('How do yo do?'),
('Good morning!'), -- 1
--2
--3
/* 4
5
6
7
8
9
*/
('Good night!')
--Delete the steps from the Approval Policy
DELETE Test.Greeting WHERE GreetingId = 3
/*
SELECT 1 * FROM1 zTest.Greeting g
WHERE
g.Message like 'Hello%'
DROP TABLE Test.Greeting
*/

No violations found.

Analysis Rules