Coupling vs Cohesion
Coupling and cohesion are two concepts found in Java (and all other object oriented languages). Coupling measures how much each of the program modules are dependent on the other program modules. Cohesion measures how strongly each of the functions are related within a module. Actually, any object oriented language (including Java) has the two main objectives of increasing cohesiveness and decreasing the coupling at the same time, in order to develop most efficient programmes. These two software engineering metrics were developed by Larry Constantine to reduce the cost of modifying and maintaining software.
What is Cohesion?
Cohesion measures how strongly each of the functions are related within a program module. Well structured classes lead to highly cohesive programs. If a certain class is performing a set of highly related functions, that class is said to be cohesive. On the other hand, if a class is performing a bunch of totally unrelated functionalities that means the class is not cohesive at all. It is important to understand that not having cohesiveness does not mean that the overall application does not have the required functionality. It’s just that without cohesion, it will be very difficult to mange the functionality because they will be scattered in many wrong places as the complexity of the application increases over time. Maintaining, modifying and extending behaviors scattered all over the code is very tedious even for the most experience programmers.
What is Coupling?
Coupling measures how much each of the program modules are dependent on the other program modules. Interactions between two objects occur because there is coupling. Loosely-coupled programs are high in flexibility and extensibility. Strong coupling is never good because one object can be highly dependent on some other object. This is a nightmare when the code is modified, because high coupling means that the programmers need to work on several places of code for even a single behavioral modification. Strong coupling always leads to programs with low flexibility and less scalability/extensibility. However, in programming languages like Java, completely avoiding coupling is impossible. But it is recommended that the programmers give their best effort to reduce the coupling as much as possible. It is also possible to have some coupling to help objects interact with each other without hampering its scalability and flexibility.
What is the difference between Coupling and Cohesion?
Even though coupling and cohesion deal with the quality of a module in software engineering, they are entirely different concepts. Cohesion talks about how much the functionality are related to each other within the module, while coupling deals with how much one module is dependent on the other program modules within the whole application. In order to have the best quality software, cohesion and coupling should reach the two opposite ends of their spectrums. In other words, loose coupling and strong cohesion provides the best software. Having private fields, non-public classes and private methods provide loose-coupling, while making all members visible within the class and having package as the default visibility provide high cohesion.