Home
Control flow statements IF, WHILE and TRY should not be nested too deeply
Description
This code rule states that control flow statements such as IF, WHILE and TRY should not be nested too deeply. This means that when writing code, it is important to keep the number of nested control flow statements to a minimum. Too many nested control flow statements can make code difficult to read and debug, and can lead to performance issues. It is therefore important to keep the number of nested control flow statements to a reasonable level.
Key Benefits
- Preventing errors: By avoiding deep nesting of IF, WHILE and TRY statements, you can reduce the chances of introducing errors into your code.
- Improving readability: Nested IF, WHILE and TRY statements can quickly become difficult to read and understand. Keeping nesting shallow helps make your code more readable.
- Faster execution: Shallow nesting can also help speed up the execution of your code, as the computer won't have to process as many instructions.
Non-compliant Code Example
DECLARE @Number int; SET @Number = 50; IF @Number < 10 PRINT 'The number is less than 10.'; ELSE IF @Number < 20 PRINT 'The number is less than 20.'; ELSE IF @Number < 30 PRINT 'The number is less than 30.'; ELSE IF @Number < 40 PRINT 'The number is less than 40.'; ELSE IF @Number < 50 PRINT 'The number is less than 50.'; --Non compliant code (Control flow statements are nested beyond default defined limit 4) ELSE IF @Number < 60 PRINT 'The number is less than 60.'; --Non compliant code (Control flow statements are nested beyond default defined limit 4) GO