programming

Coupling Explained in TypeScript

Coupling, a fundamental concept in software development, defines the interdependence between different components within a system. In TypeScript, a statically typed superset of JavaScript, managing coupling is crucial for building robust and maintainable applications. Let's explore the significance of coupling through real-world examples with TypeScript code, incorporating interface contracts to further enhance flexibility.

What is Coupling?

Coupling represents the level of connectivity between different modules or components in a system. It can be either tight, where changes in one module affect others significantly, or loose, allowing for greater independence and flexibility.

Low Coupling with Interfaces (Preferred)

Consider a scenario where a NotificationService module needs to send notifications, and it relies on an external service that adheres to the NotificationSender interface:

// File: NotificationSender.ts
interface NotificationSender {
    sendNotification(to: string, message: string): void;
}

// File: EmailSender.ts
class EmailSender implements NotificationSender {
    sendNotification(to: string, message: string): void {
        // Implementation to send email
    }
}

// File: NotificationService.ts
class NotificationService {
    private notificationSender: NotificationSender;

    constructor(notificationSender: NotificationSender) {
        this.notificationSender = notificationSender;
    }

    sendNotification(user: string, message: string): void {
        // Business logic
        this.notificationSender.sendNotification(user, message);
    }
}

Here, the NotificationService depends on the NotificationSender interface, ensuring low coupling. The EmailSender class implements this interface, allowing easy substitution with other classes that adhere to the same contract.

High Coupling (Avoid If Possible)

Now, imagine if the NotificationService directly used an instance of EmailSender:

// File: NotificationService.ts
class NotificationService {
    private emailSender: EmailSender = new EmailSender();

    sendNotification(user: string, message: string): void {
        // Business logic
        this.emailSender.sendNotification(user, message);
    }
}

In this case, there is a high coupling between NotificationService and EmailSender, making the code less flexible and harder to maintain.

Reducing Coupling with Dependency Injection and Interfaces

Let's continue with the concept of dependency injection and interfaces to reduce coupling. Consider an application file where dependencies are injected:

// File: App.ts
import { EmailSender } from './EmailSender';
import { NotificationService } from './NotificationService';

const emailSender = new EmailSender();
const notificationService = new NotificationService(emailSender);

notificationService.sendNotification('John Doe', 'New message received.');

By injecting the NotificationSender dependency into the NotificationService, we maintain low coupling and utilize the flexibility provided by interface contracts.

Benefits of Low Coupling

Interface Contracts

Clearly define expectations, enabling easy integration of different implementations.

Extensibility

Introduce new notification methods without affecting existing code.

Code Readability

Understand module interactions through well-defined interfaces.

Collaboration

Encourage teams to work on distinct modules without constant coordination.

Conclusion

Coupling is a critical factor in determining code maintainability and flexibility. Real-world scenarios often involve intricate dependencies, making it essential to embrace practices like dependency injection and interface contracts to achieve low coupling. By doing so, developers can ensure their applications remain adaptable and resilient in the face of evolving requirements.

Author avatar
About Authorpolubis

👋 Hi there! My name is Adrian, and I've been programming for almost 7 years 💻. I love TDD, monorepo, AI, design patterns, architectural patterns, and all aspects related to creating modern and scalable solutions 🧠.