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
c#
|refactoring
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:
- 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.
- I could also write a private helper class that takes an object and write both signatures as public functions.
- I could replace dostuff and dostuff2 with delegates or function calls or something.
© Stack Overflow or respective owner