To write code that is easy to delete but not easy to extend, we can focus on making the code very simple and tightly coupled, avoiding abstraction and modularity. By doing so, it’s easy to remove a block of code because it doesn’t have many dependencies or complex interactions with other parts of the system, but extending or modifying that code would be difficult because of a lack of flexibility and extensibility.
Here’s an example in Python that illustrates this concept:
Example: A Simple, Hard-to-Extend Code
class UserService:
def create_user(self, name, email):
print(f"Creating user: {name} with email: {email}")
# Hardcoded logic for user creation
if "@" not in email:
print("Invalid email!")
else:
print("User created successfully.")
def delete_user(self, user_id):
print(f"Deleting user with ID: {user_id}")
# Direct delete logic with no checks or abstractions
class EmailService:
def send_welcome_email(self, email):
print(f"Sending welcome email to {email}")
# Simple hardcoded email sending logic
print(f"Email sent to {email}.")
# A tightly coupled system
class UserManager:
def __init__(self):
self.user_service = UserService()
self.email_service = EmailService()
def register_user(self, name, email):
self.user_service.create_user(name, email)
self.email_service.send_welcome_email(email)
def remove_user(self, user_id):
self.user_service.delete_user(user_id)
print(f"User {user_id} removed from all systems.")
Why is this easy to delete?
- Minimal Dependencies: The code doesn’t have layers of abstractions or interfaces, meaning you can easily remove or delete entire classes like
UserService
orEmailService
without worrying about too many dependencies or side effects. - Simple Logic: There’s no complicated setup, no external dependencies, and no need to track multiple moving parts.
Why is this not easy to extend?
- Tight Coupling: The
UserManager
directly instantiatesUserService
andEmailService
. To extend the behavior (like adding a new service for notifications), you would need to modifyUserManager
and other places where these services are used. It’s hard to change without altering the entire class. - No Abstractions or Interfaces: There are no abstractions that could allow for easy swapping of different service implementations. If we wanted to add more types of services (e.g., adding support for SMS notifications), we would need to rewrite a lot of existing code.
How to make it easier to delete:
- If you wanted to delete something, you could easily remove the entire
UserService
class andEmailService
class without breaking much, as there are no deep dependencies or complex interactions.
How to make it harder to extend:
- If you need to extend functionality, you’d need to modify the core logic (like
UserManager
) and introduce changes across various places in the codebase. There’s no easy way to plug in new services or logic.
This code has been deliberately structured to be simple to remove, but difficult to extend due to tight coupling and lack of flexibility.