Minor

Jump statements should not be redundant

Description

    The "Jump statements should not be redundant" rule states that SQL Server code should not contain redundant jump statements. Jump statements are commands that cause an immediate exit from a loop or block of code. Examples of jump statements include "BREAK", "GOTO", and "RETURN". Redundant jump statements are those that are unnecessary, as they do not add any value to the code and can lead to confusion. The use of redundant jump statements should be avoided in SQL Server code, as they can lead to unexpected behavior and can make the code difficult to understand.

Key Benefits

  • Maintainability: By avoiding redundant jump statements, code is easier to read and maintain.
  • Performance: Redundant jump statements can lead to longer execution times.
  • Robustness: Redundant jump statements can lead to unexpected behavior and errors.

 

Non-compliant Code Example

WHILE ( SELECT AVG(ListPrice) FROM dbo.DimProduct) < $300  
BEGIN  
    UPDATE dbo.DimProduct  
        SET ListPrice = ListPrice * 2;  
    Continue;     --Non compliant code (Redundant jump statement in while loop)
    SELECT MAX ( ListPrice) FROM dbo.DimProduct  
END 

Compliant Code Example

WHILE ( SELECT AVG(ListPrice) FROM dbo.DimProduct) < $300  
BEGIN  
    UPDATE dbo.DimProduct  
        SET ListPrice = ListPrice * 2;  
    SELECT MAX ( ListPrice) FROM dbo.DimProduct  
    IF ( SELECT MAX (ListPrice) FROM dbo.DimProduct) > $500   --Compliant code (Loop break only when particular condition achieved.)
        BREAK;  
END 
Visual Expert 2023
 VETSQLRULE49