Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 642133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:08:17+00:00 2026-05-13T21:08:17+00:00

So while rewriting some code, I came across something along the lines of: Method

  • 0

So while rewriting some code, I came across something along the lines of:

Method 1

while ( iter.hasNext() ) {
    Object obj = iter.next();
    if ( obj instanceof Something ) {
        returnValue = (Something) obj;
        break;
    }
}

I re-wrote it as the following without much thought (the actual purpose of the re-write was for other logic in this method):

Method 2

while ( (iter.hasNext()) && (returnValue == null) ) {
    Object obj = iter.next();
    if ( obj instanceof Something ) {
        returnValue = (Something) obj;
    }
}

I personally don’t have any strong preference for either and really don’t see anything wrong with either approach. Can anyone else think of benefits or consequences of using either approach? The variable, returnValue is returned. How would people feel if that was the last block in the method, and it is just returned?

EDIT: So here’s what I’m doing: Currently this method takes a set of authorizations and validates them – returning a boolean. This method allows grouping so you can specify that at least one or all (meaning if at least one authorization is valid, pass the whole set). However, this method does not support levels of authorization which is what I’m changing it to do so that each level can specify different grouping. All this bit is just background information…does not have that much to do with the above code – an alternative method is used to perform the above block of code.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:08:17+00:00Added an answer on May 13, 2026 at 9:08 pm

    Clearer, for me, would be to extract this as a method of its own; then you can simply return the value rather than assigning it to a local.

    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Something)
            return (Something)obj;
    }
    return null;
    

    Better yet would be a foreach loop

    for (Object o : yourList)
        if (o instanceof Something)
            return (Something)o
    return null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While porting some code from Delphi 7 to Delphi 2010 I was rewriting my
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
I am rewriting some rendering C code in C++. The old C code basically
If I have some code that looks something like: typedef struct { bool some_flag;
I'm rewriting some code that works into code that's currently not working. Basically, I
While debugging and keep pressing F5, if the source code does not exist, eclipse
While refactoring my code base I found a piece of code which I'd like
I'm experiencing some issues with rewriting my blocking socket server to a non-blocking version.
i have the following code that generate some images with urls associated from database:
I am running a query using a scope and some conditions. Something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.