java - How can I refactor code to remove if statements? -


i learnt everywhere using if statement bad practice when it's possible avoid them . i'm trying learn how make clean code, seems design patterns helpful i'm wondering if it's possible refactor piece of code in order remove if statement from, here code desmonstrating this:

public class printer {      private boolean on=false;     private job currentjob=null;     private string name;      public string getname(){         return name;     }      public void setname(string name) {         this.name=name;     }      public void seton(boolean _on){         this.on=_on;     }      public boolean geton(){         return this.on;     }      public void setcurrentjob(job _currentjob){         this.currentjob=_currentjob;     }      public job getcurrentjob(){         return this.currentjob;     }      private boolean getonstart(){         seton(true);         return geton();     }      public boolean start(){         seton(true);         return on;     }      public boolean stop(){         seton(false);         return !on;     }      public boolean suspend(){         if (!isprinting()) {             throw new illegalstateexception("error");         }             currentjob.setstate(job.wainting);             return true;     }      public boolean resume(){         if (this.currentjob==null && currentjob.getstate()!=0) {             throw new illegalstateexception("error");         }             currentjob.setstate(job.printing);             return true;     }      public boolean cancel(){         if (this.currentjob==null && currentjob.getstate()!=0) {             throw new illegalstateexception("error");         }             currentjob = null;             return true;     }     public boolean print(job ajob){         if (isavailable()){             currentjob=ajob;             ajob.setprinter(this);             ajob.setstate(job.printing);             return true;         }         system.err.println("error");         return false;     }      public boolean printingcompleted(){         if (isprinting()){             currentjob.setprinter(null);             currentjob.setstate(job.completed);             currentjob=null;             return true;         }         system.err.println("error");         return false;     }      public void setspooler(spooler spool){         spool.join(this);     }      public boolean isavailable(){         return on && currentjob==null;     }      public boolean isprinting(){         return on && currentjob!=null;     } } 

using if incorrectly or excessively can indicate code smell not go far should avoided.

in case indeed little iffy. have coded logic like.

public void print(job ajob) {     if (!isavailable()) {         throw new illegalstateexception("cannot print when printer not available.");     }     currentjob = ajob;     ajob.setprinter(this);     ajob.setstate(job.printing); }  public void printingcompleted() {     if (!isprinting()) {         throw new illegalstateexception("attempt complete printing when no printing in progress.");     }     currentjob.setprinter(null);     currentjob.setstate(job.completed);     currentjob = null; } 

this provides 3 benefits:

  1. errors can handled/logged elsewhere.
  2. you don't have return true or false indicate success/failure (a common smell).
  3. each method has single exit point (a known smell).

Comments