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 soluzione seguente è discussa in Rifattorizzazione da Martin Fowler.
  1. Invertire la condizione dell'istruzione if più esterna.
  2. Aggiungere l'istruzione return al di sotto dell'istruzioneif.
  3. Ripetere questi passaggi fino a quando non ci saranno più istruzioni if nidificate in profondità.

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$
}

}