Skip to content

Commit a7881cd

Browse files
author
Ben Christel
committed
Add 'Feature Envy' smell and refactorings
1 parent 3f2c9f2 commit a7881cd

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

Diff for: docs/refactorings/move-method.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Move Method
2+
3+
## Example
4+
5+
### Before
6+
7+
```ruby
8+
class PaymentCalculator
9+
def invoice_total
10+
@invoice.parts.map(&:price).reduce(:+) + @invoice.labor
11+
end
12+
13+
# ...
14+
end
15+
```
16+
17+
### After
18+
19+
```ruby
20+
class PaymentCalculator
21+
# ...
22+
end
23+
24+
# ...
25+
26+
class Invoice
27+
def total
28+
parts.map(&:price).reduce(:+) + labor
29+
end
30+
31+
# ...
32+
end
33+
```

Diff for: docs/smells/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Fowler's book, _Refactoring: Improving the Design of Existing Code_.
4747
## Objects and Messages
4848

4949
- [**Null Check**](null-check.md). Fix with [Introduce Null Object](../refactorings/introduce-null-object.md) or [Introduce Continuation [BC]](../refactorings/introduce-continuation.md).
50-
- [**Feature Envy.**](http://wiki.c2.com/?FeatureEnvySmell) Fix with [Move Method](), possibly preceded by [Extract Method](../refactorings/extract-method.md).
50+
- [**Feature Envy.**](feature-envy.md) Fix with [Move Method](../refactorings/move-method.md), possibly preceded by [Extract Method](../refactorings/extract-method.md).
5151
- [**Primitive Obsession.**](http://wiki.c2.com/?PrimitiveObsession) Fix using [Replace Primitive with Object](). See also [Avoid Hashy Syntax In Ruby](http://wiki.c2.com/?AvoidHashySyntaxInRuby) for a language-specific refactoring technique.
5252
- [**Multiple Responsibilities.**](http://wiki.c2.com/?OneResponsibilityRule) See also [God Class](http://wiki.c2.com/?GodClass). TODO: how to fix?
5353
- [**Switch on Type.**](http://wiki.c2.com/?SwitchStatementsSmell) Fix using [Replace Conditional with Polymorphism]().

Diff for: docs/smells/feature-envy.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Feature Envy
2+
3+
A method cares more about another object's data than about
4+
its own.
5+
6+
## What It Looks Like
7+
8+
```ruby
9+
def shipment_delivered?
10+
shipment.delivery_date.to_s.strip.empty?
11+
end
12+
```
13+
14+
```ruby
15+
def invoice_total
16+
invoice.parts.map(&:price).reduce(:+) + invoice.labor
17+
end
18+
```
19+
20+
## Why It Hurts
21+
22+
Feature envy may mask [Duplicated Code](duplicated-code.md)
23+
since similar envious code may be repeated in multiple
24+
places. It is often associated with [Message
25+
Chains](message-chain.md), since the envious code asks for
26+
and manipulates another object's data and thus has to know
27+
about the types of those data.
28+
29+
## How To Fix It
30+
31+
Use [Move Method](../refactorings/move-method.md) to move
32+
the envious code to the envied class. You may have to
33+
[Extract Method](../refactorings/extract-method.md) first to
34+
isolate the code that should be moved.
35+
36+
## Related Smells
37+
38+
[Primitive Obsession](primitive-obsession.md) is often a
39+
sign of hidden Feature Envy. If the primitive were replaced
40+
by a class you could modify, you could move the
41+
primitive-obsessed methods to it.

0 commit comments

Comments
 (0)