Home
CASE should be used for sequences of simple tests
Description
The "CASE should be used for sequences of simple tests" PowerBuilder code rule states that the CASE statement should be used when there are multiple, simple tests that need to be performed, instead of using a series of IF statements. The CASE statement is more efficient and easier to read than multiple IF statements, as it allows for a single expression to be evaluated and compared against multiple values, with each value having its own corresponding action. It also allows for a default action to be taken if none of the values match the expression.
Key Benefits
- Ease of Use: CASE allows for sequences of simple tests to be quickly and easily created.
- Cost Savings: CASE can help to reduce costs by eliminating the need for manual testing.
- Flexibility: CASE allows for tests to be easily modified and adapted to changing requirements.
- Organization: CASE provides an organized approach to testing, allowing for easy tracking and reporting of results.
Non-compliant Code Example
function string TestFunctionCall (string cnt, int x )
IF x = 0 THEN //Non compliant code (Instead of IF and ELSEIF use Case clause)
cnt = "x = 0";
ELSEIF x = 1 THEN
cnt = "x = 1";
ELSE
cnt = "else";
END IF;
return cnt
end function
Compliant Code Example
function string TestFunctionCall (string cnt, int x ) CHOOSE CASE Real(x) CASE x = 0 cnt = "x = 0" CASE x = 1 cnt = "x = 1" CASE Else cnt = "x = else" END CHOOSE return cnt end function