Feature Envy: I Wanna Hold Your Hand
Feature Envy Example
In the
Customer
module below, the function getMobilePhoneNumber()
provides a North American-formatted mobile Phone
number:
typedef struct Phone
{
char m_unformattedNumber[UNFORMATTED_PHONE_SIZE];
} Phone;
typedef struct Customer
{
Phone mobilePhone;
} Customer;void getMobilePhoneNumber(Customer* customer, char* out, int outLen)
{
char buf1[AREA_CODE_SIZE];
char buf2[PREFIX_SIZE];
char buf3[NUMBER_SIZE];
snprintf(out, outLen, "(%s) %s-%s",
subString(buf1, customer->mobilePhone.m_unformattedNumber, 0, 2),
subString(buf2, customer->mobilePhone.m_unformattedNumber, 3, 5),
subString(buf3, customer->mobilePhone.m_unformattedNumber, 6, 9));
}
See how
Customer
reaches into Phone
’s data to format the number?
Feature Envy often shows up as a misplaced responsibility.
typedef struct Phone
{
char m_unformattedNumber[UNFORMATTED_PHONE_SIZE];
} Phone;void getFormattedPhoneNumber(Phone* phone, char* out, int outLen)
{
char buf1[AREA_CODE_SIZE];
char buf2[PREFIX_SIZE];
char buf3[NUMBER_SIZE];
snprintf(out, outLen, "(%s) %s-%s",
subString(buf1, phone->m_unformattedNumber, 0, 2),
subString(buf2, phone->m_unformattedNumber, 3, 5),
subString(buf3, phone->m_unformattedNumber, 6, 9));
}typedef struct Customer
{
Phone mobilePhone;
} Customer;void getMobilePhoneNumber(Customer* customer, char* out, int outLen)
{
getFormattedPhoneNumber(&customer->mobilePhone, out, outLen);
}
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(...)`` |