Home

Conditionals should start on new lines

Description

    This rule states that when writing a conditional statement in SQL Server, the condition should start on a new line. This helps to improve readability and make it easier to spot errors. For example, instead of writing:

    IF (condition) THEN

    It should be written as:

    IF
    (condition)
    THEN

Key Benefits

  • Improved readability: Conditional statements should start on new lines to improve readability and make the code easier to understand.
  • Easier to debug: Starting conditionals on new lines makes it easier to debug code, as it allows for easier identification of the statement.
  • Simpler to maintain: Starting conditionals on new lines makes it simpler to maintain code, as it is easier to add or remove conditions.

 

Non-compliant Code Example

DECLARE @Number int;  
SET @Number = 50;  
IF @Number > 100  PRINT 'The number is large.';   --Non compliant code (Conditionals should start on new lines)
ELSE   
   BEGIN  IF @Number < 10   --Non compliant code (Conditionals should start on new lines)
      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  --Compliant code (Conditional is starting in the new line)
    PRINT 'The number is large.';  
ELSE   
    BEGIN  
        IF @Number < 10   --Compliant code (Conditional is starting in the new line)
            PRINT 'The number is small.';  
        ELSE  
            PRINT 'The number is medium.';  
    END ;  
GO 
Visual Expert 2024
 VETSQLRULE10