Home
Two branches in a conditional structure should not have exactly the same implementation
Description
This rule states that when a conditional structure is used, the two branches should not have the same implementation. This means that the code executed in each branch should be different. This helps to ensure that the code is more organized and readable, and that the code runs as expected.
Key Benefits
- Reduced Complexity : By avoiding having two branches with the same implementation, the overall complexity of the conditional structure is reduced.
- Easier Maintenance : With only one implementation for each branch, maintenance and debugging of the conditional structure is made simpler.
- Improved Performance : By avoiding having the same implementation for both branches, SQL Server can optimize the conditional structure for improved performance.
Non-compliant Code Example
function string CallFunctionTest (string cnt)
string str;
if cnt = 1 then
cnt = 'B';
str = 'A';
elseif cnt = 2 then
cnt = 'B'; //Non compliant code (IF clause and ELSEIF clause is having exactly the same implementation )
str = 'A';
else
cnt = 'C';
str = 'A';
end if
return cnt
end function
Compliant Code Example
function string CallFunctionTest (string cnt)
string str;
if cnt = 1 OR cnt = 2 then //Compliant code
cnt = 'B';
str = 'A';
else
cnt = 'C';
str = 'A';
end if
return cnt
end function