Jtest logo




Contents  Previous  Next  Index

CODSTA.SMC


Avoid "switch" statements with many "cases"

Description

This rule flags any "switch" statement with many "cases".

"switch" statements with many "case" statements make code difficult to follow. More importantly, switches with many cases often indicate places where polymorphic behavior could better be used to provide different behavior for different types. Note that although the general principle is to avoid many cases in a switch, the actual cutoff point is arbitrary.

Example

 package CODSTA;
 
 class SMC
 {
    public void foo(int i) {
       switch (i) {          // Violation
       case 1:
          break;
       case 2:
          break;
       case 3:
          break;
       case 4:
          break;
       case 5:
          break;
       case 6:
          break;
       case 7:
          break;
       case 8:
          break;
       case 9:
          break;
       case 10:
          break;
       case 11:
          break;
       default:
          break;
       }
    }
 }

Repair

Look for cleaner ways to invoke the alternative behaviors.


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft