3
\$\begingroup\$

I need help making this code more efficient but still functions the same, such as by making it access the Twitter API fewer times. When I run this code for like 1 min, I get an error from Twitter saying that it exceeds limits.

List<Status> tweets = result.getTweets();

       for (Status tweet : tweets) {

            if(!(tweet.getUser().getScreenName().toString().equals("gh") || tweet.getUser().getScreenName().toString().equals("gh"))){

                boolean found = false;

                List<Status> Stats=  tw.getHomeTimeline();
                for (Status StatList : Stats) {  

                    if(StatList.getTweetText().equals(tweet.getTweetText())){   
                        found = true;
                        break;
                        try {
                            System.out.println(tweet.getUser().getScreenName() );

                            Thread.sleep(2000);
                        } catch(InterruptedException ex) {
                            Thread.currentThread().interrupt();
                        }
                    }else{
                        // nothing here                    
                    }
                }
                if (found) // you can move this and the initialisation(boolean found = false) out side the upper if statement
                    System.out.println("false");
                else
                    System.out.println("turn");

            }else{
                System.out.println("blocked");
            }
        }
\$\endgroup\$
1
  • 1
    \$\begingroup\$ What Twitter client library are you using? Could you please include enough context for us to tell what result is? \$\endgroup\$ Dec 8, 2014 at 5:09

2 Answers 2

2
\$\begingroup\$
  • dead code should be removed
  • store multiple accessed properties to variables like @thepace mentioned
  • use braces for single if statements also. This is a matter of taste, but your code will be less errorprone
  • comments which doesn't explain why something is done should be removed. What is done should be explained by the code itself.
  • this double checking if the screenname is "gh" isn't needed. You can't be more sure than sure.
  • Calling toString() on a String is not necessary
  • empty else blocks can be removed
  • variable names should be using camelCase casing

Refactoring

after applying the above

List<Status> tweets = result.getTweets();

for (Status tweet : tweets) {

    String userName = tweet.getUser().getScreenName();
    if(!(userName.equals("gh")){

        boolean found = false;

        List<Status> stats=  tw.getHomeTimeline();
        for (Status status : stats) {  
            String tweetText = tweet.getTweetText();
            if(status.getTweetText().equals(tweetText)){   
                found = true;
                break;
             }
         }
         if (found) {
             System.out.println("false");
         } else {
             System.out.println("turn");
         }
     } else {
         System.out.println("blocked");
     }
 }
\$\endgroup\$
2
\$\begingroup\$

Just a few hints that might better the code:

  1. If you tend to access a structure for a given element multiple times with an operation on it, extract it in a local variable:

    tweet.getUser().getScreenName() // If its String, toString not required.
    
  2. Have try..catch for statements that can cause the exception i.e put this out

    System.out.println(tweet.getUser().getScreenName() );
    
  3. As your comment states, put the boolean out with the two states and have minimal print statements. If its for debugging, add comments or debug flag.

  4. Having try..catch after break? Please check.
  5. Check if tweets is null after

    List<Status> tweets = result.getTweets();
    
  6. Check if tweet is null inside the for loop.

  7. Apply (5) and (6) for Stats as well.
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.