2

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 :)

1
  • boolean typeIsSomething = accountType.equals("something"); Now you have a boolean value which you can use within the loop Commented Dec 1, 2015 at 10:04

5 Answers 5

6

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Alex, Thanks for the answer, I actually aware of the #1 solution, But the thing is "//some code goes here" part has lot of codes which I think putting into two loops will make lot of redundancy.
To avoid duplicating the code in "some code goes here" extract it into a method that can be called from both places. The accepted answer by @Eran still has an if condition inside the loop.
4

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
}

Comments

0

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) {
     [...] 
   }

Comments

0
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);

1 Comment

A few words of additional comment are always appreciated next to the raw code.
0
//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);        

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.