Android, Best Practices

Low Coupling and High Cohesion: A Guide to Writing Better Code

Introduction

As software systems grow in complexity, it becomes increasingly important to write code that is maintainable and easy to understand. Two key principles that can help you achieve this goal are low coupling and high cohesion. In this blog post, we’ll explore these principles, and provide examples of how you can apply them to your own code.

Low Coupling

Low coupling is the idea that each module or component of your code should be as independent as possible from other modules or components. This means that changes to one component should not require changes to other components. The goal is to minimize the impact of changes, and to make your code more modular and easier to maintain.

For example, let’s say you have a program that reads data from a file, and then processes it. Instead of tightly coupling the file reading and processing logic, you can create separate modules for each task, with a minimal interface between them:

interface FileReader {
    fun read(file: File): List<String>
}

class SimpleFileReader : FileReader {
    override fun read(file: File): List<String> {
        // read the file using simple logic
        return listOf()
    }
}

interface DataProcessor {
    fun process(data: List<String>)
}

class SimpleDataProcessor : DataProcessor {
    override fun process(data: List<String>) {
        // process the data using simple logic
    }
}

By keeping the interface between the two modules simple, you can easily swap out one module for another, without affecting the other. This makes your code more modular, and easier to maintain and extend.

High Cohesion

High cohesion is the idea that each module or component of your code should be responsible for a single, well-defined task. This means that each component should have a clear purpose, and should not be responsible for multiple tasks. The goal is to improve the clarity and maintainability of your code.

For example, let’s say you have a program that manages customer data. Instead of having a single module that handles all customer-related tasks, you can create separate modules for each task:

interface CustomerRepository {
    fun findById(id: String): Customer
    fun save(customer: Customer)
    fun delete(customer: Customer)
}

interface CustomerValidator {
    fun validate(customer: Customer): Boolean
}

interface CustomerExporter {
    fun export(customer: Customer, format: String): File
}

By separating the tasks into separate modules, you can improve the clarity and maintainability of your code. Each module has a clear purpose, and can be modified or replaced without affecting other parts of the system.

Conclusion

In conclusion, low coupling and high cohesion are important principles for writing maintainable and understandable code. By keeping your components independent and well-defined, you can create code that is modular, easy to extend, and easy to maintain. While it can be challenging to apply these principles at first, they can help you write better code that is more reliable, easier to understand, and more maintainable over time.