Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 1.04 KB

feature-envy.md

File metadata and controls

41 lines (31 loc) · 1.04 KB

Feature Envy

A method cares more about another object's data than about its own.

What It Looks Like

def shipment_delivered?
  !shipment.delivery_date.to_s.strip.empty?
end
def invoice_total
  invoice.parts.map(&:price).reduce(:+) + invoice.labor
end

Why It Hurts

Feature envy may mask Duplicated Code since similar envious code may be repeated in multiple places. It is often associated with Message Chains, since the envious code asks for and manipulates another object's data and thus has to know about the types of those data.

How To Fix It

Use Move Method to move the envious code to the envied class. You may have to Extract Method first to isolate the code that should be moved.

Related Smells

Primitive Obsession is often a sign of hidden Feature Envy. If the primitive were replaced by a class you could modify, you could move the primitive-obsessed methods to it.