Esempio

public static void main( String[] args ) {
if ( args.length > 3 ) {
System.out.println( "More than 3" ); //$NON-NLS-1$
if ( args[ 0 ].startsWith( "a" ) ) { //$NON-NLS-1$
System.out.println( "Starts with a" ); //$NON-NLS-1$
if ( args[ 1 ].endsWith( "z" ) ) { //$NON-NLS-1$
System.out.println( "Ends with z"); //$NON-NLS-1$
}
}
}
}

Soluzione
Invertire la condizione ed aggiungere un'istruzione return.
La seguente soluzione viene discussa in Refactoring di Martin Fowler.
  1. Invertire la condizione dell'istruzione if pił esterna.
  2. Aggiungere un'istruzione return al di sotto dell'istruzione if.
  3. Ripetere questi passi fino ad eliminare tutte le istruzioni if nidificate.

public static void main( String[] args ) {
if ( args.length <= 3 ) {
return ;
}

System.out.println( "More than 3" ); //$NON-NLS-1$

if ( !args[ 0 ].startsWith( "a" ) ) { //$NON-NLS-1$
return ;
}

System.out.println( "Starts with a" ); //$NON-NLS-1$

if ( args[ 1 ].endsWith( "z" ) ) { //$NON-NLS-1$
System.out.println( "Ends with z" ); //$NON-NLS-1$
}

}