Home
Jump statements should not be followed by dead code
Description
The "Jump statements should not be followed by dead code" rule states that any statement that causes the execution to jump to another part of the code, such as a loop or an IF statement, should not be followed by any code that will not be executed. This is because such code is unnecessary and can lead to confusion and errors when debugging or maintaining the code. This rule helps ensure that the code is clean and efficient, and that any code that is written is actually being used.
Key Benefits
- Eliminates confusion: By not allowing dead code after a jump statement, it eliminates confusion and helps maintain code readability.
- Improves maintainability: By not allowing dead code after a jump statement, the maintainability of the code is improved.
- Reduces the size of the code: Dead code is unnecessary and can be removed, thus reducing the size of the code.
- Improves performance: By removing dead code, the performance of the code is improved.
Non-compliant Code Example
CREATE PROCEDURE checkstate @param varchar(11)
AS
BEGIN
IF (SELECT StateProvince FROM [Data].AdditionalContactInfo WHERE ContactID = @param) = 'WA'
BEGIN
Select [state]
From StateRecord
Where name = @param;
END
RETURN;
Print 'Checkstate procedure ends'; --Non compliant code (Print statement won't get execute, it is a dead code)
END;
GO
Compliant Code Example
CREATE PROCEDURE checkstate @param varchar(11)
AS
BEGIN
IF (SELECT StateProvince FROM [Data].AdditionalContactInfo WHERE ContactID = @param) = 'WA'
BEGIN
Select [state]
From StateRecord
Where name = @param;
END
Print 'Checkstate procedure ends'; --Compliant code
RETURN;
END;
GO