SA0184 : Redundant pairs of parentheses can be removed
Introduction
Section titled “Introduction”Redundant parentheses in SQL expressions can hinder readability and maintainability, potentially leading to confusion in query interpretation.
Description
Section titled “Description”Redundant parentheses in SQL expressions can be misleading. Although they often do not affect the execution of queries, they can make the code harder to read and maintain, leading to confusion. This is particularly relevant in SQL Server, where query readability is vital for ongoing maintenance and collaboration among developers.
For example:
-- Example of a query with redundant parenthesesSELECT ((column1 + column2) * column3) FROM TableName;In the above example, the outer parentheses around the arithmetic operation are unnecessary and do not change the evaluation order. This can obscure the logic of the query, especially for complex calculations or conditions.
-
Redundant parentheses can make the query seem more complex than it is, potentially leading to misunderstanding.
-
Unnecessary parentheses may hinder performance tuning and understanding of the intended logic, especially in large and complex queries.
How to fix
Section titled “How to fix”Eliminate redundant parentheses from logical and arithmetic expressions to enhance readability and maintainability of T-SQL queries.Follow these steps to address the issue:Carefully review the query and identify any sets of parentheses that do not influence the logical or arithmetic operations within the expression. Look for expressions like ((column1 + column2) * column3).Remove the unnecessary parentheses while ensuring that the logical order of operations remains unchanged. For the above expression, this would mean simplifying it to (column1 + column2) * column3.Test the query after modifications to confirm that it produces the same results and maintains the intended logic. This ensures there are no side effects from the removal of parentheses.For example: -- Example of a corrected query SELECT (column1 + column2) * column3 FROM TableName;
The rule has a Batch scope and is applied only on the SQL script.
Parameters
Section titled “Parameters”| Name | Description | Default Value |
|---|---|---|
| IgnoreArithmeticExpressionMainParentheses | Specifies whether the main parentheses, which wrap an arithmetic expression to be ignored. | yes |
| IgnoreBooleanExpressionMainParentheses | Specifies whether the main parentheses, which wrap an logical expression to be ignored. | yes |
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, world!')
INSERT INTO Test.Greeting (Message)VALUES ('How do yo do?'), ('Good morning!'), ('Good night!')
DELETE Test.Greeting WHERE (GreetingId = 3 and (aaa != 0 and ttt = 12) and a <1) or a = 1
select (6/(7/5)*12) + 1select ((1 + 2) - 3) * 4 / 5 -(6 /(7/5)*12 )
SELECT lower('AA'),(g.col1 + (g.col2)) FROM Test.Greeting gWHEREg.Message like ('Hello%')or g.Message in (((((select message from UserMessges)))))
DROP TABLE Test.Greetingdeclare @a int = 5
DECLARE @b INTset @b = (@a / 2 + 1);IF (@b > 0) AND ((@b+@a > 0))BEGIN print '1'END
set @b = (@a / 3 + 1);IF (@b > 0)BEGIN print '2'END
IF (@b > 0) AND (@b-@a > 0) AND (@b-@a) > 0BEGIN print '2'ENDExample Test SQL with Automatic Fix
Section titled “Example Test SQL with Automatic Fix”CREATE TABLE Test.Greeting(GreetingId INT IDENTITY (1,1) PRIMARY KEY,Message nvarchar(255) NOT NULL,)
INSERT INTO Test.Greeting (Message)SELECT ('Hello, world!')
INSERT INTO Test.Greeting (Message)VALUES ('How do yo do?'), ('Good morning!'), ('Good night!')
DELETE Test.Greeting WHERE (GreetingId = 3 and aaa != 0 and ttt = 12 and a <1) or a = 1
select (6/ 7/5*12) + 1select ( 1 + 2 - 3) * 4 / 5 -(6 / 7/5*12 )
SELECT lower('AA'),(g.col1 + g.col2) FROM Test.Greeting gWHEREg.Message like 'Hello%'or g.Message in ( (((select message from UserMessges))))
DROP TABLE Test.Greetingdeclare @a int = 5
DECLARE @b INTset @b = (@a / 2 + 1);IF (@b > 0) AND ( @b+@a > 0)BEGIN print '1'END
set @b = (@a / 3 + 1);IF (@b > 0)BEGIN print '2'END
IF (@b > 0) AND (@b-@a > 0) AND (@b-@a) > 0BEGIN print '2'ENDAnalysis Results
Section titled “Analysis Results”| Message | Line | Column | |
|---|---|---|---|
| 1 | SA0184 : Redundant pairs of parentheses can be removed. | 15 | 48 |
| 2 | SA0184 : Redundant pairs of parentheses can be removed. | 17 | 10 |
| 3 | SA0184 : Redundant pairs of parentheses can be removed. | 18 | 8 |
| 4 | SA0184 : Redundant pairs of parentheses can be removed. | 18 | 35 |
| 5 | SA0184 : Redundant pairs of parentheses can be removed. | 22 | 15 |
| 6 | SA0184 : Redundant pairs of parentheses can be removed. | 20 | 29 |
| 7 | SA0184 : Redundant pairs of parentheses can be removed. | 23 | 17 |
| 8 | SA0184 : Redundant pairs of parentheses can be removed. | 30 | 17 |