0
\$\begingroup\$

How can I remove the code duplication in this code? It is trying to cast the left and right numbers as integers or doubles and then using it.

public Object example(Object left, Object right) {
    if(left instanceof Integer && right instanceof Integer) {
        switch (operator) {
            case ADD:
                return (Integer)left + (Integer)right;
            case SUB:
                return (Integer)left - (Integer)right;
            case MUL:
                return (Integer)left * (Integer)right;
            case DIV:
                return (Integer)left / (Integer)right;
        }
    } else if(left instanceof Double && right instanceof Double) {
        switch (operator) {
            case ADD:
                return (Double)left + (Double)right;
            case SUB:
                return (Double)left - (Double)right;
            case MUL:
                return (Double)left * (Double)right;
            case DIV:
                return (Double)left / (Double)right;
        }
    } else {
        throw new Error("wrong types of arguments");
    }
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Please provide the context for this code. Where do the inputs come from? What function is this, and what is its return type? \$\endgroup\$ Commented Aug 12, 2018 at 4:33
  • \$\begingroup\$ The return type is Object and the inputs are Object too which maybe an Int or Double or something else. It throws an error incase it is not an int or double \$\endgroup\$
    – WAQT
    Commented Aug 12, 2018 at 11:55
  • \$\begingroup\$ Could you edit the question to include the entire function? \$\endgroup\$ Commented Aug 12, 2018 at 11:58
  • \$\begingroup\$ Is example really the name of your function? If so, it's a terrible name. \$\endgroup\$
    – Mast
    Commented Aug 12, 2018 at 12:55
  • \$\begingroup\$ @Mast it is just an example lol \$\endgroup\$
    – WAQT
    Commented Aug 12, 2018 at 22:40

1 Answer 1

2
\$\begingroup\$

One option would be to do your math operations on a double. You can use a generic method that accepts any derivative of the Number class and use the doubleValue() method for your calculation. The caller of the method can cast to Integer as needed:

enum Operators {
    ADD,
    SUB,
    MUL,
    DIV
}

public static <T1 extends Number> Double doMath(T1 left, T1 right, Operators operator) {
    switch (operator) {
        case ADD:
            return left.doubleValue() + right.doubleValue();
        case SUB:
            return left.doubleValue() - right.doubleValue();
        case MUL:
            return left.doubleValue() * right.doubleValue();
        case DIV:
            return left.doubleValue() / right.doubleValue();
        default:
            return 0.0;
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Could this be simplified even further? As in, could the list of operators be derived from the switch to reduce data duplication? Is there a reason you're no longer throwing an error if an unsupported operation is passed? \$\endgroup\$
    – Mast
    Commented Aug 13, 2018 at 5:05
  • \$\begingroup\$ What's duplicated? Throwing the error was added after I posted my code. And the error is for arguments they're passing not the operation \$\endgroup\$
    – user33306
    Commented Aug 13, 2018 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.