SA0184 : Redundant pairs of parentheses can be removed

Redundant parentheses in SQL expressions can hinder readability and maintainability, potentially leading to confusion in query interpretation.

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:

SQL
1-- Example of a query with redundant parentheses
2SELECT ((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
[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:@@_SHFB_2]
Scope

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

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

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, world!')
 9
10INSERT INTO Test.Greeting (Message) 
11VALUES ('How do yo do?'),
12        ('Good morning!'),
13        ('Good night!')
14
15DELETE  Test.Greeting WHERE (GreetingId = 3 and (aaa != 0 and ttt = 12) and a <1) or a = 1
16
17select (6/(7/5)*12) + 1
18select ((1 + 2) - 3) * 4  / 5 -(6 /(7/5)*12 ) 
19
20SELECT lower('AA'),(g.col1 + (g.col2)) FROM Test.Greeting g 
21WHERE 
22g.Message like ('Hello%')
23or g.Message in (((((select message from UserMessges)))))
24
25DROP TABLE Test.Greeting
26declare @a int = 5
27
28DECLARE @b INT 
29set @b = (@a / 2 + 1);  
30IF (@b > 0) AND ((@b+@a > 0)) 
31BEGIN
32 print '1'
33END
34
35set @b   = (@a / 3 + 1);
36IF (@b > 0)
37BEGIN
38 print '2'
39END
40
41IF (@b > 0) AND (@b-@a > 0) AND (@b-@a) > 0
42BEGIN
43 print '2'
44END

Example Test SQL with Automatic Fix
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, world!')
 9
10INSERT INTO Test.Greeting (Message) 
11VALUES ('How do yo do?'),
12        ('Good morning!'),
13        ('Good night!')
14
15DELETE  Test.Greeting WHERE (GreetingId = 3 and aaa != 0 and ttt = 12 and a <1) or a = 1
16
17select (6/ 7/5*12) + 1
18select ( 1 + 2 - 3) * 4  / 5 -(6 / 7/5*12 ) 
19
20SELECT lower('AA'),(g.col1 + g.col2) FROM Test.Greeting g 
21WHERE 
22g.Message like 'Hello%'
23or g.Message in ( (((select message from UserMessges))))
24
25DROP TABLE Test.Greeting
26declare @a int = 5
27
28DECLARE @b INT 
29set @b = (@a / 2 + 1);  
30IF (@b > 0) AND ( @b+@a > 0) 
31BEGIN
32 print '1'
33END
34
35set @b   = (@a / 3 + 1);
36IF (@b > 0)
37BEGIN
38 print '2'
39END
40
41IF (@b > 0) AND (@b-@a > 0) AND (@b-@a) > 0
42BEGIN
43 print '2'
44END

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
See Also

Other Resources