Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

4 in 1 Code Smells

Incomplete Library Class


Data Class
Refused Bequest
Comments
Incomplete Library Class
Data Class Incomplete Library Class
Refused Bequest
Comments
Reuse is often touted as the purpose of
objects. We think reuse is overrated
(we just use). However, we can't deny
that much of our programming skill is
based on library classes so that nobody
can tell whether we've forgotten our
sort algorithms.
Incomplete Library Class
Data Class
Refused Bequest
Comments Builders of library classes are rarely
omniscient. We don't blame them for
that; after all, we can rarely figure out a
design until we've mostly built it, so
library builders have a really tough job.
The trouble is that it is often bad form,
and usually impossible, to modify a
library class to do something you'd like
it to do. This means that tried-and-true
tactics such as Move Method lie
useless.
Incomplete Library Class
Data Class
Refused Bequest
Comments We have a couple of special-purpose
tools for this job. If there are just a
couple of methods that you wish the
library class had, use Introduce Foreign
Method. If there is a whole load of
extra behavior, you need Introduce
Local Extension.
Incomplete Library Class Introduce Foreign Method
Data Class
Refused Bequest
Comments
Incomplete Library Class Introduce Foreign Method
Data Class
Refused Bequest
Comments

Create a method in the client class with an instance of the server class as its
first argument.
Incomplete Library Class Introduce Foreign Method
Data Class
Refused Bequest
Comments

Create a method in the client class with an instance of the server class as its
first argument.
Incomplete Library Class
Data Class Data Class
Refused Bequest
Comments
Data classes are like children. They are
okay as a starting point, but to
participate as a grownup object, they
need to take some responsibility.
Incomplete Library Class
Data Class
Refused Bequest
Comments These are classes that have fields,
getting and setting methods for the
fields, and nothing else. Such classes
are dumb data holders and are almost
certainly being manipulated in far too
much detail by other classes. In early
stages these classes may have public
fields.
Incomplete Library Class
Data Class
Refused Bequest
Comments If so, you should immediately
apply Encapsulate Field before anyone
notices. If you have collection fields,
check to see whether they are properly
encapsulated and apply Encapsulate
Collection if they aren't. Use Remove
Setting Method on any field that
should not be changed.
Incomplete Library Class Encapsulate Field
Data Class
Refused Bequest
Comments
Incomplete Library Class Encapsulate Field
Data Class
Refused Bequest
Comments

Make it private and provide accessors.


Incomplete Library Class Encapsulate Field
Data Class
Refused Bequest
Comments

Make it private and provide accessors.


Incomplete Library Class
Data Class Refused Bequest
Refused Bequest
Comments
Subclasses get to inherit the methods
and data of their parents. But what if
they don't want or need what they are
given? They are given all these great
gifts and pick just a few to play with.
Incomplete Library Class
Data Class
Refused Bequest
Comments The traditional story is that this means
the hierarchy is wrong. You need to
create a new sibling class and use Push
Down Method and Push Down Field to
push all the unused methods to the
sibling. That way the parent holds only
what is common. Often you'll hear
advice that all superclasses should be
abstract.
Incomplete Library Class
Data Class
Refused Bequest
Comments The smell of refused bequest is much
stronger if the subclass is reusing
behavior but does not want to support
the interface of the superclass. We
don't mind refusing implementations,
but refusing interface gets us on our
high horses. In this case, however, don't
fiddle with the hierarchy; you want to
gut it by applying Replace Inheritance
with Delegation.
Incomplete Library Class
Data Class Comments
Refused Bequest
Comments
Don't worry, we aren't saying that
people shouldn't write comments.
In our olfactory analogy, comments
aren't a bad smell; indeed they are a
sweet smell. The reason we mention
comments here is that comments
often are used as a deodorant. It's
surprising how often you look at
thickly commented code and notice
that the comments are there
because the code is bad.
Incomplete Library Class
Data Class
Refused Bequest
Comments
If you need a comment to explain what
a block of code does, try Extract
Method. If the method is already
extracted but you still need a comment
to explain what it does, use Rename
Method. If you need to state some
rules about the required state of the
system, use Introduce Assertion.
Incomplete Library Class
Data Class
Refused Bequest
Comments
A good time to use a comment is when
you don't know what to do. In addition
to describing what is going on,
comments can indicate areas in which
you aren't sure. A comment is a good
place to say why you did something.
This kind of information helps future
modifiers, especially forgetful ones.

You might also like