Feature Envy Code Smell Resolution with examples
Today in this article, we will see Feature Envy Code Smell Resolution with examples.
Methods used in the application could be used to expose the internal or inner workings of other classes. That means a feature in a class may be using too much functionality from the feature of another class’s method. When you see such chatty communication from features of different classes there is clear visibility of code smell.
We will cover the below aspects of the article,
Feature envy smells come under a category called ‘Couplers’. In our last article, we understood couplers with the smell of Inappropriate Intimacy. Feature envy is similar to Inappropriate Intimacy but here instead of class relationship we talk about features/functions/method coupling.
“Code is maintained more often than it is written”
As per the principle of single responsibility, a function or a class should do one task at a time. A class should contain within itself both the behavior and methods (i.e. data).
“Code is read more often than it is written”
In object-oriented programming, there are multiple means of making classes that can interact with each other like using composition, aggregation, and inheritance. and transfer data to and from each other ultimately achieving required business functionality.
Characteristics of Feature Envy Smell
- A classic example could be where you sight a ‘method at the wrong place’.
- The class uses a significant number of methods and fields of other Classes (being used more than the class where it is defined)
- A properties /fields of a class are used by/in other class’s features (more than in the class where it is defined).
- A method being used to expose internal of other classes.
Cons
- High maintenance.
- Difficult to enhance/extend features.
- Demote readability and hard to understand.
Resolutions techniques
- Move Methods
- Move Fields
- Extract Method
Let’s look at each code recipe with examples in detail,
Move Methods
- Move methods to the right place
- Move tangled code to own methods
Examples – Feature Envy smell
Here we will use the same example as we used for the resolution of code smell -Inappropriate Intimacy,
namespace FeatureEnvySmell
{
class ContactInfo
{
public string GetStreetName()
{
return "1 Medison Square";
}
public string GetCity()
{
return "NewYork";
}
public string GetState()
{
return "NY";
}
}
class User
{
private ContactInfo _contactInfo;
User(ContactInfo contactInfo)
{
_contactInfo = contactInfo;
}
public string GetFullAddress(ContactInfo info)
{
string city = info.GetCity();//1
string state = info.GetState();//2
string streetName = info.GetStreetName();//3
return streetName + ";" + city + ";" + state;
}
}
}
Feature Envy smell Resolution
namespace FeatureEnvySmellResolved
{
class ContactInfo
{
public string GetStreetName()
{
return "1 Medison Square";
}
public string GetCity()
{
return "NewYork";
}
public string GetState()
{
return "NY";
}
public string GetFullAddress(ContactInfo info)
{
string city = info.GetCity();//1
string state = info.GetState();//2
string streetName = info.GetStreetName();//3
return streetName + ";" + city + ";" + state;
}
}
class User
{
private ContactInfo _contactInfo;
User(ContactInfo contactInfo)
{
_contactInfo = contactInfo;
}
}
}
Extract methods
Each method should do one task at a time.
I shall share an example of this technique for the same soon in my next article soon.
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.