5

Possible Duplicate:
What is the best way to replace or substitute if..else if..else trees in programs?

How can I avoid multiple if conditions? For example:

Public void search(String a,b,c,d,e)
String aTerm;

Now which of the single and multiple combinations of passed arguments contain "aTerm"? For example the output could be following:

1 - aTerm appears in "a" only
2 - aTerm appears in "a,c", and "e"
3 - aTerm appears in "d" and "e"

For each single or possible combination I want to call a specific function. I wrote so many if conditions but it looks bad. For example:

If(aTerm. equalsIgnoreCase(a)){ call function a();}
If(aTerm. equalsIgnoreCase(b)){ call function b();}
If(aTerm. equalsIgnoreCase(b) and aTerm. equalsIgnoreCase(b)){ call function ab();}
…………………… and so on………………………….

Is there any cleaner way to do it? Solution could be in PHP or Java.

Community
  • 1
  • 1
user751637
  • 307
  • 1
  • 6
  • 16

4 Answers4

1

Build a string and call the method by the string's name:

// Psuedo-code
str = "";
If( aTerm.equalsIgnoreCase(a)) str += "a";
If( aTerm.equalsIgnoreCase(b)) str += "b";
If( aTerm.equalsIgnoreCase(c)) str += "c";
If( aTerm.equalsIgnoreCase(d)) str += "d";
If( aTerm.equalsIgnoreCase(e)) str += "e";
call function named by str
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

Polymorphism can replace ifs/switch:

interface LogicWithMatcher {
    boolean match(String aTerm);
    void yourFunction();
}

class MatcherA implements LogicWithMatcher() {...}
class MatcherB implements LogicWithMatcher() {...}
class MatcherC implements LogicWithMatcher() {...}
class MatcherD implements LogicWithMatcher() {...}
class MatcherE implements LogicWithMatcher() {...}

If you have to match one function to a given input:

public LogicWithMatcher search(String yourString) {
    LogicWithMatcher[] logics = {new MatcherA(), new MatcherB ...}
    for (LogicWithMatcher logic : logics) {
        if (logic.match(yourString)) 
            return logic;
    return null;
}

String yourString = "....."
LogicWithMatcher logic = search(yourString);
if (logic != null) 
    logic.yourFunction();
else
    print("nothing matched");

Or if your given input may match multiple functions:

public void runFunctionsFor(String yourString) {
    LogicWithMatcher[] logics = {new MatcherA(), new MatcherB ...}
    for (LogicWithMatcher logic : logics) {
        if (logic.match(yourString)) 
            logic.yourFunction();
}

String yourString = "....."
runFunctionsFor(yourString);
Luigi R. Viggiano
  • 8,659
  • 7
  • 53
  • 66
0

I would probably do something like this:

public class YourClass {
    static Runnable[] targets = new Runnable[32];

    public void search(String a, String b, String c, String d, String e) {
        int tgt = 0;
        if(a.indexOf(aTerm) >= 0) tgt |= 1;
        if(b.indexOf(aTerm) >= 0) tgt |= 2;
        if(c.indexOf(aTerm) >= 0) tgt |= 4;
        if(d.indexOf(aTerm) >= 0) tgt |= 8;
        if(e.indexOf(aTerm) >= 0) tgt |= 16;
        targets[tgt].run();
    }
}

Then, just wrap the various functions you want to run in Runnables and store them at the appropriate indices in targets. I'm pretty sure this would perform much better than something using reflection.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
0

Is there any cleaner way to do it?

I think that the short answer is "No".

There are various ways to code this in Java so that avoid an explicit chain of if/else statements, but the end result is typically more complicated than the original code. To my mind, "clean" code should mean that the code easy (easier) to read.

In your example, you have a fixed method signature and a fixed predicate structure. IMO, the if/else chain is the natural solution in Java.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I'm ignoring the PHP tag. If you want answers / solutions in multiple languages, ask multiple questions ... after first checking to see if the question has already been answered. – Stephen C Dec 30 '12 at 20:37