Question

In which of the following lines of code coupling occurs? What is the kind of coupling? What is the problem induced by this coupling? How can the code be refactored to reduce coupling?

a busy cat

Was it helpful?

Solution

One way to approach this is to look at everything that the function/method depends upon.

It explicitly depends on its arguments - a form of control coupling, in this case.

However if we tried to compile this method in isolation, we can see all the other objects that it depends upon:

  • Log, and some specific methods of Log (note that we always seem to call both of these methods)
  • IGNORE_USER_REQUESTS
  • LOG_VERBOSITY_LEVEL, which is repeated 4 times in this method
  • A specific Collector class.
  • mem and 4 attributes (size, startAddress, etc) - probably a form of content coupling

I think you could argue that there are several cases of common coupling (shared global variables) there, though it's not terribly clear from the limited context. And the variables may be scoped within an object or package, not truly 'global'.

Then consider:

  • What would happen if we wanted to change one of these items. For example, if we wanted to use a different Collector implementation, or a different logger; or the structure of mem changed?
  • How would we test this method?

Note that we can't properly assess the impact of this coupling without understanding the wider code; if these objects are encapsulated within a single small class, for example, then the impact is less than if these objects are scattered throughout the entire code. Similarly, trying to refactor this code is a bit tricky in isolation, since we can only guess what we might be affecting elsewhere in our hypothetical code, and which objects we are free to refactor (some may be from 3rd party libraries, for example).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top