Reducing Code Repetition: Calling functions with slightly different signatures

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2010-03-31T21:13:11Z Indexed on 2010/03/31 21:33 UTC
Read the original article Hit count: 351

Filed under:
|

Suppose I have two functions which look like this:

public static void myFunction1(int a, int b, int c, string d)
{
    //dostuff
    someoneelsesfunction(c,d);
    //dostuff2
}

public static void myFunction2(int a, int b, int c, Stream d)
{
    //dostuff
    someoneelsesfunction(c,d);
    //dostuff2
}

What would be a good way to avoid repeated dostuff?

Ideas I've thought of, but don't like:

  1. I could make d an object and cast at runtype based on type, but this strikes me as not being ideal; it removes a type check which was previously happening at compile time.
  2. I could also write a private helper class that takes an object and write both signatures as public functions.
  3. I could replace dostuff and dostuff2 with delegates or function calls or something.

© Stack Overflow or respective owner

Related posts about c#

Related posts about refactoring