About Me

My photo
Bangalore, Karnataka, India

Sunday, March 28, 2010

Most neglected truth about SWITCH CASE

Many of a times, I have come across the code where if/else used instead of the switch case. Its not that annoying for readability but many of us actually forget the truth that switch case statements generate jump tables which are faster while executing compared to if/else. I agree that we get very minimal efficiency compared to other improvements but we get some improvement in many of the cases. If the case statements are more dense then the effect more. Java allows integer types (see java for more info on integer types. even char is integer type) and enums to be used in switch case. From now onwards we should keep in mind that use switch/case where ever possible, and avoid using default case if possible for more efficiency. This will beautify the code also.

This comment applies to other languages like C/C++ also.

No comments:

Post a Comment