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 1038853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:01:38+00:00 2026-05-16T15:01:38+00:00

What is the best (most compact) way to hand this situation: One or more

  • 0

What is the best (most compact) way to hand this situation:
One or more arguments to a method call depends on some condition, while the rest of the arguments are identical?

For example– you want to

DeathRay mynewWpn = new DeathRay(particle.proton, chassisColor.BLACK, oem.ACME)

if

enemy_composition == nature.ANTIMATTER

but

DeathRay mynewWpn = new DeathRay(particle.anti-proton, chassisColor.BLACK, oem.ACME)

if

enemy_composition == nature.MATTER

Obviously you can if-else but it looks unnecessarily long when there are a lot of arguments or more than one conditional argument. I have also done this creating an argument with an if-else beforehand and then calling the method. Again, that seems kind of clunky. Is there some sort of inline syntax similar to an Excel if-statement?

  • 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-16T15:01:38+00:00Added an answer on May 16, 2026 at 3:01 pm

    You can do

    new DeathRay(enemy_composition == nature.ANTIMATTER ? particle.proton : particle.anti-proton, chassisColor.BLACK, oem.ACME)
    

    … but I think we can all agree that’s hideous. It also assumes that there are only two kinds of particle.

    Here are some better alternatives.

    switch:

    particle type;
    switch (enemy_composition) { /* Assuming "nature" is an enum. */
      case ANTIMATTER : 
        type = particle.proton;
        break;
      case MATTER : 
        type = particle.antiproton;
        break;
    }
    DeathRay mynewWpn = new DeathRay(type, chassisColor.BLACK, oem.ACME);
    

    enum method:

    Add a method to your enum, Nature.

    public enum Nature
    {
    
      MATTER
      {
        public Particle getCounterWeapon()
        {
          return Particle.ANTIPROTON;
        }
      },
      ANTIMATTER
      {
        public Particle getCounterWeapon()
        {
          return Particle.PROTON;
        }
      };
    
      public abstract Particle getCounterWeapon();
    
    }
    

    Then use it.

    DeathRay mynewWpn = new DeathRay(enemy_composition.getCounterWeapon(), chassisColor.BLACK, oem.ACME);
    

    Map:

    particle type = counterWeapons.get(enemy_composition);
    DeathRay mynewWpn = new DeathRay(type, chassisColor.BLACK, oem.ACME);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always wondered what's the best way to achieve this task. In most
Best / Most efficient way of achieving this? DOMDocument? Xpath? Mixture of both? <currencies>
What is the best/most time efficient way to insert 1000 rows into one table
Basically, wondering what the best / most python-y way of doing this is; class
In ruby, what is the best/most-elegant way to return a value such as: #method
I am interested to find out what is the best/fastest (most efficient) way to
I just looked at this posting: What is the best or most interesting use
What's the most common/best way to setup a WCF service project and applications? Here's
What is the most standard or best way to persist data between requests? Should
What is the most relevant and best way to update data on an iOS

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.