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();
}