PowerBuilder
Related IF/ELSIF statements and WHEN clauses in a CASE should not have the same condition
Description
This rule states that when using IF/ELSIF statements and WHEN clauses in a CASE statement, they should not have the same condition. This is because the conditions should be unique to each statement or clause, as having the same condition could result in an infinite loop or other unintended behavior. This rule helps ensure that the code is structured properly and that it runs as expected.
Key Benefits
- Eliminates Confusion: By preventing the same condition from being used in IF/ELSIF statements and WHEN clauses in a CASE statement, the rule eliminates confusion and potential errors.
- Easier to Read: By separating the conditions between IF/ELSIF statements and WHEN clauses in a CASE statement, the code is easier to read and understand.
- More Flexibility: By separating the conditions between IF/ELSIF statements and WHEN clauses in a CASE statement, the code allows for more flexibility and customization.
Non-compliant Code Example
function string TestFunctionCall (string cnt) if cnt = 1 then cnt = 'A'; elseif cnt = 1 then //Non compliant code (If and ElseIf clause is having same condition) cnt = 'B'; else cnt = 'C'; end if CHOOSE CASE Real(cnt) CASE 11 to 20 sle_message = " is < 10" CASE 11 to 20 //Non compliant code sle_message = "is 11 to 20" END CHOOSE return cnt end function
Compliant Code Example
function string TestFunctionCall (string cnt)
if cnt = 1 then
cnt = 'A';
elseif cnt = 2 then //Compliant code
cnt = 'B';
else
cnt = 'C';
end if
return cnt
end function