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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:25:24+00:00 2026-05-11T21:25:24+00:00

If I have a function with a ton of conditionals what is the best

  • 0

If I have a function with a ton of conditionals what is the best way to organize it?

What I am worried about is someone else coming into the code and understanding what is going on. Even though the example is simple imagine that the conditional is very complex.

For an example:

public void function(string value, string value2)
{
    if (value == null)
        return;

    if (value2 == value)
        DoSomething();
}

or

public void function(string value, string value2)
{
    if (value != null)
    {
        if (value2 == value)
            DoSomething();
    }
}

or

public void function(string value, string value2)
{
    if (value != null && value2 == value)
        DoSomething();
}
  • 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-11T21:25:24+00:00Added an answer on May 11, 2026 at 9:25 pm

    Organize the conditions and put them into a method.

    for instance replace this:

     if( a& & n || c  && ( ! d || e ) && f > 1 && ! e < xyz ) { 
          // good! planets are aligned.
          buyLotteryTicket();
     } else if( ..... oh my ... ) { 
     }
    

    Into this:

    if( arePlanetsAligned() ) { 
        buyLotteryTicket(); 
    } else if( otherMethodHere() ) { 
       somethingElse();
    }  
    

    That way it doesn’t really matter what style you use ( 1, 2 or 3 ) because the if statement will clearly describe what’s the condition being tested. No need for additional constructs.

    The point is to make the code clearer and self documenting. If you are using a OO programming language you can use an object to store the state ( variables ) and avoid creating methods that take 5 – 10 parameters.

    These are similar questions:

    Best way to get rid of nested ifs

    Is there an alternative to this hyperidented code

    The second link one shows a more complete and complex way to transform an horrible’s everyone maintainer nightmare into a self documenting code.

    It shows how to transform this:

    public String myFunc(SomeClass input)
    {
        Object output = null;
    
        if(input != null)
        {
            SomeClass2 obj2 = input.getSomeClass2();
            if(obj2 != null)
            {
                SomeClass3 obj3 = obj2.getSomeClass3();
                if(obj3 != null && !BAD_OBJECT.equals(obj3.getSomeProperty()))
                {
                    SomeClass4 = obj3.getSomeClass4();
                    if(obj4 != null)
                    {
                        int myVal = obj4.getSomeValue();
                        if(BAD_VALUE != myVal)
                        {
                            String message = this.getMessage(myVal);
                            if(MIN_VALUE <= message.length() &&
                               message.length() <= MAX_VALUE)
                            {
                                //now actually do stuff!
                                message = result_of_stuff_actually_done;
                            }
                        }
                    }
                }
            }
        }
        return output;
    }
    

    into this:

    if ( isValidInput() && 
        isRuleTwoReady() &&
        isRuleTreeDifferentOf( BAD_OBJECT ) &&
        isRuleFourDifferentOf( BAD_VALUE ) && 
        isMessageLengthInRenge( MIN_VALUE , MAX_VALUE ) ) { 
                message = resultOfStuffActuallyDone();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ton of jquery code in the .ready() $(document).ready(function() { //jQuery goodness
I have function: char *zap(char *ar) { char pie[100] = INSERT INTO test (nazwa,
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =
I have function LoadTempMovieList(), and need to load movies from sessionStorage. But it seems
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function getCartItems in cart.js and I want to call that function in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have function some_func_1 which will create an object of type some_type and will
I have function like this: function gi_insert() { if(!IS_AJAX){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags'); $this->form_validation->set_rules('body',
i have function to send mail with attachment to microsoft exchange server. My problem

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.