Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

OOP Golden Rule

58 views
Skip to first unread message

Mr Flibble

unread,
Jul 26, 2016, 4:50:56 PM7/26/16
to
If nothing differentiates two object types (classes) except
the values of common (base class) properties and
behaviours then do not create a class for either.

Example: a weapon class is fine but a sword class
and a knife class are not as there are no intrinsic differences
between a knife and a sword.

This mirrors the OOA/D principle of preferring
object composition over class inheritance.

/Flibble

Daniel

unread,
Jul 27, 2016, 3:38:28 PM7/27/16
to
On Tuesday, July 26, 2016 at 4:50:56 PM UTC-4, Mr Flibble wrote:
> If nothing differentiates two object types (classes) except
> the values of common (base class) properties and
> behaviours then do not create a class for either.
>
> Example: a weapon class is fine but a sword class
> and a knife class are not as there are no intrinsic differences
> between a knife and a sword.
>
It depends. Supposing the base class had an abstract method slashNeck, a derived
knife class might print "F**k!!!", while a derived sword class might print, well,
nothing. Generally the decision about whether a class is needed depends on purpose.

Daniel

Mr Flibble

unread,
Jul 27, 2016, 3:44:26 PM7/27/16
to
"It depends" applies to most if not all generalizations. My
generalization is still a good rule of thumb. :)

/Flibble

0 new messages