Home
Two branches in a conditional structure should not have exactly the same implementation
Description
The "Two branches in a conditional structure should not have exactly the same implementation" rule for SQL Server code states that when a conditional structure is used, such as an "IF/ELSE" statement, the two branches must have different implementations. This rule is important for ensuring that the code is optimized and that redundant code is avoided. The two branches should be distinct and have different implementations to ensure that the code is efficient and maintainable.
Key Benefits
- Eliminates redundant code: By ensuring that two branches in a conditional structure do not have exactly the same implementation, unnecessary code is avoided.
- Improves readability: By avoiding redundant code, the code becomes more readable and easier to understand.
- Enhances maintainability: By ensuring that two branches in a conditional structure do not have exactly the same implementation, the code is easier to maintain and debug.
Non-compliant Code Example
IF @input_number = 100
BEGIN
SET @input_number = 110
SELECT FIRSTNAME FROM Employee_CR ORDER BY FIRSTNAME
END
ELSE IF @input_number = 200 --Non compliant code (Two branches is having similar implementation)
BEGIN
SET @input_number = 110
SELECT FIRSTNAME FROM Employee_CR ORDER BY FIRSTNAME
END
ELSE
BEGIN
SET @input_number = -1
SELECT FIRSTNAME FROM Employee_CR
END
GO