While reviewing some code, I noticed the opportunity to change it to use generics. The (obfuscated) code looks like:
public void DoAllTheThings(Type typeOfTarget, object[] possibleTargets)
{
var someProperty = typeOfTarget.GetProperty(possibleTargets[0]);
...
}
This code could be replaced by generics, like so:
public void DoAllTheThings<T>(object[] possibleTargets[0])
{
var someProperty = type(T).getProperty(possibleTargets[0]);
...
}
In researching the benefits and shortcomings of this approach I found a term called generic abuse. See:
- Protecting the uninitiated (developer) from generics
- https://stackoverflow.com/questions/28203199/is-this-an-abuse-of-generics
- https://codereview.stackexchange.com/q/60695
My question comes in two parts:
- Are there any benefits to moving to generics like this? (Performance? Readability?)
- What is Generics Abuse? And is using a generic every time there is a type parameter an abuse?