Thursday, March 26, 2009

Closely coupled code Vs Loosely coupled code

I saw many of the users don't know meaning of this and there differences. I would like to post it here for the users.
=>

Basically, closely coupled code means that one part of the code is strongly dependent upon another part. This means that any change in one part may have unforeseen/unwanted effects on another part.

Ideally, loosely coupled code allows for separate parts of the application to not have that strong dependency on other parts. This has a number of benefits, such as keeping bugs isolated to one area of the code (instead of having cascading effects throughout the code) and it makes it easier to re-use portions of the code.

e.g. when you use "global" within a function, you then closely couple that function to whatever it is that initially defines that variable. If for some reason that other part of the code is changed to rename that variable or do things you didn't anticipate, it might break that function. Likewise, you might not realize that the function is doing things to that variable, causing unforeseen bugs in the application. And of course it becomes difficult to re-use such a function in another context if that same variable name does not exist there and have the same meaning/usage.

No comments:

Post a Comment