SA0031 : Avoid GOTO statement to improve readability

The topic describes the SA0031 analysis rule.

Message

Avoid GOTO statement to improve readability

Description

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.

How to fix

Review the code that uses GOTO statement, and consider refactoring it to use other control of flow statements, which reduce code readability less.

Scope

The rule has a Batch scope and is applied only on the SQL script.

Parameters

Rule has no parameters.

Remarks

The rule does not need Analysis Context or SQL Connection.

Effort To Fix
1 hour per issue.
Categories

Design Rules, Bugs

Additional Information

There is no additional info for this rule.

Example Test SQL
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.'

Analysis Results
  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
See Also

Other Resources