Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 607599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:22:37+00:00 2026-05-13T17:22:37+00:00

So I’m not going for maintainability or elegance here.. looking for a way to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:22:37+00:00Added an answer on May 13, 2026 at 5:22 pm
    (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 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.