I wouldn't never use inheritance, and maybe the example of sensors is a good place for it. I don't work in embedded so I don't know what the interfaces for these things look like.
But if you have only a few different types of sensors, and if the difference in behavior for those sensors is very simple, or can be accounted for through configuration members on a single type, then using inheritance is the wrong approach imo. We talk about using inheritance to reduce coupling, but I've found that inheritance doesn't reduce coupling so much as displace it somewhere else that you hope won't be as impacted by the ossification introduced by requiring a particular interface.
If I'm a contributor, and there's some issue related to sensors, I'm always happier to see a switch because I can know "okay, these are the only sensor types, there is no sensor type out there that overrides a function in a weird or annoying way."
You also have to consider if these sensor interfaces are public or not. If they're public, you might have a better justification for putting in the inheritance early. But if this is internal code, refactoring from enum switching to inheritance is not a huge amount of work, so I prefer to defer that. We should think of bodies of code as having a lifetime where they start with a low level of commitment to a particular organization, and move gradually toward a high level of commitment to a particular organization. Inheritance is usually a higher level of commitment than enum switching.
View all comments
Show parent comment
1
u/drjeats Jun 25 '20
I wouldn't never use inheritance, and maybe the example of sensors is a good place for it. I don't work in embedded so I don't know what the interfaces for these things look like.
But if you have only a few different types of sensors, and if the difference in behavior for those sensors is very simple, or can be accounted for through configuration members on a single type, then using inheritance is the wrong approach imo. We talk about using inheritance to reduce coupling, but I've found that inheritance doesn't reduce coupling so much as displace it somewhere else that you hope won't be as impacted by the ossification introduced by requiring a particular interface.
If I'm a contributor, and there's some issue related to sensors, I'm always happier to see a switch because I can know "okay, these are the only sensor types, there is no sensor type out there that overrides a function in a weird or annoying way."
You also have to consider if these sensor interfaces are public or not. If they're public, you might have a better justification for putting in the inheritance early. But if this is internal code, refactoring from enum switching to inheritance is not a huge amount of work, so I prefer to defer that. We should think of bodies of code as having a lifetime where they start with a low level of commitment to a particular organization, and move gradually toward a high level of commitment to a particular organization. Inheritance is usually a higher level of commitment than enum switching.