This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Why Java architecture is allergic to "switch-case"?

Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
What is the exact reason for the avoidance of switch case statements in Java atleast at the High Level Design stage.
is the reason mainly attributed to
1. Need for factoring and object-orientation?
2. Usage of "switch-case" is having any performance issues?
The famous recommendation whenever "swich-case" is encountered for say executing a function based on the "switch" value is to use Command Pattern.
Will this not result in introduction of too many classes?
Please clarify....
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
switch case doesn't work on objects. so it is real 'C style' coding.
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual OO recommendation is to replace switch statements by polymorphism. The Command pattern is one way you can achieve this -- often, you can eliminate it by simply implementing a polymorphic method on existing classes; in yet other cases, double dispatch is a better idea; and finally there are situations in which eliminating a switch would make the code less flexible and clear rather than more (your class bloat concern). It really depends.
As usual, the C2 Wiki has some delightful discussions arguing that switch statements are considered harmful and that they smell too. Presumably they never brush their teeth or clean the bath
- Peter
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason to avoid switch statements is that when you add a new condition, you have to go into the switch case statement and add code. Most of the other solutions such as command and double-dispatch try to make the code more stable and extensible. That is, you can add new stuff to the system without recompiling all of it. This is important because every modification is a risk of error, and you might not even have the source code.
Here's a neat paper on dependencies (that I have recommended about a thousand times lately)
It doesn't cover switch statements in depth, but shows how cool it can be to make code open for extension but closed for modification.
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!