Clean code

Writing cleaner conditional statements

Just a thought on using cleaner code while using a lot of nested if statements.

Clean code

Clean code

Ugly code

if(process_x()) {
    /* do a lot of other things */
    if(process_y()) {

         /* do a lot of other things */
         if(process_z()) {
              /* do a lot of other things */
              /* SUCCESS */
         }
         else {
              clean_all_processes();
         }
    }
    else {
         clean_all_processes();
    }
}
else {
    clean_all_processes();
}

Good looking code

do {

  if( !process_x() )
    { clean_all_processes();  break; }

  /* do a lot of other things */

  if( !process_y() )
    { clean_all_processes();  break; }

  /* do a lot of other things */

  if( !process_z() )
    { clean_all_processes();  break; }

  /* do a lot of other things */
  /* SUCCESS */

} while (0);

The use of goto is an alternative, the bad thing about this is you always need to define the label.

Advertisements

One comment

  1. threelionheads.wordpress.com · January 2, 2014

    Sometimes you can remove ugly code only be by using good design. Check out the following pseudocode.

    interface IDoDifferentThings{
     void process();
     bool canProcess();
     void cleanAllProcesses();
    }
    
    
    abstract class AbstractProcess extends IDoDifferentThings{
    
        IDoDifferentThings nextThing;
    
        protected AbstractProcess{
        }
    
        protected AbstractProcess(IDoDifferentThings nextThing){
    	this.nextThing = nextThing;
        }
    
        void cleanAllProcesses(){
             /* cleanup code here */
        }
    
        void processNextThing(){
             if(this.nextThing == null){
    		return;
         	 }
             this.nextThing.process();
    
        }
    
        final void process(){
             if(!canProcess()){
    	     this.cleanAllProcesses();
             }
    	 
    	 this.processConcrete();
    
    	 this.processNextThing();	 
        }	
    
        protected abstract void processConcrete();
    }
    
    class ProcessX extends AbstractProcess{
    
        void processConcrete(){
    	/* do lots of X related stuff */
        }
    
        bool canProcess(){
    	/* process_x() */
        }
    }
    
    class ProcessY extends AbstractProcess{
    
        void processConcrete(){
    	/* do lots of Y related stuff */
        }
    
        bool canProcess(){
    	/* process_Y() */
        }
    }
    
    class ProcessZ extends AbstractProcess{
    
        void processConcrete(){
    	/* do lots of Z related stuff */
        }
    
        bool canProcess(){
    	/* process_Z() */
        }
    }
    

    This of course adds some complexity to your system by introducing new classes. But it makes the call much clearer.

    IDoDifferentThings xyz = new ProcessX(new ProcessY(new ProcessZ))); /* configurable */
    xyz.process();
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s