0

My code looks like

public void convertValues(String sourceValue,String targetValue) {
    if (sourceValue.equals(targetValue)) {
         //dosomething 
    } else if (sourceValue.equals(value1) && targetValue.equals(value2)) {
        //dosomething
    } else if (sourceValue.equals(value2) && targetValue.equals(value1)) {
        //dosomething 
    }
}

like that I have some 40 condition, I know writing those if else condition is stupidity.

Is it good approach to use enums or hashmaps?

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
Sameer
  • 21
  • 3
    There's definitely going to be a better approach to this, but we'd have to see the full context to say what would work best. In any case, this would be a much better fit for https://codereview.stackexchange.com/. – Michael Berry Jul 02 '18 at 16:21
  • Are the rest of the conditions of the form `sourceValue.equals() && targetValue.equals( – Thiyagu Jul 02 '18 at 16:21
  • already answered [here](https://stackoverflow.com/questions/10175805/how-to-avoid-a-lot-of-if-else-conditions) – MajiD Jul 02 '18 at 16:28
  • Possible duplicate of [How to avoid a lot of if else conditions](https://stackoverflow.com/questions/10175805/how-to-avoid-a-lot-of-if-else-conditions) – Guillaume Georges Jul 02 '18 at 16:34
  • @user7 yes all conditions are like that – Sameer Jul 02 '18 at 17:12

2 Answers2

0

To speedup entering a spaghetti code, you probably need to prepare a helper class that would simplify adding the repeated lines.

If you don't like using else if, use return; (Caution: snobbish people are allergic to seeing return not at the end).

Use a helper class / method for checking values.

Example:

public class Main {
String value1 = "a";
String value2 = "b";
String value3 = "c";
String value4 = "d";

// use a nested private class where you could add all long 
// and repetitive methods that need to be done
// with source and target

private class Helper {
    private String src;
    private String tgt;
    Helper(String src, String tgt) {
        this.src = src;
        this.tgt = tgt;
    }
    public boolean eq(String val, String val1) {
        return src.equals(val) && tgt.equals(val1);
    }
    // add other repetitive functions here
}


public void convertValues(String sourceValue, String targetValue) {

    // use a simple name for the helper class.
    Helper $ = new Helper(sourceValue, targetValue);

    if (sourceValue.equals(targetValue)) {
        // put your execution lines into a separate method.
        // this way, it will become reusable and much easier to maintain
        // call a method1
        return;
    }

    if ($.eq(value1, value2)) {
        // call a method2
        return;
    }

    if ($.eq(value2, value1)) {
        // call a method3
        return;
    }

    if ($.eq(value1, value3)) {
        // call a method4
        return;
    }
    // ....
}


// public void method1()
// public void method2()

public static void main(String[] args) throws java.lang.Exception {
   // Everything else

}
}
Eugene Kartoyev
  • 501
  • 4
  • 11
0

Using lambda expressions in Java 8, it can be implemented like:

Define Value class:

public class Value {

    private final String value1;
    private final String value2;
    // Function to exetute
    private final BiConsumer<String, String> f;

    public Value(String value1, String value2, BiConsumer<String, String> f) {
        super();
        this.value1 = value1;
        this.value2 = value2;
        this.f = f;
    }

    public boolean execute(String src, String tgt) {
        if(src.equals(value1) && tgt.equals(value2)) {
            f.accept(src, tgt);
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((value1 == null) ? 0 : value1.hashCode());
        result = prime * result + ((value2 == null) ? 0 : value2.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Value other = (Value) obj;
        if (value1 == null) {
            if (other.value1 != null)
                return false;
        } else if (!value1.equals(other.value1))
            return false;
        if (value2 == null) {
            if (other.value2 != null)
                return false;
        } else if (!value2.equals(other.value2))
            return false;
        return true;
    }           
}

// Initialize values

Set<Value> values = new HashSet<>();
values.add(new Value("value1", "value2", (src, tgt) -> {/* do something here */}));
values.add(new Value("value2", "value1", (src, tgt) -> {/* do something here */}));
values.add(new Value("value4", "value5", (src, tgt) -> {/* do something here */}));

// Execute

if (sourceValue.equals(targetValue)) {
     //dosomething 
} else {
    for(Value v:values) {
        if(v.execute(src, tgt)) {
             break;
        }
    }
}
Pavel Molchanov
  • 2,299
  • 1
  • 19
  • 24