Click or drag to resize

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.

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.

Categories

Design Rules

Additional Information

There is no additional info for this rule.

Example Test Script
 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

 MessageLineColumn
1SA0031 : Avoid GOTO statement to improve readability.720
2SA0031 : Avoid GOTO statement to improve readability.820
3SA0031 : Avoid GOTO statement to improve readability.124
See Also

Other Resources