Home

Expressions should not be too complex

Description

    The "Expressions should not be too complex" rule is a best practice for writing SQL Server code. It suggests that developers should avoid writing complex expressions that are hard to read and understand. Such expressions can lead to unexpected results and can cause performance issues. This rule encourages developers to break down complex expressions into simpler parts to make them easier to read and understand. Additionally, it encourages developers to use simpler expressions that are more efficient and easier to debug. Following this rule can help ensure that the code is readable and maintainable, and that it performs well.

Key Benefits

  • Reduced complexity: Expressions should not be too complex, as this can lead to confusion and errors.
  • Improved readability: Keeping expressions simple makes them easier to read and understand.
  • Increased efficiency: Complex expressions can take longer to execute, resulting in slower performance.

 

Non-compliant Code Example

DECLARE @id int;
DECLARE @Number1 int; 
DECLARE @Number2 int; 
DECLARE @Number3 int; 
DECLARE @Number4 int; 

SET @Number1 = 50;  
SET @Number2 = 150;  
SET @Number3 = 500;  
SET @Number4 = 950;  

IF (@Number4 > 100 and @Number1 < 100) AND
        (@Number2 > 100 and @Number3 < 400) OR
        (@Number4 > 100 and @Number2 < 200)   --Non compliant code
    Select * From Employee Where id < @id;
    PRINT 'The number is less then 100.';
Visual Expert 2024
 VETSQLRULE66