-5

In Java is it possible to use ternary to pass multiple arguments into a method call?

For example - in a method have:

    print(degree == 270 ? "e", 5 : "t", 6);

which calls:

    public void print(String s, int t){

    }

By using the ternary I want to pass in e and 5 OR t and 6. Without the need of having to duplicate code - like:

    print(degree == 270 ? "e" : "t", degree == 270? 5 : 6);

I don't think some understand, I don't want to use this method above, it runs the check an unnecessary time.

Jim
  • 274
  • 3
  • 13

3 Answers3

1

Why not

    (degree == 270) ? print("e", 5) : print("t", 6);

EDIT: apparently does not work in java :/

max_
  • 324
  • 1
  • 7
1

It would be possible if your expression consists of a wrapper object that has both of the values (s and t) as variables, but then you would have to instantiate both. For making your code better, I would try something like:

if(degree == 270) {
    print("e", 5);
}
else {
    print("f", 6);
}

or

switch(degree) {
    case 270:
        print("e", 5);
        break;
    default:
        print("f", 6);
        break;
}

as long as you compare to absoute values (not ranges).

I know it's not what you're looking for, but normally, in enterprise environments, this is the notation that would be recommended as it's the easiest to understand for others that will mantain the code and reduces risk of errors if some novice programmer has to change something.

At the end, the compiled code will be efficient either way. In fact, I'm thinking that your second option will go through the conditional expression twice, in which case it would be better just to use one of the alternatives I wrote.

Regards

Menno
  • 12,175
  • 14
  • 56
  • 88
Martin
  • 3,018
  • 1
  • 26
  • 45
  • I work in industry and I was told ternarys are easier to maintain and look at. I know wrapping it would have worked but that's unnecessary. Thanks anyway. – Jim May 29 '13 at 11:44
  • 1
    Ternaries are great, I agree. `return test? "a" : "b"` puts less mental load on the reader than `if (test) return "a" else return "b"` because in the former case it is known up front that we are unconditionally returning, and it is only left to decide what, wheras with `if` we must carefully inspect both clauses to make sure. – Marko Topolnik May 29 '13 at 11:48
  • Well, it's always a matter of opinion from people in each enterprise. However, the longer code is really easy to look at. What makes it risky is if you don't use the braces on 1 line if statements, often a programmer adds a line and forgets that there are no braces... At the end, I think it's a matter of having coding standards... – Martin May 29 '13 at 11:50
  • @Marko I agree, specially in the case you state because you're avoiding multiple return statements in a method which is also often considered as a bad practice... As I said, it will often depend on the policies of the enterprise... – Martin May 29 '13 at 11:52
0

A conditional expression (that's the official name) is an expression just like any other Java expression. As far as the surrounding method call is concerned, only its result matters. So clearly, you cannot use one expression to specify two arguments because each argument is its own expression.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • That's not really an answer to the question. He asks whether it is possible to return MULTIPLE results depending on the condition. – Menno May 29 '13 at 11:21