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

  • Home
  • SEARCH
  • 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 7183581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:00:44+00:00 2026-05-28T18:00:44+00:00

Is it possible to avoid code duplication in such cases? (Java code) void f()

  • 0

Is it possible to avoid code duplication in such cases? (Java code)

void f()
{
    int r;
    boolean condition = true;
    while(condition)
    {
        // some code here (1)

        r = check();
        if(r == 0)
            break ;
        else if(r == 1)
            return ;
        else if(r == 2)
            continue ;
        else if(r == 3)
            condition = false;

        // some code here (2)

        r = check();
        if(r == 0)
            break ;
        else if(r == 1)
            return ;
        else if(r == 2)
            continue ;
        else if(r == 3)
            condition = false;

        // some code here (3)
    }
    // some code here (4)
}

int check()
{
    // check a condition and return something
}

A possible solution may be using Exceptions, but that doesn’t seem to be a good practice.
Is there any so-called good pattern of program flow control in such cases? For example, a way to call break ; from inside the check() function.
(Possibly in other programming languages)

  • 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-05-28T18:00:44+00:00Added an answer on May 28, 2026 at 6:00 pm

    Some good answers (especially @Garrett’s just now) to a tough question but I’ll add my $0.02 for posterity.

    There is no easy answer here about how to refactor this block without seeing the actual code but my reaction to it is that it needs to be redesigned.

    For example, a way to call break ; from inside the check() function. (Possibly in other programming languages)

    If you are asking for a different break that Java does not support (without a hack) and having the duplicated check() and various different loop exit/repeat code indicates to me that this is a large and complicated method. Here are some ideas for you to think about:

    • Each of the some code here blocks are doing something. If you pull those out to their own methods, how does that change the loop?

    • Maybe break the loop down into a series of comments. Don’t get deep into the code but think about it conceptually to see if a different configuration drops out.

    • Have you had another developer in your organization who is not involved with this code take a look at it? If you explain in detail how the code works someone they may see some patterns that you are not since you are in the weeds.

    I also think that @aix’s idea of a finite state machine is a good one but I’ve needed to use this sort of mechanism very few times in my programming journeys — mostly during pattern recognition. I suspect that a redesign of the code with smaller code blocks pulled into methods will be enough to improve the code.

    If you do want to implement the state machine here are some more details. You could have a loop that was only running a single switch statement that called methods. Each method would return the next value for the switch. This doesn’t match your code completely but something like:

    int state = 0;
    WHILE: while(true) {
        switch (state) {
           case 0:
                // 1st some code here
                state = 1;
                break;
           case 1:
                state = check();
                break;
           case 2:
                return;
           case 3:
                break WHILE;
           case 4:
                // 2nd some code
                state = 1;
                break;
            ...
        }
     }
    

    Hope some of this helps and best of luck.

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

Sidebar

Related Questions

Is it generally possible to avoid moving some controls while Scrolling? In particular is
As much as possible I want to avoid unnecessary duplication of code. In my
Just an academical question: Is it possible to avoid int casting when comparing Enum
I'm implementing some code using the java.util.concurrency framework. I will be passing a collection
I use some maps while I code : imap ( ()<C-[>i imap [ []<C-[>i
Trying to mirror my API responses with as little code duplication as possible and
Is it possible to avoid list property tags when serializing? //[Serializable()] - removed, unnecessary
Is it possible to avoid the automatic collapse of a Silverlight ComboBox after LostFocus?
Is it possible to avoid deploying the artifact that is built according to the
Is it possible to avoid the more tab to show up in my tab-based

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.