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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:29:18+00:00 2026-05-26T06:29:18+00:00

I often have a situation where I need to do: function a1() { a

  • 0

I often have a situation where I need to do:

function a1() {
 a = getA;
 b = getB;
 b.doStuff();
 ....  // do some things
 b.send()
 return a - b;
}

function a2() {
 a = getA;
 b = getB;
 b.doStuff();
 ....  // do some things, but different to above
 b.send()
 return a - b;
}

I feel like I am repeating myself, yet where I have ...., the methods are different, have different signatures, etc..

What do people normally do? Add an if (this type) do this stuff, else do the other stuff that is different? It doesn’t seem like a very good solution either.

  • 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-26T06:29:18+00:00Added an answer on May 26, 2026 at 6:29 am

    Polymorphism and possibly abstraction and encapsulation are your friends here.

    You should specify better what kind of instructions you have on the .... // do some things part. If you’re always using the same information, but doing different things with it, the solution is fairly easy using simple polymorphism. See my first revision of this answer. I’ll assume you need different information to do the specific tasks in each case.

    You also didn’t specify if those functions are in the same class/module or not. If they are not, you can use inheritance to share the common parts and polymorphism to introduce different behavior in the specific part. If they are in the same class you don’t need inheritance nor polymorphism.


    In different classes

    Taking into account you’re stating in the question that you might need to make calls to functions with different signature depending on the implementation subclass (for instance, passing a or b as parameter depending on the case), and assuming you need to do something with the intermediate local variables (i.e. a and b) in the specific implementations:

    Short version: Polymorphism+Encapsulation: Pass all the possible in & out parameters that every subclass might need to the abstract function. Might be less painful if you encapsulate them in an object.

    Long Version
    I’d store intermediate state in generic class’ member, and pass it to the implementation methods. Alternatively you could grab the State from the implementation methods instead of passing it as an argument. Then, you can make two subclasses of it implementing the doSpecificStuff(State) method, and grabbing the needed parameters from the intermediate state in the superclass. If needed by the superclass, subclasses might also modify state.

    (Java specifics next, sorry)

    public abstract class Generic {
      private State state = new State();
      public void a() {
         preProcess();
         prepareState();
         doSpecificStuf(state);
         clearState();
         return postProcess();
      }
      protected void preProcess(){ 
         a = getA;
         b = getB;
         b.doStuff();
      }
      protected Object postProcess(){ 
         b.send()
         return a - b;
      }
      protected void prepareState(){ 
         state.prepareState(a,b);
      }
      private void clearState() { 
         state.clear();
      }
      protected abstract doSpecificStuf(State state);
    }
    public class Specific extends Generic { 
       protected doSpecificStuf(State state) { 
           state.getA().doThings();
           state.setB(someCalculation);
       }
    }
    public class Specific2 extends Generic { 
       protected doSpecificStuf(State state) { 
           state.getB().doThings();
       }
    }
    

    In the same class

    Another possibility would be making the preProcess() method return a State variable, and use it inthe implementations of a1() and a2().

    public class MyClass {
      protected State preProcess(){ 
         a = getA;
         b = getB;
         b.doStuff();
         return new State(a,b);
      }
      protected Object postProcess(){ 
         b.send()
         return a - b;
      }
      public void a1(){
          State st = preProcess();
          st.getA().doThings();
          State.clear(st);
          return postProcess();
      }
      public void a2(){
          State st = preProcess();
          st.getB().doThings();
          State.clear(st);
          return postProcess();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is often situation, but here is latest example: Companies have various contact data
Often I have a situation when I meed to show whole picture but clarify
I have a situation where I need to write some unit tests for some
I'm often confronted with the following situation: I have some data in a table
I often have a situation where I want to do some conditional logic and
I often have the following situation in my PowerShell code: I have a function
I often have data in Excel or text that I need to get into
I often have a subroutine in Perl that fills an array with some information.
I often have to deal with XML documents that contain namespaced elements, but doesn't
I have found that I often have to implement some sort of a scheduler

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.