How to remove if else condition from loop?

I have a code snippet similar to the one below,

public ArrayList getReport(reportJDOList,accountType)
{
    String abc = "";

    for(ReportJDO reportJDO : reportJDOList)
    {
        if(accountType.equals("something")
           abc = reportJDO.getThis();
        else
           abc = reportJDO.getThat();

        //somecode goes here
    }

    returning List;
}

As I know the value of accountType before the iteration, I dont want this check to happen, for every entry in a list as it would cause numerous number of checks if the size of reportJDOList is 10000 for an instance. How we remove this thing from happening? Thanks in Advance :)

Answers


IF you want to save the String comparison, make it once before the loop and store the result in a boolean variable :

String abc = "";
boolean isThis = accountType.equals("something");
for(ReportJDO reportJDO : reportJDOList) {  
    abc = isThis ? reportJDO.getThis() : reportJDO.getThat();
    //somecode goes here
}

You can indeed peform check once and implement 2 loops:

if(accountType.equals("something") {
   for(ReportJDO reportJDO : reportJDOList) {
       abc = reportJDO.getThis();
   }
} else {
   for(ReportJDO reportJDO : reportJDOList) {
       abc = reportJDO.getThat();
   }
}

Obviously you can improve your design by either

  1. separating you loops into 2 different methods
  2. Using command pattern, i.e. implementing loop body in different command and executing it to loop.
  3. Using Guava's Function (it is just improvement of #2)
  4. Using java 8 streams.

I'd vote for clean coding this - perform the check once and delegate the logic into private methods, each performing the loop individually. This duplicates code for the loop but gives greatest flexibility if at some point you need to do something more in SomethingReport that's not duplicated in OtherReport.

   public ArrayList getReport(reportJDOList,accountType) {
     if("soemthing".equals(accountType)) {
       return getSomethingReport(reportJDOList);
     } else {
       return getOtherReport(reportJDOList); 
     }
   }

   private ArrayList getSomethingReport(reportJDOList) {
     [...] 
   }

interface AccountHandler {
    String get(Report r);
}

AccountHandler thisHandler= new AccountHandler() {
    @Override
    public String get(Report r) {
        return r.getThis();
    }
};  
AccountHandler thatHandler= new AccountHandler() {
    @Override
    public String get(Report r) {
        return r.getThat();
    }
};

//...............
AccountHandler ah;
ah = (what.equalsIgnoreCase("this")) ? thisHandler : thatHandler;
Report r=new Report();
// loop
ah.get(r);

//Using reflection:
Report r = new Report();
Method thisMethod = r.getClass().getDeclaredMethod("getThis");
Method thatMethod = r.getClass().getDeclaredMethod("getThat");
Method m =  (what.equalsIgnoreCase("this")) ? thisMethod : thatMethod;
m.invoke(r);        

Need Your Help

Having a single image view to show different images

ios cocoa-touch uiimageview

My app requires characters except they can be customized. Is it possible to have one image view but change the actual image to something else?

Set up a for loop to fill image src's

javascript html xml for-loop

So I'm sending a request to NASA's APOD API and receiving back an array of info, part of which is an image url. I need to perform this action multiple times, and am wondering how to set this up as ...