SA0031 : Avoid GOTO statement to improve readability |
The topic describes the SA0031 analysis rule.
Avoid GOTO statement to improve readability
Use of the GOTO statement is generally considered to be poor programming practice and is not recommended.
Extensive use of GOTO tends to lead to unreadable code especially when procedures grow long. In most of the cases use of the GOTO statement is not necessary because there are better statements available to control the execution path. There are no specific situations that require the use of GOTO; instead it is more often used for convenience.
Review the code that uses GOTO statement, and consider refactoring it to use other control of flow statements, which reduce code readability less.
The rule has a Batch scope and is applied only on the SQL script.
Rule has no parameters.
The rule does not need Analysis Context or SQL Connection.
Design Rules, Bugs
There is no additional info for this rule.
SQL
1DECLARE @Counter int; 2SET @Counter = 1; 3WHILE @Counter < 10 4BEGIN 5 SELECT @Counter 6 SET @Counter = @Counter + 1 7 IF @Counter = 4 GOTO Branch_One -- Jumps to the first branch. 8 IF @Counter = 5 GOTO Branch_Two -- This will never execute. 9END 10Branch_One: 11 SELECT 'Jumping To Branch One.' 12 GOTO Branch_Three; --This will prevent Branch_Two from executing. 13Branch_Two: 14 SELECT 'Jumping To Branch Two.' 15Branch_Three: 16 SELECT 'Jumping To Branch Three.' |
Message | Line | Column | |
---|---|---|---|
1 | SA0031 : Avoid GOTO statement to improve readability. | 7 | 20 |
2 | SA0031 : Avoid GOTO statement to improve readability. | 8 | 20 |
3 | SA0031 : Avoid GOTO statement to improve readability. | 12 | 4 |