Home
All branches in a conditional structure should not have exactly the same implementation
Description
The rule "All branches in a conditional structure should not have exactly the same implementation" states that when a conditional structure is used in SQL Server code, the branches should not have identical implementations. This means that each branch should have a unique implementation that is tailored to the specific conditions of that branch. This helps to ensure that the code is efficient and that it produces the desired results. Additionally, this rule helps to prevent potential errors or unexpected behavior that could occur if the same implementation were used for all branches.
Key Benefits
- Eliminates redundant code: By ensuring that each branch in a conditional structure has a different implementation, code duplication is avoided.
- Simplifies maintenance: When a change is required, it can be made in a single place, rather than having to update multiple sections of code.
- Increases readability: By separating out the different implementations, the code becomes more organized and easier to read.
- Ensures consistency: By adhering to this rule, it ensures that all branches of a conditional structure are treated in the same way.
Non-compliant Code Example
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 10'
WHEN Quantity > 40 THEN 'The quantity is greater than 10' --Non compliant code (When clause is having same implementation as of above condition)
WHEN Quantity = 30 THEN 'The quantity is greater than 10'
ELSE 'The quantity is greater than 10'
END AS QuantityText
FROM OrderDetails;