Home

CASE structures should not have too many CASE clauses

Description

    The "CASE structures should not have too many CASE clauses" rule states that when using CASE structures in PowerBuilder code, the number of CASE clauses should be kept to a manageable amount. This is because too many CASE clauses can make the code difficult to read and understand, and can also lead to performance issues. It's best to use other methods to structure the code if possible, such as using IF/ELSE statements. If CASE structures must be used, it's important to keep the number of clauses to a minimum.

Key Benefits

  • Easier to maintain: CASE structures should not have too many CASE clauses as this can make them difficult to maintain and debug.
  • Increased performance: Having fewer CASE clauses can improve the performance of SQL Server queries as it reduces the amount of code that needs to be processed.
  • Easier to read: Fewer CASE clauses makes the code easier to read and understand, which can help to reduce the amount of time spent debugging.

 

Non-compliant Code Example

private function string TestFunctionCall (int cnt)

string sle_message

CHOOSE CASE Real(cnt)

CASE is < 10
		sle_message = " < 10"
CASE 11 to 20
		sle_message = " 11 to 20"
CASE 21 to 30
		sle_message = " 21 to 30"
CASE 31 to 40
		sle_message = " 31 to 40"
CASE 41 to 50
		sle_message = " 41 to 50"
CASE 51 to 60
		sle_message = " 51 to 60"
CASE 61 to 70
		sle_message = " 61 to 70"
CASE 71 to 80
		sle_message = " 71 to 80"
CASE 81 to 90
		sle_message = " 81 to 90"
CASE 91 to 100
		sle_message = " 91 to 100"
CASE is > 200
		sle_message = " > 200"   //Non compliant code (Allowed less than 10 number of case statements)
CASE ELSE
		sle_message = "Cannot evaluate!"  
END CHOOSE

return ""

end function

Compliant Code Example

private function string TestFunctionCall (int cnt)

string sle_message

CHOOSE CASE Real(cnt)

CASE is < 10
		sle_message = " < 10"
CASE 11 to 20
		sle_message = " 11 to 20"
CASE 21 to 30
		sle_message = " 21 to 30"
CASE 31 to 40
		sle_message = " 31 to 40"
CASE 41 to 50
		sle_message = " 41 to 50"
CASE 51 to 60
		sle_message = " 51 to 60"
CASE 61 to 70
		sle_message = " 61 to 70"
CASE ELSE
        sle_message = " > 70"   //Compliant code (Case statements are below allowed limit of 10)
END CHOOSE

return ""

end function
Visual Expert 2024
 VEPBRULE48