I had a discussion with a co-worker about interface members having parameters that some implementations don't use.
Say I have an interface
interface IDoctor
{
string GetMedicalOpinion(Age age, Weight weight, SleepSchedule sleepSchedule, Symptom symptom)
}
Implementations of IDoctor won't necessarily use every method parameter:
class BadDoctor : IDoctor
{
string GetMedicalOpinion(Age age, Weight weight, SleepSchedule sleepSchedule, Symptom symptom)
{
if (Symptom == Symptoms.Fatigue)
return "You're sick!";
return "You're okay."
}
}
My co-worker thinks that the interface is poorly defined because there are implementations that don't use every method parameter.
I think this is fine. Having one method with all of the parameters gives us polymorphism -- we can swap in IDoctors of varying competence without modifying the surrounding code. Also, the parameters are immutable data, so consumers shouldn't have any expectation that the function does anything other than return a string. Finally, the parameters are all things that consumers should have naturally, so providing the info isn't a burden on consumers.
Does this violate contemporary design principles? Is there really a problem with interface members having parameters that are used by some implementations, but ignored by others?
IDoctor
, notIDoctorService
. If I had aclass
implementing theIDoctor
interface takingSymptoms
as a constructor parameter, I would assume the symptoms actually belong to the doctor and not the patient the doctor is supposed to inspect. Also what do you do if you want to inspect more patients with the same doctor? It becomes impossible to do.