SA0183 : The commented out code reduces readability and should be deleted
Introduction
Section titled “Introduction”Commented-out code reduces readability and creates maintenance challenges.
Description
Section titled “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:
-- 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
Section titled “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:
-- Corrected SQL script without commented-out codeSELECT Name, Position FROM Employees WHERE Department = 'Sales';The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”| Name | Description | Default Value |
|---|---|---|
| MinCommentedBlockLines | The minimum number of lines a commented block, in order to be considered by the rule. | 2 |
Remarks
Section titled “Remarks”The rule does not need Analysis Context or SQL Connection.
Effort To Fix
Section titled “Effort To Fix”2 minutes per issue.
Categories
Section titled “Categories”Design Rules, Code Smells
Additional Information
Section titled “Additional Information”There is no additional info for this rule.
Example Test SQL
Section titled “Example Test SQL”CREATE TABLE Test.Greeting(GreetingId INT IDENTITY (1,1) PRIMARY KEY,Message nvarchar(255) NOT NULL,)
INSERT INTO Test.Greeting (Message)SELECT 'Hello!'UNION ALLSELECT 'Hi!'UNION ALLSELECT 'Hello, world!' -- DROP TABLE Test.GreetingINSERT INTO Test.Greeting (Message)VALUES ('How do yo do?'), ('Good morning!'), -- 1--2--3/* 4 5 6 7 89
*/ ('Good night!')--Delete the steps from the Approval PolicyDELETE Test.Greeting WHERE GreetingId = 3/*SELECT 1 * FROM1 zTest.Greeting gWHEREg.Message like 'Hello%'
DROP TABLE Test.Greeting*/Analysis Results
Section titled “Analysis Results”No violations found.