Home
Identical expressions should not be used on both sides of a binary operator
Description
This rule states that when using a binary operator (such as an equals sign or a less than sign) the same expression should not be used on both sides of the operator. For example, in the following statement:
x = x
The expression "x" is used on both sides of the equals sign, which is not allowed according to this rule. Instead, the statement should be written as:
x = 5
or some other expression that is not the same on both sides of the equals sign.
Key Benefits
- Eliminates potential errors - By avoiding the use of identical expressions on both sides of a binary operator, potential errors caused by mismatched expressions are eliminated.
- Improves readability - Using different expressions on either side of a binary operator makes it easier to read and understand code.
- Makes code more concise - By avoiding the use of unnecessary and redundant expressions, code can be made more concise and efficient.
Non-compliant Code Example
DECLARE @MyProduct int;
SET @MyProduct = 750;
IF (@MyProduct <> 500)
SELECT ProductID, Name, ProductNumber
FROM Production.Product
WHERE ProductID = @MyProduct AND ProductID = @MyProduct; --Non compliant code (Identical expressions is used on the both side of binary operator)