# C#.net - What is the exact meaning of loose coupling

### Question

Tuesday, May 03, 2011 6:43 AM

• Hi Mathesvaran,

Consider a simple shopping cart application that uses a CartContents class to keep track of the items in the shopping cart and an Order class for processing a purchase. The Order needs to determine the total value of the contents in the cart, it might do that like so:

Tight Coupling Example Code :

```public class CartEntry
{
public float Price;
public int Quantity;
}

public class CartContents
{
public CartEntry[] items;
}

public class Order
{
private CartContents cart;
private float salesTax;

public Order(CartContents cart, float salesTax)
{
this.cart = cart;
this.salesTax = salesTax;
}

public float OrderTotal()
{
float cartTotal = 0;
for (int i = 0; i < cart.items.Length; i++)
{
cartTotal += cart.items[i].Price * cart.items[i].Quantity;
}
cartTotal += cartTotal*salesTax;
return cartTotal;
}
}

```

Notice how the OrderTotal method (and thus the Order class) depends on the implementation details of the CartContents and the CartEntry classes. If we were to try to change this logic to allow for discounts, we'd likely have to change all 3 classes. Also, if we change to using a List collection to keep track of the items we'd have to change the Order class as well. This is tight coupling that to the change in one class if it brings change in the other classes or objects, it is said to be tightly coupled and maintenance of such a code becomes too difficult to implement a slight change you need to change n number of places.

Loose Coupling Code :

```public class CartEntry
{
public float Price;
public int Quantity;

public float GetLineItemTotal()
{
return Price * Quantity;
}
}

public class CartContents
{
public CartEntry[] items;

public float GetCartItemsTotal()
{
float cartTotal = 0;
foreach (CartEntry item in items)
{
cartTotal += item.GetLineItemTotal();
}
return cartTotal;
}
}

public class Order
{
private CartContents cart;
private float salesTax;

public Order(CartContents cart, float salesTax)
{
this.cart = cart;
this.salesTax = salesTax;
}

public float OrderTotal()
{
return cart.GetCartItemsTotal() * (1.0f + salesTax);
}
}

```

The logic that is specific to the implementation of the cart line item or the cart collection or the order is restricted to just that class. So we could change the implementation of any of these classes without having to change the other classes. We could take this decoupling yet further by improving the design, introducing interfaces, etc.  Here the point is that we have made the implementations of the cart line item or the cart collection or the order so independent from one another that if you need to change the order class, possibly nothing needs to be changed in the other classes.  This is what loose coupling means and is considered to be the most appropriate programming approach in Object Oriented Programming.

The above codes are just examples and are not meant to run... its just an explanation of implementation of such codes in C#.

Thanks,

Roozan Parvez Bharucha MCT, MCITP (SQL Server 2008, Windows 7 Admin), MCTS (SQL Server 2008, Windows 7 Config, .NET Frmwk 3.5, .NET Frmwk 4.0 (Windows)), MCPD Enterprize App (Frmwk 3.5), MCPD Windows (Frmwk 4.0) CEO, RajAryanTech
• Proposed as answer by Tuesday, May 03, 2011 12:07 PM
• Marked as answer by Wednesday, May 11, 2011 6:37 AM
Tuesday, May 03, 2011 12:07 PM
• Hi Friend,
Loose coupling is nothing but a practice or approach to make sure the components in a system or a network is dependent to each other as much least as possible. It is doing so to reduce the risk that can happen when a change is made to any of the module or component in a system. Limiting the interconnections between those will help to isolate the problems if something goes wrong in any of these components. The trouble shooting and maintenance will be easy because the one which has the issue will be least connected to others....

-- Thanks Ajith R [MCTS:ASP.NET&Win Forms]
• Marked as answer by Wednesday, May 11, 2011 6:37 AM
Tuesday, May 03, 2011 8:23 AM
• it means the level of being able to just copy and paste the code to somewhere else. If you have a function, that internally references a global variable, then your code is depending on that external variable, which means that if you just copy and paste that code to another program, it won't just work, you need to copy the external variable too; this code is NOT loosely coupled. In OO terms, it means that your object should not depend on other objects, if possible.
Regards, Nico
• Marked as answer by Wednesday, May 11, 2011 6:37 AM
Tuesday, May 03, 2011 1:01 PM

