Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statements in a function what is the best way

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();
}
like image 312
zachary Avatar asked Jun 23 '09 14:06

zachary


1 Answers

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();
}
like image 103
OscarRyz Avatar answered Oct 08 '22 07:10

OscarRyz