views:

481

answers:

2
+3  Q: 

java ternary hack

So I'm not going for maintainability or elegance here.. looking for a way to cut down on the total tokens in a method just for fun. The method is comprised of a long nested if-else construct and I've found that (I think) the way to do it with the fewest tokens is the ternary operator. Essentially, I translate this:

String method(param) {

    if (param == null)
        return error0;

    else if (param.equals(foo1))
        if (condition)
            return bar1;
        else
            return error1;

    else if (param.equals(foo2))
        if (condition)
            return bar2;
        else
            return error1;

    ...


    else
        return error;

}

to this:

String method(param) {

    return 

        param == null ?
            error0 :

        param.equals(foo1) ?
            condition ?
                bar1 :
                error1 :

        param.equals(foo2) ?
            condition ?
                bar2 :
                error2 :

        ...

        error

    }

However, there are a couple cases where in addition to returning a value I also want to change a field or call a method; e.g.,

    else if (param.equals(foo3))
        if (condition) {
            field = value;
            return bar3;
        }
        else
            return error3;

What would be the cheapest way to do this token-wise? What I'm doing now is ugly but doesn't waste too many tokens (here the field is a String):

        param.equals(foo3) && (field = value) instanceOf String ?
            condition ?
                bar2 :
                error2 :

Again, the point is not good coding, I'm just looking for hacks to decrease the token count. If there's a shorter way to write the entire thing I'm open to that as well. Thanks for any suggestions.

Edit: Each word and punctuation mark counts as one token. So, for example, "instanceOf String" is two tokens, but "!= null" is three. The main things I can see for possible improvement are the "&&" and the parentheses. Is there a way to put "field = value" somewhere besides the conditional, and if not is there a construct that makes "field = value" a boolean without the need for parentheses?

+2  A: 

if param is null, return 0
Then make a case/switch/select statement on the parameter. That's clean .

Trevoke
I don't think he *wants* "clean" (unfortunately for future maintenance devs).
FrustratedWithFormsDesigner
And token-cheap.
Romain
You can't switch on a String.
mtpettyp
if you have "equality" conditions over strings, probably an enum will do the job.
Lorenzo Boccaccia
@mtpettyp: It will be allowed in Java 7: http://blogs.sun.com/darcy/entry/project_coin_final_five
Chris Jester-Young
@Chris Jester-Young - good to know. It's nice to see some useful syntactical-sugar being added.
mtpettyp
@Chris Jester-Young Still it's a switch case... ugly and error-prone
Helper Method
like mtpettyp said, you can't switch on a String, so it doesn't help.
and for the size of this program, it would be less token-efficent to use enums
@FrustratedWithFormsDesigner: if you *think* from how the question is formulated that there will be "future maintenance devs" working on this "codebase", then I'd like to have a bit of what you're smoking because it seems heavy.
Webinator
+2  A: 
(field = value) instanceof String

Assuming that it already satisfies your needs (and it thus includes returning false when value is null), a shorter alternative would then have been

(field = value) != null

Or if you actually overlooked that and want to make null return true as well, then use

(field = value) == value

This can be made much shorter if you use 1-letter variable names.

Further I don't see other ways and I agree with most of us that this all is somewhat nasty ;)

BalusC
There's actually a program that grades our work, and counts each string and each punctuation mark as a token. So "instanceOf String" is two tokens, and "!= null" is three. It would be nice if there was a construct that didn't require the parentheses around "field = value"
I would disagree that `!=` counts for two. It's technically **one** operator.
BalusC
hmm i just checked and the program actually does count != as one
hey great! i don't need parentheses with this one. thanks!
It will indeed compile without parentheses, but you actually need them! Otherwise it will incorrectly evaluate `field = (value != null)`. The `!=` has higher precedence than `=`.
BalusC
hmm it actually works fine for me