Explain Cohesion and coupling ( .NET and C# Architecture level interview question) ?

Recently there was a .NET architect position interview in polaris and this question was asked to almost all candidates. Lot of people were aware of coupling but not of cohesion. So below goes a full explanation in detail with examples.

If you look at the dictionary meaning of cohesion it means sticking things together which are of the same types or substance.

Cohesion in software is a measure which tells how strongly related the responsibilities of a class or module is.

For example below is a class diagram of a simple “customer” class. In this class diagram “CustomerName” and “CustomerCode” are very much related to the “Customer” class but watch the “SupplierCode” property it’s not related directly.

Because this class has unrelated elements it has LOW.

Cohesion can be at any level class level, module level etc. For example below is a simple package diagram which groups classes in a “Customer” package.

Now Customers buy products and have multiple addresses. So it’s logical to have “Customer” , “Product” and “Address” in this namespace but look at “Exception” and “Logger” classes they are needed but they are not logically the part of “Customer” namespace.

add 2
So below grouping would look more logical and appropriate.

add 3
Coupling is a contrast to cohesion. Coupling says how much the classes and modules directly depend on each other. A good architecture is that architecture where change in one place should not lead to changes all over the places. If classes are directly connected with each other, then change in one class leads to changes in other class as well , so a loose coupling is more desirable.

Below is a simple class diagram which shows “Customer” and “Address” relationship. In the first section of the diagram “Customer” and “Address” classes are directly connected. In other words changes in “Address” class will lead to change in “Customer” class as well. So rather than these classes talk with each other directly they can talk via interfaces which the next section of the diagram shows. This is termed as decoupling.

add 4
Coupling and cohesion are measured using ordinal measurement: – LOW and HIGH. So we have low cohesion , high cohesion , low coupling and high coupling. Below table explains the same in detail.

High Low
Cohesion Classes and modules have related and logical elements together. This is a desirable situation. Elements are unrelated and are not desirable.
Coupling This means the classes are tightly coupled with each other and will make the software more difficult to maintain This means that classes are not tightly coupled and changes in one place will not affect the other section. This is a desirable situation.

In other words, good software architectures have high cohesion and low coupling.

One of the mostasked C# Architect interview question is about DI VS IOC. Below is a nice video created by www.questpond.com which explains the same practically.







About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s