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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:16:38+00:00 2026-05-31T17:16:38+00:00

As much as possible I want to avoid unnecessary duplication of code. In my

  • 0

As much as possible I want to avoid unnecessary duplication of code. In my scenario described below I have used Action delegates to avoid code duplication. However, when using the Action delegate approach, the code becomes approximately 50%-80% slower.

Is there a more efficient approach from a performance perspective than Action delegates to avoid code duplication in the scenario described below?

I have two methods that are equivalent except for the innermost statement of a lengthy loop:

public T[] MethodA<T>(T[] from)
{
  ...
  for (var i = 0; i < len; ++i)
  {
    var j = GetIndex(i);
    to[j] = from[i];    // This statement differs in MethodA and MethodB
  }
  ...
  return to;
}

public T[] MethodB<T>(T[] from)
{
  ...
  for (var i = 0; i < len; ++i)
  {
    var j = GetIndex(i);
    to[i] = from[j];    // This statement differs in MethodA and MethodB
  }
  ...
  return to;
}

To avoid code duplication I have implemented a helper method that takes an Action delegate. Invocation of the delegate replaces the variable statement, like this:

private T[] HelperMethod<T>(T[], Action<T[], T[], int, int> action)
{
  ...
  for (var i = 0; i < len; ++i)
  {
    var j = GetIndex(i);
    action(from, to, i, j);    // Invoke the Action delegate
  }
  ...
  return to;
}

Then I can reduce MethodA and MethodB as follows:

public T[] MethodA<T>(T[] from)
{
  return HelperMethod(from, (src, dest, src_idx, dest_idx) => dest[dest_idx] = src[src_idx]);
}

public T[] MethodB<T>(T[] from)
{
  return HelperMethod(from, (src, dest, dest_idx, src_idx) => dest[dest_idx] = src[src_idx]);
}

Note that the only difference between the refactored MethodA and MethodB is the order of the src_idx and dest_idx in the Action signatures of the HelperMethod calls.

  • 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-31T17:16:39+00:00Added an answer on May 31, 2026 at 5:16 pm

    You could pass a parameter to the method and do A or B depending on the parameter.
    I don’t like it but I’m just saying it could be done. I actually prefer the Action way.

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

Sidebar

Related Questions

I want to avoid code duplication as much as possible. Suppose I have a
I want DRY/reuse as much editor code (View and Model) as possible. Some of
I want get as much as possible from Redis + Hiredis + libevent. I'm
I'm trying to avoid using Interface Builder as much as possible. At the moment
I am trying to avoid reloading as much as possible when browsing back and
In our application we have two use cases in which we want to avoid
I need to do as much as possible on the client side. In more
I try to use single quotes as much as possible and I've noticed that
I am writing an SSH client using only bash (as much as possible) and
I would like to cache my website with memcache as much as possible. There

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.