Home

Collapsible if statements should be merged

Description

    The "Collapsible if statements should be merged" code rule states that when multiple if statements are logically connected, they should be merged into a single if statement to improve readability and maintainability of the code. This helps to reduce the complexity of the code and make it easier to understand what the code is doing. Additionally, merging the if statements can help to reduce the number of lines of code, making it easier to debug and maintain.

Key Benefits

  • Reduced code complexity: By merging collapsible if statements, code complexity can be reduced, making it easier to read and understand.
  • Eliminates code duplication: By merging collapsible if statements, code duplication can be eliminated, resulting in more efficient and manageable code.
  • Improved performance: By merging collapsible if statements, performance can be improved, resulting in faster execution of code.

 

Non-compliant Code Example

DECLARE @Number int;  
SET @Number = 50;  
IF @Number > 100  
    IF @Number < 200       --Non compliant code (Two if condition we can be merged)
        PRINT 'The number is large.';  
ELSE   
   BEGIN  
      IF @Number < 10  
        PRINT 'The number is small.';  
      ELSE  
        PRINT 'The number is medium.';  
   END ;  
GO  

Compliant Code Example

DECLARE @Number int;  
SET @Number = 50;  
IF @Number > 100  AND @Number < 200       --Compliant code
        PRINT 'The number is large.';  
ELSE   
   BEGIN  
      IF @Number < 10  
        PRINT 'The number is small.';  
      ELSE  
        PRINT 'The number is medium.';  
   END ;  
GO  
Visual Expert 2024
 VETSQLRULE43