Reputation: 2026
i'm trying to write better code.
i have a function deals two different types of input, the function is long and will be only small difference between two type. currently i wrote like this:
function(typeA inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeB inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
I want to know is there a better way that i won't need to put so many repeated code, maybe just write one function can switch type...
typeA and typeB are different base class.
currently A has 5 items, and B has 3.
Upvotes: 0
Views: 118
Reputation: 4930
If your code is that simple, just create a new function to do the repeated work and call it twice (as Karl did).
If you want to leave your class customizable (e.g. you're writing a framework and want to let your users specify different behaviors for new types they might have), you should consider using the Template Method Pattern, in which specific methods are defined by subclasses (and you use polymorphism/overloads). You can customize your class easily using inheritance.
http://en.wikipedia.org/wiki/Template_method_pattern
If the problem is even more complex, you can use the Strategy Pattern, in which your whole algorithm is defined in another class:
http://en.wikipedia.org/wiki/Strategy_pattern
Upvotes: 0
Reputation: 2112
you can use more generic type or parent type. for example,
function(object input)
{
if (input is TypeA)
{
}
else if (input is TypeB)
{
}
}
Upvotes: 0
Reputation: 4381
Try this, assuming that typeA
and typeB
both inherit from a BaseType
base class (or interface) :
SharedFunction(BaseType inputs)
{
......
......
<lots of same code>
......
......
}
FunctionA(typeA inputs)
{
SharedFunction(inputs)
<small different code part>
}
FunctionB(typeB inputs)
{
SharedFunction(inputs)
<small different code part>
}
Upvotes: 3
Reputation: 508
As Karl mentioned, if TypeA and TypeB are derived from the same class, then just use the base class as the parameter type. If not, I would create a simple interface, and then derive TypeA and TypeB from that, and pass the interface as the parameter type.
public interface IMyType
{
// Properties you need both types to have
// Methods/Functions you need both types to have
}
Then, do
function(IMyType obj)
{
// Logic
}
Hope that helps.
Upvotes: 0
Reputation: 20257
Assuming these methods are all in the same class (so no base classes), I would consider using an Action of Func as a parameter to the initial method like so:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
You can then call the method like so:
Method(SubMethod1);
Method(SubMethod2);
Upvotes: 1
Reputation: 34844
Assuming both types TypeA
and TypeB
both derive from the same base class, then generalize your function parameter to the base type and then have conditional logic for sections that are "different" based upon the type, this will allow for one method to handle both types, like this:
function(typeBase inputs)
{
......
......
<lots of same code>
......
......
if(inputs is TypeA)
{
// Do stuff here for TypeA
}
else if(inputs is TypeB)
{
// Do stuff here for TypeB
}
}
Upvotes: 0