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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:01:31+00:00 2026-05-25T22:01:31+00:00

I have 2 alternatives to implement a calculation method, and I am wondering what

  • 0

I have 2 alternatives to implement a calculation method, and I am wondering what would be the better approach.

The method needs some int and double parameters and (in some cases) a special flag to do some different calculation.

In the first example, I could call the method with ‘calculateFoo(1, 2.0d)’ to have the boolean flag == FALSE.

In the second example, I always have to set the boolean flag (even if I do not need it)

Method 1: (here I’m using the ‘…’ as an ‘method overloading’ parameter)

public SomeObject calculateFoo(int pIntValue, double pDoubleValue, boolean... pDoSpecialStuff) {

    if (pDoSpecialStuff.length == 0 || pDoSpecialStuff[0] == false) {
        // method was called without boolean flag or boolean flag was set to FALSE

        // do some calculation stuff
        // ...
    } else {
        // method was called with boolean flag == TRUE

        // do some other calculation stuff
        // ...
    }

    return SomeObject; 
}

Method 2: (this is the ‘common’ approach)

public SomeObject calculateFoo(int pIntValue, double pDoubleValue, boolean pDoSpecialStuff) {

    if (pDoSpecialStuff == false) {
        // method was called with boolean flag == FALSE

        // do some calculation stuff
        // ...
    } else {
        // method was called with boolean flag == TRUE

        // do some other calculation stuff
        // ...
    }

    return SomeObject; 
}
  • 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-25T22:01:32+00:00Added an answer on May 25, 2026 at 10:01 pm

    Consider the following pattern:

    public ResultType calcFoo( int i, double d ) {
        return calc( i, d, false );
    }
    
    public ResultType calcFoo( int i, double d, boolean flag ) {
        if( flag ) {
            ...
            return result;
        }
        else {
            ...
            return result;
        }
    }
    

    Generally it’s better to use an enum instead of a boolean flag. It makes your code more readable and is just as fast.

    I noticed you considered using varargs. If you want to use more flags consider using an EnumSet to pass a set of flags to the method. If you want to pass 0 or 1 flags, varargs is even more definitely an antipattern.

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

Sidebar

Related Questions

I have to implement some scripting capabilities in our product. The scripting will typically
What alternatives are there to GAE, given that I already have a good bit
I have been using extplorer http://extplorer.sourceforge.net/ Are there any good alternatives which run on
In C++, what alternatives do I have for exposing a collection, from the point
I have a tool strip which contains a ToolStripDropDownButton. As an alternative access method,
I am writing an if/else alternative using a table driven method. I have the
I have simple map-reduce type algorithm, which I want to implement in python and
I want to implement some sort of lookup table in C++ that will act
I have made a Java Swing application. Now I would like to make it
I have some fundamental points/questions about OpenGL, not all involving code but concepts as

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.