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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:20:20+00:00 2026-06-01T03:20:20+00:00

We have App A as main app. Now we build from it App B

  • 0

We have App A as main app. Now we build from it App B which uses a subset of App A’s functionality.

App A stays like it is whereas app B only uses a subset of A

So I want to refactor the function without or with as little dublication as possible and with maximum readability.

So the function looks like this (it is actually longer, this is an excerpt):

  class SomeClass {

    Data prepareData() {

     if (this.bothId==1 || this.appAid=2 /*or only relevant for appA*/) {       
        if(this.data==null) { /*appA*/
            appAdoSmth(); /*appA*/
        }
        boolean merge=false; /*appA*/
        if (this.data==null) { /*appA*/
          merge=appAanalyze(data); /*appA*/
        }
        bothPrepare(merge); 
    } else if (bothIsRelevant()) {
      if(appArelevant()) { /*appA*/
        data=appAprepare(); /*appA*/
      } else {
        data=prepareBoth(); 
      }     
      bothUpdateSomeValue();
    }

  }

How would you do it?

  • 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-01T03:20:22+00:00Added an answer on June 1, 2026 at 3:20 am

    Other Answers address the general question of how to refactor code. They offer good advice, but I don’t think it is what you are asking.

    I think you are asking about possible refactorings of the code in your question.

    It is hard to give an answer that is generally applicable, or even specifically applicable. (The sample code isn’t your real code, and it is a little difficult to understand what it actually “means”).

    • AndreasD gives one approach: break the big complicated nested if into separate methods.

    • Another approach is to use the Stragegy design pattern. Separate the code that is specific to each app into strategy classes. For example:

      interface Strategy {
          Data prepareData();
      }
      
      class GeneralStrategy implements Strategy {
          Data prepareData() {
              // do general preparation
          }
      }
      
      class App1Strategy extends GeneralStrategy {
          Data prepareData() {
              // do app1-specific preparation
              super.prepareData();
              // do more app1-specific preparation
          }
      }
      

    and so on.

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

Sidebar

Related Questions

I have a winforms app and the main (and only) form has several buttons.
I have an app that opens a non-modal form from the main form. The
I have an Android App which consists on different modules. The Main module is
I have an app using a ListView as a main screen. Each row displays
i have a mvvm app that the main window is a tab control. i
When I have one app.config in my main project I always have to duplicate
If I have a whole bunch of requires in my main app file: require
I have a widget that supposed to call an Activity of the main app
I have a main C++ app built in Visual Studio 2005, called A.exe. It
I have an Android app with a ListActivity in the main view. The list

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.