Awesome Code -Inappropriate Intimacy Code Smell resolution
Today in this article, we will understand Inappropriate Intimacy Code Smell characteristics and how to fix such issues in the code.
Classes used in the application could become close buddies and spend more time with each other :). This may be found to be okay in real life. But not in application development, one should remain watchful while writing the code.
This code smell comes under a category called Couplers.
“Code is maintained 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 techniques
- and doing data transfers to and from each other to prepare business functionality.
“Code is read more often than it is written”
Characteristics of Inappropriate Intimacy smell
- Classes used in the application become close buddies and spend more time with each other.
- Classes interface with each other in an inappropriate way.
- Classes are tightly coupled.
- Each 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 classes’ features (more than in the class where it is defined).
- Bi-directional behavior between classes creates tight inter-dependency.
Cons of Inappropriate Intimacy
- High maintenance.
- Difficult to enhance/extend features.
- Demote readability and hard to understand.
Resolutions Techniques
- Move Methods
- Move Fields
- Bidirectional Association to Unidirectional
- Extract Method
- Extract Class
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
Example :
Code with smell
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;
}
}
}
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;
}
}
}
- Move Fields
- Move Fields to the right place
Example: similar to above…
So one can try to move related methods or fields to the appropriate class as shown above to remediate this type of code smell.
Bidirectional to Unidirectional Association
Change the bi-directional Association of classes to Unidirectional.
Cons of Bidirectional dependency
- Bi-directional behavior could turn costlier considering the maintenance of code.
- Create a tight coupling of code.
- Creating interdependency of classes and change in one class could affect change in other classes.
- Could cause issues like unused references which could lead to memory leak issues.
Example:
With CodeSmell
namespace BiDirectionCodeSmell
{
class A
{
private B b;
}
class B
{
private A a;
}
}
After CodeSmell resolution
namespace FixedCodeSmellApporoach1
{
class A
{
private B b;
}
class B
{
}
}
OR
namespace FixedCodeSmellApproach2
{
class A
{
private B b;
A(IClassB classb)
{
b = (B)classb;
}
}
interface IClassB
{
}
class B:IClassB
{
}
}
“One dimensional association using DI (dependency injection) is a good example. Recent .NET Core framework for WebAPI pipeline does already provides inbuilt support for IOC and help us injecting required classes as needed through explicit dependency injection principle”.
Extract methods
Each method should do one task at a time.
I shall share an example of this technique for the same soon
Extract class
- Classes should know as little as possible about each other.
- Classes should follow the single responsibility principle.
I shall share an example of this technique for the same soon.
References :
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.