if statement - How can I avoid using method selector arguments (flag/boolean arguments) in Java -


i've finished clean code , 1 of precepts avoid passing boolean arguments functional selectors, e.g.,

instead of this:

public double calculateweeklypay(boolean overtime) {     double pay = calculatepay();     if (overtime) {          return pay *= 1.5;     }     return pay; } 

we should this, , let consumer call appropriate method:

public double calculateweeklypay() {     return calculatepay(); }  public double calculateweeklypaywithovertime() {     double pay = calculateweeklypay();     return pay *= 1.5; } 

but should in more complicated methods splitting method mean lots of duplicate code?

what should done here:

public double calculateweeklypay(boolean overtime) {      double pay = calculatepay();     pay = modifypay1(pay);     pay = modifypay2(pay)     pay = modifypay3(pay)     if (overtime) {         pay = pay *= 1.5;     }     pay = modifypay4(pay);     pay = modifypay5(pay);     pay = modifypay6(pay); 

}

you have distinguish between public api , internal api.

for public api should avoid such selector arguments. internal api (the private methods) appropriate mechanism implementation. make this:

public double calculateweeklypaywithovertime() {     return calculateweeklypay(true); }  public double calculateweeklypaywithoutovertime() {     return calculateweeklypay(false); }  private double calculateweeklypay(boolean overtime) {     double pay = calculatepay();     if (overtime) {          pay *= 1.5;     }     return pay; } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -