Feature Envy: I Wanna Hold Your Hand
Feature Envy Example
In the
Customer
class below, the method getMobilePhoneNumber()
provides a North American-formatted mobile Phone
number:
class Phone
{
public:
Phone(string unformattedNumber) : m_unformattedNumber(unformattedNumber) {}
string getAreaCode() const;
string getPrefix() const;
string getNumber() const;private:
string m_unformattedNumber;
};class Customer…
public:
string getMobilePhoneNumber() const
{
return "(" + m_mobilePhone.getAreaCode() + ") " +
m_mobilePhone.getPrefix() + "-" +
m_mobilePhone.getNumber();
} private:
Phone m_mobilePhone;
See how
Customer
reaches into Phone
’s data to format the number?
Feature Envy often shows up as a misplaced responsibility.
class Phone
{
public:
Phone(string unformattedNumber) : m_unformattedNumber(unformattedNumber) {} string toFormattedString() const
{
return "(" + getAreaCode() + ") " + getPrefix() + "-" + getNumber();
}private:
string getAreaCode() const;
string getPrefix() const;
string getNumber() const;
string m_unformattedNumber;
};class Customer…
public:
string getMobilePhoneNumber() const
{
return m_mobilePhone.toFormattedString();
} private:
Phone m_mobilePhone;
Now
Customer
relies on Phone
to do the formatting.
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |