When the same code is duplicated in two or more separate branches of a conditional, it can make the code harder to understand, maintain, and can
potentially introduce bugs if one instance of the code is changed but others are not.
Having two branches in an IF
/ELSIF
chain with the same implementation is at best duplicate code, and at worst a coding
error.
IF param = 1 THEN
sort_order := 0;
column := 'LastName';
ELSIF param = 2 THEN
sort_order := 0;
column := 'LastName'; -- Noncompliant
ELSE
sort_order := 1;
column := 'FirstName';
END IF;
If the same logic is truly needed for both instances, then in an IF
chain they should be combined.
IF param = 1 OR param = 2 THEN
sort_order := 0;
column := 'LastName';
ELSE
sort_order := 1;
column := 'FirstName';
END IF;
Exceptions
Branches in an IF
/ELSIF
chain with implementation that contains a single line of code are ignored.
IF param = 1 THEN
sort_order := 0;
ELSIF param = 2 THEN
sort_order := 1;
ELSE
sort_order := 0; -- No issue, usually this is done on purpose to increase the readability
END IF;
But this exception does not apply to IF
chains without ELSE
-s when all branches have the same single line of code. In
case of IF
chains with ELSE
-s rule S3923 raises a bug.
IF param = 1 THEN -- Noncompliant, this might have been done on purpose but probably not
sort_order := 0;
ELSIF param = 2 THEN
sort_order := 0;
END IF;