### All replies

• Hi Friend,
Loose coupling is nothing but a practice or approach to make sure the components in a system or a network is dependent to each other as much least as possible. It is doing so to reduce the risk that can happen when a change is made to any of the module or component in a system. Limiting the interconnections between those will help to isolate the problems if something goes wrong in any of these components. The trouble shooting and maintenance will be easy because the one which has the issue will be least connected to others....

-- Thanks Ajith R [MCTS:ASP.NET&Win Forms]
• Marked as answer by Wednesday, May 11, 2011 6:37 AM
Tuesday, May 03, 2011 8:23 AM
• Hi Mathesvaran,

Consider a simple shopping cart application that uses a CartContents class to keep track of the items in the shopping cart and an Order class for processing a purchase. The Order needs to determine the total value of the contents in the cart, it might do that like so:

Tight Coupling Example Code :

```public class CartEntry
{
public float Price;
public int Quantity;
}

public class CartContents
{
public CartEntry[] items;
}

public class Order
{
private CartContents cart;
private float salesTax;

public Order(CartContents cart, float salesTax)
{
this.cart = cart;
this.salesTax = salesTax;
}

public float OrderTotal()
{
float cartTotal = 0;
for (int i = 0; i < cart.items.Length; i++)
{
cartTotal += cart.items[i].Price * cart.items[i].Quantity;
}
cartTotal += cartTotal*salesTax;
return cartTotal;
}
}

```

Notice how the OrderTotal method (and thus the Order class) depends on the implementation details of the CartContents and the CartEntry classes. If we were to try to change this logic to allow for discounts, we'd likely have to change all 3 classes. Also, if we change to using a List collection to keep track of the items we'd have to change the Order class as well. This is tight coupling that to the change in one class if it brings change in the other classes or objects, it is said to be tightly coupled and maintenance of such a code becomes too difficult to implement a slight change you need to change n number of places.

Loose Coupling Code :

```public class CartEntry
{
public float Price;
public int Quantity;

public float GetLineItemTotal()
{
return Price * Quantity;
}
}

public class CartContents
{
public CartEntry[] items;

public float GetCartItemsTotal()
{
float cartTotal = 0;
foreach (CartEntry item in items)
{
cartTotal += item.GetLineItemTotal();
}
return cartTotal;
}
}

public class Order
{
private CartContents cart;
private float salesTax;

public Order(CartContents cart, float salesTax)
{
this.cart = cart;
this.salesTax = salesTax;
}

public float OrderTotal()
{
return cart.GetCartItemsTotal() * (1.0f + salesTax);
}
}

```

The logic that is specific to the implementation of the cart line item or the cart collection or the order is restricted to just that class. So we could change the implementation of any of these classes without having to change the other classes. We could take this decoupling yet further by improving the design, introducing interfaces, etc.  Here the point is that we have made the implementations of the cart line item or the cart collection or the order so independent from one another that if you need to change the order class, possibly nothing needs to be changed in the other classes.  This is what loose coupling means and is considered to be the most appropriate programming approach in Object Oriented Programming.

The above codes are just examples and are not meant to run... its just an explanation of implementation of such codes in C#.

Thanks,

Roozan Parvez Bharucha MCT, MCITP (SQL Server 2008, Windows 7 Admin), MCTS (SQL Server 2008, Windows 7 Config, .NET Frmwk 3.5, .NET Frmwk 4.0 (Windows)), MCPD Enterprize App (Frmwk 3.5), MCPD Windows (Frmwk 4.0) CEO, RajAryanTech
• Proposed as answer by Tuesday, May 03, 2011 12:07 PM
• Marked as answer by Wednesday, May 11, 2011 6:37 AM
Tuesday, May 03, 2011 12:07 PM
• it means the level of being able to just copy and paste the code to somewhere else. If you have a function, that internally references a global variable, then your code is depending on that external variable, which means that if you just copy and paste that code to another program, it won't just work, you need to copy the external variable too; this code is NOT loosely coupled. In OO terms, it means that your object should not depend on other objects, if possible.
Regards, Nico
• Marked as answer by Wednesday, May 11, 2011 6:37 AM
Tuesday, May 03, 2011 1:01 PM
• Thank you. Make sense was clear
Wednesday, May 18, 2011 1:07 PM