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 8966389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:01:02+00:00 2026-06-15T17:01:02+00:00

I’m sure there must be a standard way to do this, but my attempts

  • 0

I’m sure there must be a standard way to do this, but my attempts to search Stackoverflow have failed.

I have a method like:

public void processSomeWidgetsForUser(int userItemId) {
    Iterator<Widgets> iter = allWidgets.values().iterator();
    while(iter.hasNext()) {
        Widget thisWidget = iter.next();
        if (userItemId == -1 || thisWidget.getUsersItemId() == userItemId) {                 
             widget.process();
        }
    }
}

As you can see -1 is a “special value” meaning process all. Doing this saves repeating the loop code in another method called processSomeWidgetsForAllUsers.

But I dislike special values like this because they are easy to misuse or misunderstand, which is exactly the situation what I’m having to fix now (where someone thought -1 meant something else).

I can only think of two ways to improve this.

  1. have a constant, containing -1 called something like
    Widget.ALLWIDGETS which at least is self-documenting, but doesn’t
    stop code from using a -1 (if someone integrates old code in, for
    example)
  2. change the method to take a list of all user ids to
    process, which can be empty, but that doesn’t seem great
    performance-wise (would need to retrieve all user ids first and then loop through
    removing. Also what happens if the number of widgets in the list changes between
    retreiving the ids and removing

Is there a better way? I’m sure I’m missing something obvious.

The above code has been changed slightly, so may not compile, but you should get the gist.

  • 1 1 Answer
  • 0 Views
  • 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-06-15T17:01:02+00:00Added an answer on June 15, 2026 at 5:01 pm

    Although somewhat redundant, a fairly neat self-documenting approach could be to have 3 methods rather than one;

    Make your original method private, and make one small change which would be to add your static final int EXECUTE_ALL = -1 and use that in your original method, then add the two new methods;

    public void processWidget(int wID) throws IllegalArgumentException {
        if(wID == EXECUTE_ALL) throw new IllegalArgumentException();
        originalMethod(wID);
    }
    
    public void processAllWidgets() {
        originalMethod(EXECUTE_ALL);
    }
    

    It makes your class a little more cluttered, but as far as the exposed methods go, it is clearer and hopefully foolproof. You could alter it not to throw an exception and just ignore any invalid ids, that just depends on your situation.

    This approach of course has the major downside that it changes how the class appears to other classes, breaking everything that currently uses the, now private, originalMethod().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you

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.