aspose file tools*
The moose likes Java in General and the fly likes How to avoid multiple if else Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to avoid multiple if else" Watch "How to avoid multiple if else" New topic
Author

How to avoid multiple if else

Tushar Anandani
Greenhorn

Joined: Aug 25, 2006
Posts: 6
I am having this problem that in my Service Facade for the Application we have 6 else ifs when the max length is 5....is there a work around for it...
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
perhaps, post more details and some code.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14346
    
  22

Who says that the max. number of else if's is 5? This is certainly not a limitation of the Java compiler. Do you have to adhere to some kind of coding standard that imposes this limitation?

IMO, coding standards should only be used as guidelines - if there is a good reason to break a rule, it should be allowed, provided that the reason is documented in a comment, for example.

If you go and invent strange code constructions to avoid breaking the coding standard, you're most likely only going to make the code worse - less readable, less maintainable.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tushar Anandani
Greenhorn

Joined: Aug 25, 2006
Posts: 6
yeah i am using code analyzer as a coding standard tool....actually we have some ids on the basis of which we have to call various methods.So in service facade we choose the id on the basis of client's request.So if's are necessary......is there a solution....???Please help me!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
If the ids are numeric use a switch statement. If they're not numeric, then I think you can put them in an enum and then use a switch statement with the enum, but I've never done the latter so I may be wrong.


Joanne
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Or you could use a Map that has the ids as keys and Command objects as values.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
5 seems a bit arbitrary, but the problem is solved with an appropriate abstraction using a strategy.
Instead of:

Prefer:

You can even write an implementation that uses the if/else to start off, but prefer a "Map-backed" strategy. You will note that above interface method is just one method of a Map<String, Integer> - it is an unfortunate attribute of OO/Java that you have to minimalise by declaring a new type - requirement leak is inherent within the paradigm.
[ August 25, 2006: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Ilja Preuss:
Or you could use a Map that has the ids as keys and Command objects as values.


How does one populate this map?


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Anand Hariharan:


How does one populate this map?




In Java 5, you would of course add the generics type declarations. Does that help?
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Originally posted by Ilja Preuss:




In Java 5, you would of course add the generics type declarations. Does that help?


When you proposed using a map as an alternative, I somehow imagined that populating the map would be pretty much equivalent to the multiple if-else / switch statements.

I guess it depends on how many (different) places the OP lands up using multiple if-else statements. If it is at several places, a map would be a lot more elegant as you suggest.

respectfully,
- Anand
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Anand Hariharan:


When you proposed using a map as an alternative, I somehow imagined that populating the map would be pretty much equivalent to the multiple if-else / switch statements.

I guess it depends on how many (different) places the OP lands up using multiple if-else statements. If it is at several places, a map would be a lot more elegant as you suggest.

respectfully,
- Anand


There are more advantages than simply to prevent duplication. Using the appropriate abstraction (not necessarily a Map) provides minimalism which in turn provides adaptation to requirement change. That the implementation of the strategy is in the same place as what would otherwise be if/else is irrelevant - what is more relevant is that the implementation is free to be whatever it wants. In terms of the Map implementation, you also have the advantage of O(1) seek rather than O(n) seek for if/else.

An if else can always be abstracted to an association. Map contains one method that describes an association (and unfortunately many more which is why we have the notion of "Map-backed" gumph).
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I like the map-driven solution to separate code that I expect to change from code that I want to be stable. I populate the map from configuration so I can add new keys and commands without touching the code. The system is open for extension but closed for change.

Every time we open up code to add another else-if we run the risk of breaking it. Not touching it is a better choice any day.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to avoid multiple if else