Welcome to the very first lesson of the "Clean Code with Multiple Classes" course! 🎉 This course aims to guide you in writing code that's easy to understand, maintain, and enhance. Within the broader scope of clean coding, effective class collaboration is crucial for building well-structured applications. In this lesson, we will delve into the intricacies of class collaboration and coupling — key factors that can make or break the maintainability of your software. Specifically, we'll address some common "code smells" that indicate problems in class interactions and explore ways to resolve them.
Let's dive into the challenges of class collaboration by focusing on four common code smells:
- Feature Envy: Occurs when a method in one class is overly interested in methods or data in another class.
- Inappropriate Intimacy: Describes a situation where two classes are too closely interconnected, sharing private details.
- Message Chains: Refers to sequences of method calls across several objects, indicating a lack of clear abstraction.
- Middle Man: Exists when a class mainly delegates its behavior to another class without adding functionality.
Understanding these code smells will enable you to improve your class designs, resulting in cleaner and more maintainable code.
These code smells can significantly impact system design and maintainability. Let's consider their implications:
- They can lead to tightly coupled classes, making them difficult to modify or extend. 🔧
- Code readability decreases, as it becomes unclear which class is responsible for which functionality.
Addressing these issues often results in code that's not only easier to read but also more flexible and scalable. Tackling these problems can markedly enhance software architecture, making it more robust and adaptable.
Feature Envy occurs when a method in one class is more interested in the fields or methods of another class than its own. Here's an example:
Python1class ShoppingCart: 2 def __init__(self): 3 self.items = [] 4 5 def calculate_total_price(self): 6 total = 0 7 for item in self.items: 8 total += item.get_price() * item.get_quantity() 9 return total 10 11class Item: 12 def __init__(self, price, quantity): 13 self.price = price 14 self.quantity = quantity 15 16 def get_price(self): 17 return self.price 18 19 def get_quantity(self): 20 return self.quantity
In this scenario, calculate_total_price()
in ShoppingCart
overly accesses data from Item
, indicating feature envy.
To refactor, consider moving the logic to the Item
class:
Python1class ShoppingCart: 2 def __init__(self): 3 self.items = [] 4 5 def calculate_total_price(self): 6 total = 0 7 for item in self.items: 8 total += item.calculate_total() 9 return total 10 11class Item: 12 def __init__(self, price, quantity): 13 self.price = price 14 self.quantity = quantity 15 16 def calculate_total(self): 17 return self.price * self.quantity
Now, each Item
calculates its own total, reducing dependency and distributing responsibility appropriately. ✔️
Inappropriate Intimacy occurs when a class is overly dependent on the internal details of another class. Here's an example:
Python1class Library: 2 def __init__(self, book): 3 self.book = book 4 5 def print_book_details(self): 6 print(f"Title: {self.book.get_title()}") 7 print(f"Author: {self.book.get_author()}") 8 9class Book: 10 def __init__(self, title, author): 11 self.title = title 12 self.author = author 13 14 def get_title(self): 15 return self.title 16 17 def get_author(self): 18 return self.author
In this scenario, the Library
class relies too heavily on the details of the Book
class, demonstrating inappropriate intimacy. The key distinction between Inappropriate Intimacy and Feature Envy is that inappropriate intimacy involves a significant intertwining between two classes, while feature envy is about a method's excessive interest in another class's data or behavior instead of its own.
To refactor, allow the Book
class to handle its own representation:
Python1class Library: 2 def __init__(self, book): 3 self.book = book 4 5 def print_book_details(self): 6 print(self.book.get_details()) 7 8class Book: 9 def __init__(self, title, author): 10 self.title = title 11 self.author = author 12 13 def get_details(self): 14 return f"Title: {self.title}\nAuthor: {self.author}"
This adjustment enables Book
to encapsulate its own details, encouraging better encapsulation and separation of concerns. 🛡️
Message Chains occur when classes need to traverse multiple objects to access the methods they require. Here's a demonstration:
Python1class User: 2 def __init__(self, address): 3 self.address = address 4 5 def get_address(self): 6 return self.address 7 8class Address: 9 def __init__(self, zip_code): 10 self.zip_code = zip_code 11 12 def get_zip_code(self): 13 return self.zip_code 14 15class ZipCode: 16 def __init__(self, postal_code): 17 self.postal_code = postal_code 18 19 def get_postal_code(self): 20 return self.postal_code 21 22# Usage 23user = User(Address(ZipCode("90210"))) 24postal_code = user.get_address().get_zip_code().get_postal_code()
The chain user.get_address().get_zip_code().get_postal_code()
illustrates this problem.
To simplify, encapsulate the access within methods:
Python1class User: 2 def __init__(self, address): 3 self.address = address 4 5 def get_user_postal_code(self): 6 return self.address.get_postal_code() 7 8class Address: 9 def __init__(self, zip_code): 10 self.zip_code = zip_code 11 12 def get_postal_code(self): 13 return self.zip_code.get_postal_code() 14 15class ZipCode: 16 def __init__(self, postal_code): 17 self.postal_code = postal_code 18 19 def get_postal_code(self): 20 return self.postal_code 21 22# Usage 23user = User(Address(ZipCode("90210"))) 24postal_code = user.get_user_postal_code()
This adjustment makes the User
class responsible for retrieving its postal code, creating a clearer and more direct interface. 📬
A Middle Man problem occurs when a class primarily exists to delegate its functionalities. Here's an example:
Python1class Controller: 2 def __init__(self, service): 3 self.service = service 4 5 def execute(self): 6 self.service.perform_action() 7 8class Service: 9 def perform_action(self): 10 # Action performed 11 pass
The Controller
doesn't do much beyond delegating to Service
.
To refactor, simplify delegation or reassign responsibilities:
Python1class Service: 2 def perform_action(self): 3 # Action performed 4 pass 5 6# Usage 7service = Service() 8service.perform_action()
By removing the unnecessary middle man, the design becomes more streamlined and efficient. 🔥
In this lesson, you've explored several code smells associated with suboptimal class collaboration and coupling, including Feature Envy, Inappropriate Intimacy, Message Chains, and Middle Man. By identifying and refactoring these smells, you can elevate your code's clarity and maintainability.
Get ready to put these concepts into practice with upcoming exercises, where you'll identify and refactor code smells, strengthening your skills. Keep striving for cleaner, more effective code! 🌟